473,587 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GUI - Change contentpane when button pressed

25 New Member
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 7442
r035198x
13,262 MVP
Call pack() after adding stuff to the content pane.
Apr 30 '09 #2
milk242
25 New Member
@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 MVP
After
Expand|Select|Wrap|Line Numbers
  1. contentPane.add(nameField);
add
Expand|Select|Wrap|Line Numbers
  1. pack();
Apr 30 '09 #4
milk242
25 New Member
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 MVP
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
41043
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 sure how to do it in javascript <form name="form1" method="post" action="myNewplace.asp" ONSUBMIT="return ValidateData();"> <input...
2
9527
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 the button to change the link as well to the image that is displayed but i cannot get it to work. How do I do this. Could someone tell me or point...
7
1829
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 values to be added to a database and sent by email. 1. I have the code which submits the form values to the database. (See at
6
22117
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 I want to know how can I have the mousedown and mouseup buttons events. I think that I have to do this in JavaScript. Can you help me on this?
3
10616
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 can get my question answered. Thank you very much.
19
2172
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 enter.
2
13483
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 'clicked' when Enter is pressed in the text box. I can't set the form's AcceptButton property to the toolStripButton - it doesn't allow...
3
1748
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 button is displayed in its "pressed in" state. When the data arrives and is displayed the button pops out to its normal "unpressed" state.
2
4978
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 actually be pressed! Is this a bug in ASP.NET? Is there any technique to prevent that? I know that a Response.Redirect to the same page solves...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.