Results 1 to 7 of 7
- 04-20-2011, 09:20 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
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
page1.javaJava 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); } }
page2.javaJava 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(); } }
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
-
Please see similar active threads that address this. For instance: JFrame and CardLayout -- Wrong Parent
- 04-21-2011, 02:42 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Answer to my own question...
For those out there with the same problem reading this thread, here's my working code:
test.java
page1.javaJava 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(); } }
page2.javaJava 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; } }
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
-
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); } }
- 04-22-2011, 01:49 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
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
-
Your way creates more objects and wastes more space.
- 04-22-2011, 03:36 PM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
Similar Threads
-
Accessing Properties File
By java_men in forum Java ServletReplies: 0Last Post: 05-11-2010, 11:34 AM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Accessing file
By Doctor Cactus in forum New To JavaReplies: 1Last Post: 10-24-2008, 07:31 PM -
Accessing a file randomly
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 08:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks