473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7428
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

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

Similar topics

2
by: jb | last post by:
Hello, I need to know which button was pressed in the submit , i tried reading the vaule of submit it the validateDate function but it returns 'undefined' value ; I do this in asp all the time, Not...
2
by: Ben | last post by:
Hi. I have a button that change a number of images src's when I click a button. The src's are stored in an array and I just use document.src=pics to change the src of the image. However I want...
7
by: Miguel Dias Moura | last post by:
Hello, I am working in ASP.net / VB with Access Database. i have a Multipage in my web site with the Submit button as the last button. When the Submit button is pressed i want the form...
6
by: ruca | last post by:
Hi gurus, I have a imagebutton in my WebForm, and I want that when I click (mouse down) on her the imagebutton change image and when I "unclick" (mouse up) change to the original image. Basically...
3
by: Mel | last post by:
Hi, Is there any way I can force to change button background colour when toggle button is checked? Just like button change its background colour when mouseover in MS Visual Studio.NET. I hope I...
19
by: darrel | last post by:
On my vb.net page, I have 4 sets of inputs + form buttons. example: Search: (GO) Zip: (GO) County: (GO) County: (GO) The problem is if I go to the page, type in a zip code, and hit...
2
by: matthewr | last post by:
In Internet Explorer, for example, when you hit return in the address bar, the Go button is pressed. In my program, I have a toolstrip with a textbox and button. How do I ensure the button is...
3
by: noddy | last post by:
I have a button <input type="button" value="Get data"> When it is clicked a database is accessed and the data is eventually displayed on the screen. While the data is being retrieved the...
2
by: Max2006 | last post by:
Hi, I have a simple form with a button on it. After a button click postback, anytime user press F5 (browser page refresh), the button's OnClick event handler is called again without the button...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.