Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create jLabels when a button is clicked

 
Sachini Wickramaratne
Greenhorn
Posts: 3
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your code used to add the label?
 
Sachini Wickramaratne
Greenhorn
Posts: 3
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im using netbeans and the jpanel was dragged and dropped
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic