Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-13-2008, 06:56 AM
Member
 
Join Date: Feb 2008
Posts: 1
love2java is on a distinguished road
Dynamically create a button, but what happened?
I am new to Swing GUI, now I have a button1, and I want to create a new button in a panel named centerPanel when I click button1, so my code like this:

Code:
public class TestButton extends JFrame { public static void main(String args[]) { try { TestButton frame = new TestButton(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public TestButton() { super(); setBounds(100, 100, 500, 375); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel center = new JPanel(); getContentPane().add(center, BorderLayout.CENTER); final JPanel south = new JPanel(); getContentPane().add(south, BorderLayout.SOUTH); final JButton button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent arg0) { JButton newbutton = new JButton("Heeeeeeeee"); center.add(newbutton); center.repaint(); } }); button.setText("Button1"); south.add(button); } }
the program can be launched, but when I click the button1, the new button does not be created, it's not appear in center panel. I try my best to find out the reason but failed, please give me a hand. Your answers are appreciated much.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-17-2008, 01:01 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
center.add(newbutton); // After adding/removing/changing a component you must // tell its parent container to do a new layout. We do // this with the JComponent method revalidate or // with the older Container method validate. center.revalidate(); // center.repaint();
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create object dynamically with class name known in string format ranu_gokhe Advanced Java 1 04-09-2008 03:15 AM
Create, compile and call a Java source dynamically Java Tip Java Tips 0 02-17-2008 09:57 AM
How to create widgets dynamically sarbuland Advanced Java 0 02-06-2008 09:08 PM
dynamically updating clock malakaherath New To Java 2 08-01-2007 08:57 PM


All times are GMT +3. The time now is 04:28 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org