Results 1 to 3 of 3
  1. #1
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Dynamically adding JLabel to my GUI

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    
    
    public class a62JButton {
    
       /**
        * @param args
        */
       public static void main(String[] args) {
          GUI go = new GUI();
          go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          go.setSize(300, 300);
          go.setVisible(true);
       }
    
    }
    
    class Generate {
       
      Random gen = new Random(); 
      private int number;
     
      public int getNumber() {
         number = gen.nextInt(6);
         return number;
      }
      
    }
    
    class GUI extends JFrame {
       
       private JButton reg;
       private JButton cus;
       private JTextField tField;
       private JPasswordField pField;
       
       GUI() {
          super("The title");
          setLayout(new FlowLayout());
          
          reg = new JButton("Regular");
          add(reg);
          
        //  Icon b = new ImageIcon(getClass().getResource("a.png"));
          cus = new JButton("Custom");
         // cus.setRolloverIcon(null);
          add(cus);
          
          tField = new JTextField("Input");
          tField.setEditable(true);
          add(tField);
          
          
          HandlerClass handler = new HandlerClass();
          reg.addActionListener(handler);
          cus.addActionListener(handler);
       }
       
       private class HandlerClass implements ActionListener {
        //  ArrayList<JLabel> al = new ArrayList<JLabel>();
          Generate obj = new Generate();
          private int muutuja;
          
          public void actionPerformed(ActionEvent e) {
             if(e.getSource()==reg) {
                muutuja = obj.getNumber();
                JOptionPane.showMessageDialog(null, "Generated random number "+ muutuja);
                  [B] add(new JLabel(Integer.toString(muutuja)));[/B]
             } else if(e.getSource()==cus) {
                if(tField.isEditable()) {
                   tField.setEditable(false);            
                } else {
                   tField.setEditable(true);
                }
             }
          }
          
       }
    }
    I made a program with GUI. When you run the program and click on the JButton, then it generates Random number, but how can I add another JLabel to GUI which displays the generated number while the program is running. I bolded the line which shows the problem line. I can't get added that line to in main class created go object which is a GUI.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Dynamically adding JLabel to my GUI

    call validate() after you add the JLabel

    Container.validate() (Java Platform SE 6)
    Last edited by al_Marshy_1981; 12-08-2011 at 09:23 PM.

  3. #3
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Dynamically adding JPanels to the JTabbedPane !!
    By Stephen Douglas in forum New To Java
    Replies: 1
    Last Post: 04-12-2010, 05:54 PM
  2. 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
  3. [SOLVED] Adding JLabel to JFrame
    By mlfatty in forum AWT / Swing
    Replies: 3
    Last Post: 03-04-2009, 11:33 PM
  4. Adding wav sound into a JLabel.
    By hitmen in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2009, 05:59 AM
  5. Adding and removing panels dynamically
    By kbyrne in forum AWT / Swing
    Replies: 1
    Last Post: 04-12-2008, 08:28 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
  •