Results 1 to 5 of 5
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default 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);
    		}
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    revalidate the JPanel after adding the JLabel to it. You will also need to repaint the JPanel at times, especially if removing JLabels

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Thank you.

    method revalidate() works.

    Java Code:
    	
    	@Override
    		public void actionPerformed(ActionEvent arg0) {
    			label = new JLabel("Name");
    			rightPanel.add(label);
    		//	rightPanel.repaint();
    			[B][COLOR="Blue"]rightPanel.revalidate();[/COLOR][/B]
    		}
    Yes, you are right, repaint() method would be required later for deleting labels.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have added button for removing labels. In actionListener I've used methods validate(), and repaint().

    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();
    		}
    	}
    }
    Fubarable, thank you very much for good suggestions.

Similar Threads

  1. combo box listener?
    By Prajin in forum AWT / Swing
    Replies: 1
    Last Post: 07-08-2010, 01:26 PM
  2. can we change label from action class?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2009, 03:12 PM
  3. Replies: 0
    Last Post: 07-11-2008, 05:39 PM
  4. how to add links (Action) to a combo box
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 08:04 AM
  5. Show Text Fields on combo Box selected value
    By smajidali26 in forum AWT / Swing
    Replies: 0
    Last Post: 11-29-2007, 10:28 AM

Posting Permissions

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