Results 1 to 6 of 6
- 12-21-2009, 04:15 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
Populating CardLayout JPanels with External classes
Can someone let me know if you can populate a JPanel from an external class. I am using a main JFrame with a cardlayout full of JPanels. I want to add all of the buttons and populate the entire panel from an external class. In other words instead of using
JPanel card2 = new JPanel();
and initiating all of the components I would like use
LogonPanel card2 = new LogonPanel() where "LogonPanel" is a seperate class(object) that extends JPanel and all of the components are added.
Java Code:public class Nascar implements ActionListener { JPanel cards; final static String SIGNIN="Sign in"; final static String EXIT="Exit"; final static String FORGOTPASSWORD="Forgot your password?"; Icon logo = new ImageIcon ("logo.//gif"); public void addComponentToPane(Container pane) { JPanel card1 = new JPanel(); JButton signIn = new JButton(SIGNIN, logo); signIn.setActionCommand(EXIT); signIn.addActionListener(this); card1.add(signIn); JButton forgotPassword = new JButton(FORGOTPASSWORD); forgotPassword.setActionCommand(EXIT); forgotPassword.addActionListener(this); card1.add(forgotPassword); LogonScreen card2 = new LogonScreen(); cards=new JPanel(new CardLayout()); cards.add(card1, SIGNIN); cards.add(card2, EXIT); pane.add(cards, BorderLayout.CENTER); } public void actionPerformed(ActionEvent event) { CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, (String)event.getActionCommand()); } private static void createAndShowGUI() { JFrame frame = new JFrame ("Nascar Challenge"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setIconImage(new ImageIcon("logo1.gif").getImage()); frame.setSize(750, 750); frame.setLocationRelativeTo(null); Nascar nascar = new Nascar(); nascar.addComponentToPane(frame.getContentPane()); //frame.pack(); frame.setVisible(true); } public static void main(String [] args) { createAndShowGUI(); } }
Java Code:public class LogonScreen extends JPanel { public LogonScreen() { JPanel logon = new JPanel(); setLayout(new BorderLayout()); JButton signIn = new JButton("Sign In"); logon.add(signIn, BorderLayout.SOUTH); } }
-
The short answer is: yes, no it can be done no problem.
So, what problems are you having when you try this?
- 12-21-2009, 05:22 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
When I click the forgotPassword button it just gives me the empty JPanel. It creates the object but the button that was added in the LogonScreen object does not appear. I have tried revalidate, and repaint but no luck.
-
This has nothing to do with CardLayout or using separate classes and everything to do with the fact that you add nothing to the LogonScreen JPanel. Instead you add a JButton to a separate and distinct JPanel called "logon" and then do nothing with this JPanel.
So if you add your JButton to the actual panel, you'll see it:
Java Code:class LogonScreen extends JPanel { public LogonScreen() { //JPanel logon = new JPanel(); setLayout(new BorderLayout()); JButton signIn = new JButton("Sign In"); //logon.add(signIn, BorderLayout.SOUTH); add(signIn, BorderLayout.SOUTH); } }
- 12-21-2009, 06:01 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
Thanks so much, it makes perfect sense now.
-
You're quite welcome. Much luck and happy coding!
Similar Threads
-
Populating a JTable
By toymachiner62 in forum New To JavaReplies: 2Last Post: 10-13-2009, 05:56 AM -
Unexpected behavior Thread.sleep() is used within CardLayout
By mityay in forum AWT / SwingReplies: 3Last Post: 06-28-2009, 07:18 PM -
load external classes
By gotenks05 in forum New To JavaReplies: 17Last Post: 10-27-2008, 06:09 PM -
Regarding CardLayout
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-07-2008, 07:52 PM -
Using previous with CardLayout
By uncopywritable in forum New To JavaReplies: 2Last Post: 08-05-2007, 09:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks