1

There are a lot questions on this, but they weren't able to help me or I didn't understand it..

Basically I want user to press the button before system goes back to main method. In this case if system goes back to main method, system will quit.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class test123 implements ActionListener{


JTable table;
JButton button;
JFrame frame; 

public test123 (){

    frame = new JFrame();
    frame.setLayout(null);

    button = new JButton("Finish");
    button.setBounds(200, 10, 70, 40);
    button.addActionListener(this);

    frame.add(button);


    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setSize(600, 200);
    frame.setVisible(true);
    frame.setTitle("TEst123");  
}


public void actionPerformed(ActionEvent e){
    if(e.getSource() == button){
        System.out.println("message....");
    }
}

public static void main(String arg[]){
    test123 gui = new test123();

    System.exit(0);

}
}

Sorry if it was just me lacking the understanding and thank you for help.

EDIT: Maybe I explained this incorrectly or displayed it incorrectly. Lets say if the system goes back to main system it will do something I don't want, therefor I want the user to press the button to go back to the main system or do "the thing". Sorry for bad explanation, including this one.

This class is separate from my work and I just used it to test stuff... In my project, user can choose from several buttons (lets say main method is the menu in this case). The user presses a button goes to a new window/frame, if the program doesn't pause or waits for button to be pressed, it will go back to main method.

  • So, you want the program to close when the button is pressed ? – ortis Sep 05 '14 at 16:47
  • 1
    *"but they weren't able to help me or I didn't understand it.."* So what is there to believe we can explain it in a way *you* can understand? Perhaps start with - what were the questions, and how did those answers not help you - or what did you not understand about those answers? – Andrew Thompson Sep 05 '14 at 16:48
  • 2
    Move `System.exit(0);` into the `actionPerformed(ActionEvent)` method. BTW - what resources are you using to learn Swing? It seems to be giving you many bad habits (AKA - 'that code is crap'). – Andrew Thompson Sep 05 '14 at 16:50
  • @Aeshang probably he want "Finish" button to be pressed not a keyboard key... – Betlista Sep 05 '14 at 16:54
  • add a mouse listener on the button and implement mouse up method... – StackFlowed Sep 05 '14 at 16:56
  • 1
    @Aeshang That ranks up near the silliest comment I've read today. First of all, an `ActionListener` is the proper listener for a button (usually). Secondly the OP wants to move it on key presses, rather than button clicks. Thirdly, it does not address the actual problem. – Andrew Thompson Sep 05 '14 at 17:27
  • 2
    Your repetitive statement of _"go back to the main"_ method makes absolutely no sense. The main method should generally never be called. It is the simple the launching point for the jvm. Swing is Event Driven, meaning everything happens though events. There _is_ no _"waiting"_. When a user activate a component, code that listens for those event does something. If you want to have a main screen, then just have the main menu button listener show the main screen. – Paul Samsotha Sep 05 '14 at 17:27
  • *"The user presses a button goes to a new window/frame, if the program doesn't pause.."* If the 2nd frame is a modal dialog, the app. will wait. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Sep 05 '14 at 17:40

1 Answers1

1

The quick answer is what Andrew Thompson wrote in his comment:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;

public class test123 implements ActionListener {

    JTable table;
    JButton button;
    JFrame frame;

    public test123() {

        frame = new JFrame();
        frame.setLayout(null);

        button = new JButton("Finish");
        button.setBounds(200, 10, 70, 40);
        button.addActionListener(this);

        frame.add(button);

        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(600, 200);
        frame.setVisible(true);
        frame.setTitle("TEst123");
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            System.out.println("message....");
//          System.exit(0);
            frame.dispose(); // better than exit
        }
    }

    public static void main(String arg[]) {
        test123 gui = new test123();
    }

}

but Java class name should start with upper case test123 -> Test123 (but you can find a lot better name for sure).

Why not to extend JFrame also?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Test123 extends JFrame implements ActionListener {

    private static final long serialVersionUID = 3378774311250822914L;

//  private JTable table;
    private JButton button;
//  JFrame frame;

    public Test123() {

//      frame = new JFrame();
        this.setLayout(null);

        button = new JButton("Finish");
        button.setBounds(200, 10, 70, 40);
        button.addActionListener(this);

        this.add(button);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(600, 200);
        this.setTitle("Test123");
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            System.out.println("message....");
//          System.exit(0);
            this.dispose();
        }
    }

    public static void main(String arg[]) {
        Test123 gui = new Test123();
    }

}

Read more in Concurrency in Swing tutorial to find out how to deal with long running tasks out of dispatch thread...


From your question it seems, that you do not know how swing application behaves. You create a GUI and the you are waiting for user's input. So basically you do not care what was your program executing, when user pressed the button... (and therefore it doesn't matter where it returns to)

Betlista
  • 10,327
  • 13
  • 69
  • 110
  • 1
    Good call on changing the `System.exit(0)` to `dispose()` as well as the many other tips. There are still things of concern in the code, but they go beyond the scope of the question. – Andrew Thompson Sep 05 '14 at 17:30
  • @AndrewThompson feel free to share, in bigger frame I'd use layout (but not needed in this simple example) and Also setting size is not something I prefer (but this is related to that layout thing also). Of course in bigger example usage of panels is better, strings should be localized in production code, but for this example it would be too much... – Betlista Sep 05 '14 at 17:38
  • 1
    I agree. I think you have already covered more than the OP has asked, the extra tips are simply 'the cream on the cake'. The 'other concerns' I was referring to was pretty much the horrid lack of use of layout managers, but I'd only go into that if the OP asked (on a dedicated question). It's too much to go into just to answer this question. – Andrew Thompson Sep 05 '14 at 17:42