Results 1 to 6 of 6
  1. #1
    fred2482 is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

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

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

  3. #3
    fred2482 is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    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.

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

    Default

    Quote Originally Posted by fred2482 View Post
    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);
       }
    }

  5. #5
    fred2482 is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks so much, it makes perfect sense now.

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

Similar Threads

  1. Populating a JTable
    By toymachiner62 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 05:56 AM
  2. Replies: 3
    Last Post: 06-28-2009, 07:18 PM
  3. load external classes
    By gotenks05 in forum New To Java
    Replies: 17
    Last Post: 10-27-2008, 06:09 PM
  4. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 PM
  5. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 09:43 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
  •