469,283 Members | 2,297 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

GUI - Change contentpane when button pressed

25
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!

Expand|Select|Wrap|Line Numbers
  1. public class ButtonDemo extends JFrame implements ActionListener
  2. {
  3.     public static final int WIDTH = 400;
  4.     public static final int HEIGHT = 300;
  5.     private JTextField nameField; 
  6.  
  7.     public ButtonDemo()
  8.     {
  9.         setSize(WIDTH, HEIGHT);
  10.         WindowDestroyer listener = new WindowDestroyer();
  11.         addWindowListener(listener);
  12.  
  13.         Container contentPane = getContentPane();
  14.         contentPane.setBackground(Color.WHITE);
  15.  
  16.         contentPane.setLayout(new FlowLayout());
  17.  
  18.         JButton customerButton = new JButton("Customer");
  19.         customerButton.addActionListener(this);
  20.         contentPane.add(customerButton); 
  21.     }
  22.  
  23.     public void actionPerformed(ActionEvent e)
  24.     {
  25.         String actionCommand = e.getActionCommand();
  26.         Container contentPane = getContentPane();
  27.  
  28.         if (actionCommand.equals("Customer"))
  29.         {
  30.             nameField = new JTextField("Hi everyone");
  31.             nameField.addActionListener(this);
  32.             contentPane.add(nameField);
  33.         }
  34.         else
  35.             System.out.println("Error in button interface.");
  36.     }
  37. }
Apr 29 '09 #1
5 6910
r035198x
13,262 8TB
Call pack() after adding stuff to the content pane.
Apr 30 '09 #2
milk242
25
@r035198x
Sorry for the noob question but how would I do that? What would I put in the parameter of the Window header?
Apr 30 '09 #3
r035198x
13,262 8TB
After
Expand|Select|Wrap|Line Numbers
  1. contentPane.add(nameField);
add
Expand|Select|Wrap|Line Numbers
  1. pack();
Apr 30 '09 #4
milk242
25
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.
Apr 30 '09 #5
r035198x
13,262 8TB
Better read up on Layout managers again then.Perhaps you could use CardLayout for this?
Apr 30 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Ben | last post: by
7 posts views Thread by Miguel Dias Moura | last post: by
6 posts views Thread by ruca | last post: by
19 posts views Thread by darrel | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.