Handle a JButton click event in another class

I am new to java from C #, so I am not familiar with the best Java practices.

I have a main class that opens a JFrame to receive multiple input lines from a user. When the user clicks the submit button, the GUI should close and the main class continues processing using input.

This is the main class:
public class Main { FInput fInput; public void main(String[] args) { if(args.length==0) { fInput = new FInput(); fInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fInput.pack(); fInput.setVisible(true); } else startProcess(args); } public void startProcess(String[] args) { // Do stuff } 

The main class will use this frame to get input from the user:

 public class FInput extends JFrame{ private JTextField txtSourceDirectory; private JTextField txtTargetDirectory; private JTextField txtDefectNumber; private JTextField txtSliceTokens; private JButton btnStart; public FInput() { // Initialize text fields and button JButton.addActionListener(something); } } 

In all the examples that I could find, the listener will be FMain itself. However, in this case, I want Main to listen and use input in the startProcess method.

Will the main ActionListener tool, and pass it to the FMain constructor, is that the way to go?
+7
source share
3 answers

Yes, that’s the right idea. You have to do two things in order to be able to do this, though:

  • Put this at the beginning of the FInput class:

     Main m = new Main(this); 
  • Then put these lines somewhere in the Main class ...

     FInput gui; public Main(FInput in) { gui = in; } 

Now you can access any component of the FInput class from the Main class by doing something like this.

 gui.someComponent ... 

To configure listeners, simply write someComponent.addItemListener(m); or something like that.

Hope this helps!


@Yoav In response to your last comment ...

You do not need to separate the listening class from the GUI class; you can combine them into one class ...

 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame implements ActionListener { private JTextField txtSourceDirectory; private JTextField txtTargetDirectory; private JTextField txtDefectNumber; private JTextField txtSliceTokens; private JButton btnStart; public Main() { txtSourceDirectory = new JTextField(40); //change this to the amount of characters you need txtTargetDirectory = new JTextField(40); txtDefectNumber = new JTextField(40); txtSliceTokens = new JTextField(40); btnStart = new JButton("Start"); add(txtSourceDirectory); add(txtTargetDirectory); add(txtDefectNumber); add(txtSliceTokens); add(btnStart); btnStart.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public void actionPerformed(ActionEvent event) { //do stuff } static void startProcess(String[] ARGS) { //do stuff } public static void main(String[] args) { if (args.length == 0) { Main frame = new Main(); } else { startProcess(args); } } } 
+5
source

The first main method in java should always be public static void . The following is an example of how this can be done.

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; /** * Main class is frame but also implements ActionListener interface. */ public class Main extends JFrame implements ActionListener { private JButton btnStart; private static Main frame; public Main() { JPanel panel = new JPanel(); btnStart = new JButton("Press me"); // Add action listener. Listener could be any class that implements // ActionListener btnStart.addActionListener(this); // This means add button btnStart to panel panel.add(btnStart); // This means add panel to frame this.add(panel); } // main method in java always must be public, static and void. You forgot to // put static. public static void main(String[] args) { if (args.length == 0) { frame = new Main(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } else frame.startProcess(args); } public void startProcess(String[] args) { // TODO } @Override public void actionPerformed(ActionEvent arg0) { // Here you put your code that is executed every time you press button. // For example you just want to show message. JOptionPane.showMessageDialog(this, "You pressed Button."); } } 

But it is much better to have a special class. For example:

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JOptionPane; public class ButtonListener implements ActionListener { JFrame parent; public ButtonListener(JFrame parent) { this.parent = parent; } @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(parent, "You pressed Button"); } } 

And in the main class, you simply add an action listener button to the button:

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; /** * Main class is frame but also implements ActionListener interface. */ public class Main extends JFrame { private JButton btnStart; private static Main frame; public Main() { JPanel panel = new JPanel(); btnStart = new JButton("Press me"); ButtonListener listener = new ButtonListener(this); // Add action listener. Listener could be any class that implements // ActionListener btnStart.addActionListener(listener); // This means add button btnStart to panel panel.add(btnStart); // This means add panel to frame this.add(panel); } // main method in java always must be public, static and void. You forgot to // put static. public static void main(String[] args) { if (args.length == 0) { frame = new Main(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } else frame.startProcess(args); } public void startProcess(String[] args) { // TODO } } 
+2
source

Also consider using the JOptionPane shown here in the Main class. You can customize the appearance, including the button text , as shown in How to create dialog boxes .

+2
source

All Articles