aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Create jLabels when a button is clicked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Create jLabels when a button is clicked" Watch "Create jLabels when a button is clicked" New topic
Author

Create jLabels when a button is clicked

Sachini Wickramaratne
Greenhorn

Joined: Nov 04, 2012
Posts: 3

Hi, im in trouble with creating jLabels when a button is clicked. Im working with Netbeans and i need to create and display a jLabel,everytime a "ADD" button is clicked. ive ried Lumps of code but nothing gets me there. i dont see any label in my jframe


~$oFtY~
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 484

What's your code used to add the label?


PM Me If You Want to get Urgent Help on Java Programming
Sachini Wickramaratne
Greenhorn

Joined: Nov 04, 2012
Posts: 3

I tried a lot of code and ive commented them here..



private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

JLabel label= new JLabel();


//label.setText("Bottom Right");
//label.setHorizontalAlignment(JLabel.RIGHT);
//label.setVerticalAlignment(JLabel.BOTTOM);
jPanel1.add(label);
//this.label.
//this.repaint();
// setVisible(true);
//labels();
// TODO add your handling code here:
}
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 484

so the problem may be due to the layout manager of the panel. Which layout manager are you using for the JPanel?
Sachini Wickramaratne
Greenhorn

Joined: Nov 04, 2012
Posts: 3

Im using netbeans and the jpanel was dragged and dropped
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 484

Sachini Wickramaratne wrote:Im using netbeans and the jpanel was dragged and dropped


That's the problem. I guess you haven't got enough Swing stuff before using NetBeans, so you are stuck now.

I can help you to fix the problem if you post the full code here. But anyway, you need to prepare yourself with Swing knowledge before using a GUI designer like NetBeans.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Yet another reason why DnD style IDE/tools should be avoided like a plague.
On a more conceptual level, whenever you add/remove components to an existing (visible) UI, you need to call a revalidate() and repaint().


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create jLabels when a button is clicked
 
Similar Threads
removing one component and replacing it with another
How do you "delete" a JLabel before adding a JLabel on top?
getting which of the JLabels get clicked?
radio button textfield
JList set element visible