Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Pass JFrame to actionlistener

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-08-2010
Member
 
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.

Reply With Quote
  #2  
Old 23-08-2010
Member
 
Join Date: Apr 2008
Posts: 2,278
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);
    )
)
Reply With Quote
  #3  
Old 23-08-2010
Member
 
Join Date: Aug 2008
Posts: 717
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);
        )
    )
)
Reply With Quote
  #4  
Old 23-08-2010
Member
 
Join Date: Apr 2008
Posts: 2,568
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.
Reply With Quote
  #5  
Old 23-08-2010
Member
 
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.
Reply With Quote
  #6  
Old 23-08-2010
Member
 
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?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Pass JFrame to actionlistener"
Thread Thread Starter Forum Replies Last Post
Network Diagram - Configure to show Forward Pass/Backward Pass m3lyssa Microsoft Project 9 30-08-2010 04:57 PM
Where to use actionlistener ? Anthony12 Software Development 7 09-08-2010 10:20 AM
Add actionlistener to JButton KAMANA Software Development 6 03-08-2010 10:54 AM
Query regarding use of ActionListener in java beelow Software Development 3 16-11-2009 09:56 AM
How to pass object to jframe Brunoz Software Development 3 06-08-2009 11:36 AM


All times are GMT +5.5. The time now is 01:57 AM.