Results 1 to 7 of 7
  1. #1
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default Accessing JFrame from seperat file?

    I've built a simple programe to illustrat my problem. I'm trying to use a JFrame in my mane page and to build my JPanel in a seperat file. My problem is that I can't find how to switch JPanel from the seperate file.

    Here's my 3 pages:

    test.java

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class test {
    	public static JFrame frame = new JFrame ("MyPanel");
        
    	public static void main (String[] args) {
            page1 MyPanel1 = new page1();
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (MyPanel1);
            frame.pack();
            frame.setVisible (true);
        }
    }
    page1.java

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class page1 extends JPanel {
    	
    	public page1() {
    
        	JButton page1BTN = new JButton ("Page 1");
            page1BTN.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	page1BTN_Clicked(e);
                }
            });
            
            setPreferredSize (new Dimension (234, 175));
            setLayout (null);
            add (page1BTN);
    
            page1BTN.setBounds (55, 70, 100, 20);
        }
    
        protected void page1BTN_Clicked(ActionEvent e) {
        	page2 MyPanel2 = new page2();
    		super.removeAll();
        	super.add(MyPanel2);
        	super.validate();
    	}
    
    }
    page2.java

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class page2 extends JPanel{
    	
    	public page2()  {
    
            JButton page2BTN = new JButton ("Page 2");
            page2BTN.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	page2BTN_Clicked(e);
                }
            });
            
            setPreferredSize (new Dimension (234, 175));
            setLayout (null);
            add (page2BTN);
    
            page2BTN.setBounds (55, 70, 100, 20);
        }
    	
        protected void page2BTN_Clicked(ActionEvent e) {
        	page1 MyPanel1 = new page1();
        	super.removeAll();
        	super.add(MyPanel1);
        	super.validate();
    	}
    }
    Newb today, Pro tomorrow,
    Soulmed

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Talking Answer to my own question...

    For those out there with the same problem reading this thread, here's my working code:

    test.java
    Java Code:
    import javax.swing.*;
    
    public class test {
    	public static JFrame frame = new JFrame ("MyPanel");
    	public static void main (String[] args) {
            page1 MyPanel1 = new page1();
            
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (MyPanel1.getPanel());
            frame.pack();
            frame.setVisible (true);
        }
    	public static void changePanel(JPanel jp){
    		//JOptionPane.showMessageDialog(null,frame);
    		frame.getContentPane().removeAll();		
            frame.getContentPane().add (jp);
            frame.pack();
            frame.validate();
    	}
    }
    page1.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class page1 extends test {
    	
    	public page1() {
    	}
    	public JPanel getPanel(){
    		JPanel jp = new JPanel();
        	JButton page1BTN = new JButton ("Page 1");
            page1BTN.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	page2 MyPanel2 = new page2();
                	changePanel(MyPanel2.getPanel());
                }
            });
            
            jp.setPreferredSize (new Dimension (234, 175));
            jp.setLayout (null);
            jp.add (page1BTN);
    
            page1BTN.setBounds (55, 70, 100, 20);
            return jp;
        }
    
    }
    page2.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class page2 extends test{
    	
    	public page2()  {
    	}
    	public JPanel getPanel(){
    		JPanel jp = new JPanel();
            JButton page2BTN = new JButton ("Page 2");
            page2BTN.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	page1 MyPanel1 = new page1();
                	changePanel(MyPanel1.getPanel());
                }
            });
            
            jp.setPreferredSize (new Dimension (200, 150));
            jp.setLayout (null);
            jp.add (page2BTN);
    
            page2BTN.setBounds (55, 70, 100, 20);
            return jp;
        }
    	
    }
    Newb today, Pro tomorrow,
    Soulmed

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The problem I have with your code is that you repeatedly recreate your two JPanels for no reason. Why not simply use a CardLayout and swap JPanels based on a constant String. e.g.,

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Test2 {
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Test2");
          frame.getContentPane().add(new MainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class MainPanel extends JPanel {
       public static final String SUB_PANEL_1 = "Sub Panel 1";
       public static final String SUB_PANEL_2 = "Sub Panel 2";
       private SubPanel subPanel1 = new SubPanel(this, SUB_PANEL_1, SUB_PANEL_2);
       private SubPanel subPanel2 = new SubPanel(this, SUB_PANEL_2, SUB_PANEL_1);
       private CardLayout cardlayout = new CardLayout();
       
       public MainPanel() {
          setLayout(cardlayout);
          add(subPanel1, SUB_PANEL_1);
          add(subPanel2, SUB_PANEL_2);
       }
       
       public void swapPanel(String nextPanelName) {
          cardlayout.show(this, nextPanelName);
       }
    }
    
    class SubPanel extends JPanel {
       private MainPanel mainPanel;
       private String nextPanelName;
    
       public SubPanel(MainPanel mainPanel, String thisPanelName, String nextPanelName) {
          this.mainPanel = mainPanel;
          this.nextPanelName = nextPanelName;      
          
          JButton nextPanelBtn = new JButton("Show " + nextPanelName);
          nextPanelBtn.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                nextPanelBtnActionPerformed();
             }
          });
          JPanel holdingPanel = new JPanel(new GridBagLayout());
          holdingPanel.setPreferredSize(new Dimension(200, 150));
          holdingPanel.add(nextPanelBtn);
          
          setLayout(new BorderLayout());
          add(new JLabel(thisPanelName, SwingConstants.CENTER), BorderLayout.NORTH);
          add(holdingPanel, BorderLayout.CENTER);
       }
    
       private void nextPanelBtnActionPerformed() {
          mainPanel.swapPanel(nextPanelName);
       }
       
    }

  5. #5
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    I might be wrong. I'm still new to Java.

    The reason why I did it this way is that if my program has lots of pages, that would represent lots of memory space occupied by unused pages.

    Am I right?
    Newb today, Pro tomorrow,
    Soulmed

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  7. #7
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    #1. You can create panel from class1 then create a method in class2 that has a parameter of jpanel then add that to the class2's jframe

Similar Threads

  1. Accessing Properties File
    By java_men in forum Java Servlet
    Replies: 0
    Last Post: 05-11-2010, 12:34 PM
  2. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  3. Accessing file
    By Doctor Cactus in forum New To Java
    Replies: 1
    Last Post: 10-24-2008, 08:31 PM
  4. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 09:15 PM

Posting Permissions

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