Results 1 to 6 of 6

Thread: Pass JFrame to actionlistener

  1. #1
    Join Date
    Aug 2010
    Posts
    34

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,279

    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);
        )
    )

  3. #3
    Join Date
    Aug 2008
    Posts
    725

    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);
            )
        )
    )

  4. #4
    Join Date
    Apr 2008
    Posts
    2,569

    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.

  5. #5
    Join Date
    Dec 2007
    Posts
    765

    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.

  6. #6
    Join Date
    Aug 2010
    Posts
    34

    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?

Similar Threads

  1. Replies: 9
    Last Post: 30-08-2010, 04:57 PM
  2. Where to use actionlistener ?
    By Anthony12 in forum Software Development
    Replies: 7
    Last Post: 09-08-2010, 10:20 AM
  3. Add actionlistener to JButton
    By KAMANA in forum Software Development
    Replies: 6
    Last Post: 03-08-2010, 10:54 AM
  4. Query regarding use of ActionListener in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 09:56 AM
  5. How to pass object to jframe
    By Brunoz in forum Software Development
    Replies: 3
    Last Post: 06-08-2009, 11:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •