GUI - Change contentpane when button pressed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milk242
    New Member
    • Feb 2009
    • 25

    GUI - Change contentpane when button pressed

    Hi everyone

    I'm trying to change what shows up in the contentpane depending on what button you press. Say you press a button labeled "Customer", it would add textfields to the content pane.

    I thought I could just add the action int eh actionPerformed method but doesn't seem to work.

    Thanks for your help!

    Code:
    public class ButtonDemo extends JFrame implements ActionListener
    {
    	public static final int WIDTH = 400;
    	public static final int HEIGHT = 300;
    	private JTextField nameField; 
    	
    	public ButtonDemo()
    	{
    		setSize(WIDTH, HEIGHT);
    		WindowDestroyer listener = new WindowDestroyer();
    		addWindowListener(listener);
    		
    		Container contentPane = getContentPane();
    		contentPane.setBackground(Color.WHITE);
    		
    		contentPane.setLayout(new FlowLayout());
    		
    		JButton customerButton = new JButton("Customer");
    		customerButton.addActionListener(this);
    		contentPane.add(customerButton); 
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		String actionCommand = e.getActionCommand();
    		Container contentPane = getContentPane();
    		
    		if (actionCommand.equals("Customer"))
    		{
    			nameField = new JTextField("Hi everyone");
    			nameField.addActionListener(this);
    			contentPane.add(nameField);
    		}
    		else
    			System.out.println("Error in button interface.");
    	}
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Call pack() after adding stuff to the content pane.

    Comment

    • milk242
      New Member
      • Feb 2009
      • 25

      #3
      Originally posted by r035198x
      Call pack() after adding stuff to the content pane.
      Sorry for the noob question but how would I do that? What would I put in the parameter of the Window header?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        After
        Code:
        contentPane.add(nameField);
        add
        Code:
        pack();

        Comment

        • milk242
          New Member
          • Feb 2009
          • 25

          #5
          Thanks for the help!
          but...

          Do you know where I can read up more of the pack method? It sort of does what I want but I'll have more than 1 button on the window, each button should populate the content pane with new fields. The pack() method displays the field and when I press the button again, it'll duplicate the fields. Also it breaks the set size of the window.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Better read up on Layout managers again then.Perhaps you could use CardLayout for this?

            Comment

            Working...