Results 1 to 5 of 5
- 08-11-2010, 10:59 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Show label in panel, combo box, action listener
Hi I have written small program. It has combo box. When I choose one item from combo box it should be write something on new label on Panel.
My program writes it on new Label, but it is not visible until I change size of Frame (and panel as well :D).
Is there any suggestion about what I'm doing wrong?
Java Code:import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JComboBox; public class ComboListenExample { public static void main(String[] args) { ComboFrame frame = new ComboFrame(); frame.shwUI(); } } class ComboFrame extends JFrame { JPanel rightPanel; JComboBox combo; JLabel label; String[] names = { "Mike", "Jack", "Steven", "Peter", "John" }; public ComboFrame() { rightPanel = new JPanel(); getContentPane().add(rightPanel, BorderLayout.EAST); label = new JLabel("Hello"); rightPanel.add(label); rightPanel.setLayout(new GridLayout(5,0)); combo = new JComboBox(names); getContentPane().add(combo, BorderLayout.NORTH); combo.addActionListener(new MyListen()); } public void shwUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(new Dimension(400,400)); setLocationRelativeTo(null); setVisible(true); } private class MyListen implements ActionListener { JLabel label; @Override public void actionPerformed(ActionEvent arg0) { label = new JLabel("Name"); rightPanel.add(label); } } }
-
revalidate the JPanel after adding the JLabel to it. You will also need to repaint the JPanel at times, especially if removing JLabels
- 08-11-2010, 12:29 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Thank you.
method revalidate() works.
Yes, you are right, repaint() method would be required later for deleting labels.Java Code:@Override public void actionPerformed(ActionEvent arg0) { label = new JLabel("Name"); rightPanel.add(label); // rightPanel.repaint(); [B][COLOR="Blue"]rightPanel.revalidate();[/COLOR][/B] }
-
Often you'll want to place the JLabel in your GUI to start with and simply set its text on some event. If you do this, then you won't need to repaint or revalidate or remove.
- 08-11-2010, 12:47 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
I have added button for removing labels. In actionListener I've used methods validate(), and repaint().
Fubarable, thank you very much for good suggestions.Java Code:import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JComboBox; public class ComboListenExample { public static void main(String[] args) { ComboFrame frame = new ComboFrame(); frame.shwUI(); } } class ComboFrame extends JFrame { JButton button; JPanel rightPanel; JComboBox combo; JLabel label; String[] names = { "Mike", "Jack", "Steven", "Peter", "John" }; public ComboFrame() { button = new JButton("remove all"); getContentPane().add(button, BorderLayout.SOUTH); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { rightPanel.removeAll(); rightPanel.validate(); rightPanel.repaint(); } }); rightPanel = new JPanel(); getContentPane().add(rightPanel, BorderLayout.EAST); label = new JLabel("Hello"); rightPanel.add(label); rightPanel.setLayout(new GridLayout(5,0)); combo = new JComboBox(names); getContentPane().add(combo, BorderLayout.NORTH); combo.addActionListener(new MyListen()); } public void shwUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(new Dimension(400,400)); setLocationRelativeTo(null); setVisible(true); } private class MyListen implements ActionListener { JLabel label; @Override public void actionPerformed(ActionEvent arg0) { label = new JLabel("Name"); rightPanel.add(label); // rightPanel.repaint(); rightPanel.revalidate(); } } }
Similar Threads
-
combo box listener?
By Prajin in forum AWT / SwingReplies: 1Last Post: 07-08-2010, 12:26 PM -
can we change label from action class?
By kishan in forum Advanced JavaReplies: 2Last Post: 04-28-2009, 02:12 PM -
How to resize the Label when the Shell resizes using a Listener mechanism
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:39 PM -
how to add links (Action) to a combo box
By impact in forum New To JavaReplies: 2Last Post: 05-03-2008, 07:04 AM -
Show Text Fields on combo Box selected value
By smajidali26 in forum AWT / SwingReplies: 0Last Post: 11-29-2007, 09:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks