Results 1 to 3 of 3
  1. #1
    garnachito is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Print JLabel with a button ?

    Hi all

    I need a little help here, so, I have this:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ejemplo extends JFrame implements ActionListener{
    	
    	JLabel myVar;
    	JButton boton1;
    	
    	public ejemplo(){
    		
    		setLayout(null);
    		
    		myVar = new JLabel("Hello");
    		myVar.setBounds(150, 100, 100, 30);
                    // I'm not adding this here.
    		
    		boton1=new JButton("Print");
    		boton1.setBounds(150, 150, 100, 30);
    		add(boton1);
    		boton1.addActionListener(this);
    	}
    	
    	
    	 public void actionPerformed(ActionEvent e){
    		 
    		    if (e.getSource ()==boton1){
    		    		add(myVar);
    		    }
    		 }
    	
    	
    	public static void main (String[] args){
    		
    		ejemplo ventana=new ejemplo();
    		ventana.setBounds(0,0,400,300);
    		ventana.setVisible(true);
    		ventana.setResizable(true);
    		
    	}
    	
    }
    As you can see, I only need to print the string "Hello" when I press the button.

    But the string doesn't shows until I resize a little the frame, how can I make the string appear by just pressing the button ???

    Thanks a lot !!!

  2. #2
    garnachito is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Print JLabel with a button ?

    Nevermind!

    I just figured out how to do it... I just added this.

    Java Code:
    		    if (e.getSource ()==boton1){
    		    		add(myVar);
    		    		myVar.repaint();                 // I just added this line and that's it.
    		    }
    Thanks anyway :)

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Print JLabel with a button ?

    Adding components after a JFrame has been set to visible is more complex than that - adding them to a visible component requires you to validate/paint. There are many solutions: a) add the JLabel before setting the JFrame to visible with its text to an empty string, then call set text in the actionPerformed b) call validate on the JFrame after adding the component c) set the visibility of the component to false d) many more....

    Edit: seems you found a solution, which falls into category d)

Similar Threads

  1. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  2. Replies: 1
    Last Post: 02-11-2012, 03:07 AM
  3. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 01:16 AM
  4. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •