Pass JFrame to actionlistener
Hello,
I created a class in a frame with menu. Depending on which item is clicked in the menu to another panel to the frame to be added. Especially should it become a Sudoku 9x9 box, and another time a 16x16 Sudoku field. The 'order' sake, I've outsourced all events in a different class. But how can I tell the class "events" which is frame? Can you please provide me this logic? If you have any source code then it would be great. Thank you.
Re: Pass JFrame to actionlistener
Hello,
Try the following code
Code:
public class Frame (
public static void main(String[] args) (
JFrame frm1 = new JFrame("Sudoku Solver");
frm1.setSize(500,500);
frm1.setResizable(false);
frm1.setLocationRelativeTo(null);
frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar mb = new JMenuBar();
frm1.setJMenuBar(mb);
JMenu prg = new JMenu("Program");
mb.add(prg);
JMenuItem ext = new JMenuItem("Exit");
prg.add(ext),
ext.addActionListener(new events.exitaction());
JMenu vrt = new JMenu("vrt");
mb.add(variant);
JMenu sdk = new JMenu("Sudoku - normal");
JMenuItem sudo = new JMenuItem("9x9 Sudoku field");
JMenuItem sudokun16mal16 = new JMenuItem("16x16 Sudoku box);
sdk.add(sudo);
sudo.addActionListener(new events.sudo());
sdk.add(sudokun16mal16);
JMenuItem sudotst = new JMenuItem("Killer Sudoku");
variations.add(sdk);
variations.add(sudotst);
frm1.setVisible(true);
)
)
Re: Pass JFrame to actionlistener
Hello,
Alternatively you can try the following code
Code:
public class Events (
public static class ext implements ActionListener(
public void actionPerformed (ActionEvent e)(
System.exit(0);
)
)
static class sudo implements ActionListener(
public void actionPerformed (ActionEvent e)(
JPanel pn = new JPanel();
frame1.add(pn); / / here is the problem
JLabel lbl = new JLabel("test");
pn.add(lbl);
)
)
)
Re: Pass JFrame to actionlistener
Hello,
In your code the class sudo a JFrame is passed as the attribute would look like this:
Code:
static class sudo implements ActionListener(
private JFrame frm;
public sudo(JFrame frame) (
this.frm = frame;
)
The rest can be dismissed easily, then you should not have any problem to access it from your code. I hope this will help.
Re: Pass JFrame to actionlistener
Hello,
In your case I think the outsourcing Overkill the part of the code. Your Events are not exactly complex, I think with anonymous classes is clearer and not so cumbersome for the frame access.
Code:
sudo.addActionListener(new ActionListener()(
@ Override
public void actionPerformed(ActionEvent e)(
frm.add(...); frm must be declared as final Attention / /!
)
));
Incidentally, the complete code of the main method you should pack in a Runnable, for example
Code:
EventQueue.invokeLater (testrun); run.
Re: Pass JFrame to actionlistener
Hello,
Thank you for your replies, only one question - in the action listener can I create a label and add it to the panel and the frame added? It is not displayed, only when I have a renewed "frm.setVisible (true). Why do I run the command again, but the frame is already displayed?