• OK, it's on.
  • Please note that many, many Email Addresses used for spam, are not accepted at registration. Select a respectable Free email.
  • Done now. Domine miserere nobis.

Java Button

Black Rose

An unbreakable bond
Local time
Today 4:05 AM
Joined
Apr 4, 2010
Messages
11,431
---
Location
with mama
I am trying to make java program with a start button to begin a thread. The problem is that I need the thread to stop inside the run method because when I press the start button twice it gives me an error message. I also need to draw numbers from a loop in the thread onto the JFrame with paint. I pass the numbers with variable (i) into paint. The number should change with every repaint. I am not sure what I am doing wrong in my code. If someone could check it out I would be grateful. :)

Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication5;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Jeremy
 */

class MyClass implements Runnable {
    public void run (){
        for(int i=0; i<10; i++){
        repaint(i);
        System.out.println("Error value: " + i);
        }
        requestStop();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void requestStop() {
        Thread.interrupted();
    }
}

public class JavaApplication5 extends JPanel{

    /**
     * @param args the command line arguments
     */
    int i = 0;
    
    public void paint(Graphics g) { 
        int i = 0;
        g.drawString("Watch the number change:" + i , 50, 50);        
    }
    
    public static void main(String[] args) {
        JPanel p = new JPanel(new GridBagLayout());
        JTextField jt = new JTextField(20);
        JButton b = new JButton("number");
        JFrame f = new JFrame("Recurrent Neural Net");
        f.add(new JavaApplication5());
        f.setSize((516), (539));
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p.add(b);
        p.add(jt);
        f.add(p);
        
        Thread t = new Thread(new MyClass());
        
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                t.start();     
            }          
        });
    }   
}
 

Black Rose

An unbreakable bond
Local time
Today 4:05 AM
Joined
Apr 4, 2010
Messages
11,431
---
Location
with mama
I do not understand code that well.
These two sections in green I cannot figure out.

Code:
package javaapplication5;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JavaApplication5 extends JPanel{
    
    public static void main(String[] args) {
        JPanel p = new JPanel(new GridBagLayout());
        JTextField jt = new JTextField(20);
        JButton b = new JButton("Start");
        JFrame f = new JFrame("Recurrent Neural Net");
        f.add(new JavaApplication5());
        f.setSize((516), (539));
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p.add(b);
        p.add(jt);
        f.add(p);
        
        Thread t = new Thread(new MyClass());
        
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int x = 0;
                t.start();
                x++;
                
                [COLOR="Lime"]if(x >= 2) {
                    t.interrupted();
                }[/COLOR]
                
            }          
        });
    }
    
    int i = 0;
    public void paint(Graphics g) {
        int i = 0;
        g.drawString("Watch the number change:" + i , 50, 50);        
    }
}

class MyClass implements Runnable {
    public void run (){
        for(int i=0; i<10; i++){
[COLOR="Lime"]        //main.paint(i);[/COLOR]
        System.out.println("Error value: " + i);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 

Ex-User (9086)

Prolific Member
Local time
Today 11:05 AM
Joined
Nov 21, 2013
Messages
4,758
---
actionPerformed() keeps track of :

x is 0

t.start is a start() method of object t of a constructor class thread T of class MyClass

x is postfix incremented to 1

if(x >= 2) is the break/interrupt routine for actionPerformed counter on x=2

t.interrupted() is the actual interrupt method for the object/thread t

it's what's going to display the error message after 2 actions, button presses

main.paint(i) is commented out and doesn't run in the code, if it did it would probably make use of the object main and method drawstring of the function paint that's probably inherited from one of the import graphics libraries from the beginning

You can find the .interrupted() method of MyClass in your previous post. Which is used for interrupting the thread t.
public void requestStop() { Thread.interrupted(); } }
Try running it without the interrupt exception and explain what's not showing the way you expect it to show, I don't understand what you expect it to do.
 

Black Rose

An unbreakable bond
Local time
Today 4:05 AM
Joined
Apr 4, 2010
Messages
11,431
---
Location
with mama
You can find the .interrupted() method of MyClass in your previous post. Which is used for interrupting the thread t.
public void requestStop() { Thread.interrupted(); } }
Try running it without the interrupt exception and explain what's not showing the way you expect it to show, I don't understand what you expect it to do.

Press button once = thread start

main.paint(i); should paint i to the gui as a loop (1 then 2 then 3 ...)

press button a second time = thread stop

---

t.stop(); does not work. it does not stop the thread so I did x >= 2 to represent the button pressed twice. When x = 2 then start button is pressed twice and the thread should stop. action listener should stop the thread when start button is pressed twice.

main.paint(i); when not commented out sends errors and the program does not run.

7bvZVgi.png
 

Ex-User (9086)

Prolific Member
Local time
Today 11:05 AM
Joined
Nov 21, 2013
Messages
4,758
---
I don't see how you change i in the "watch this number change" so that it increases. You have some loop in implements runnable.

I'm sick and too distracted to try it on my own, but one of the best ways to learn functionality is focus on one function at a time.

If you want to start and stop threads, then write the most basic program you can just for that and confirm that it works, then write another one for counting starts and try to link the two later. Now you're stuck in a situation where you have to fix two functions in a single program without altering the rest that you wrote.

If an app crashes or doesn't deliver the results you like then setup breakpoints in code and follow it step by step watching the critical variables change. Not sure which IDE you're using but Eclipse certainly allows you to debug / breakpoint the code, it's also free and open source and all that jazz.

Also some info that might be of use is that Java suggests you stop using .stop and find another way of ending threads like .interrupt or other method. As far as I know in java you don't have to completely close threads as long as you cease its functionality, java does the rest for you.
 
Top Bottom