Results 1 to 3 of 3
- 12-08-2011, 09:05 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Dynamically adding JLabel to my GUI
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.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); } } } } }
- 12-08-2011, 09:18 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 12-08-2011, 09:37 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Dynamically adding JPanels to the JTabbedPane !!
By Stephen Douglas in forum New To JavaReplies: 1Last Post: 04-12-2010, 05:54 PM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
[SOLVED] Adding JLabel to JFrame
By mlfatty in forum AWT / SwingReplies: 3Last Post: 03-04-2009, 11:33 PM -
Adding wav sound into a JLabel.
By hitmen in forum AWT / SwingReplies: 2Last Post: 02-17-2009, 05:59 AM -
Adding and removing panels dynamically
By kbyrne in forum AWT / SwingReplies: 1Last Post: 04-12-2008, 08:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks