Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Add extended JPanel on selected index change of JComboBox

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Add extended JPanel on selected index change of JComboBox

    I am trying to add a JPanel to a JFrame when the type is selected from a JComboBox. It does something funny....it looks like nothing happens but as you click on where the panel should have been added, items start showing up, but not the label. However. I want the JPanel to popup in the frame when the correct type is selected from the dropdown.

    here is the code fro my main application and the JPanel class:
    import java.awt.Color;
    import java.awt.EventQueue;
     
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JComboBox;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JTextArea;
    import javax.swing.JButton;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import com.jgoodies.forms.factories.DefaultComponentFactory;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
     
    public class MainApp {
     
    	private JFrame frmIntegrationServiceApi;
    	private JTextField textField;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					MainApp window = new MainApp();
    					window.frmIntegrationServiceApi.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public MainApp() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
     
     
     
    	private void initialize() {
    		frmIntegrationServiceApi = new JFrame();
    		frmIntegrationServiceApi.setTitle("Integration Services API Test");
    		frmIntegrationServiceApi.setBounds(100, 100, 900, 590);
    		frmIntegrationServiceApi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frmIntegrationServiceApi.getContentPane().setLayout(null);
     
    		JLabel lblNewLabel = new JLabel("IS Host");
    		lblNewLabel.setBounds(20, 11, 42, 14);
    		frmIntegrationServiceApi.getContentPane().add(lblNewLabel);
     
    		textField = new JTextField();
    		textField.setBounds(82, 8, 238, 20);
    		frmIntegrationServiceApi.getContentPane().add(textField);
    		textField.setColumns(10);
     
    		JLabel lblInterface = new JLabel("Interface");
    		lblInterface.setBounds(20, 42, 56, 14);
    		frmIntegrationServiceApi.getContentPane().add(lblInterface);
     
     
     
    		final JComboBox comboBox = new JComboBox();
    		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select Interface", "IAgentManagment", "IContactManagement", "ISessionManagment"}));
    		comboBox.setBounds(82, 39, 238, 20);
    		frmIntegrationServiceApi.getContentPane().add(comboBox);
     
     
    		class MyItemListener implements ItemListener
    		{
     
    			@Override
    			public void itemStateChanged(ItemEvent arg0) {
    				if(comboBox.getSelectedIndex() == 1)
    				{
    					IAgent ia = new IAgent();
    					ia.setBounds(20, 70, 300, 425);
    					frmIntegrationServiceApi.getContentPane().add(ia);
    				}
     
    			}
     
    		}
    		MyItemListener actionListener = new MyItemListener();
    		comboBox.addItemListener(actionListener);
     
     
    		JLabel lblGeneratedUrl = new JLabel("Generated URL to Post to Integration Services");
    		lblGeneratedUrl.setBounds(389, 11, 288, 14);
    		frmIntegrationServiceApi.getContentPane().add(lblGeneratedUrl);
     
    		JTextArea textArea = new JTextArea();
    		textArea.setBounds(389, 25, 485, 46);
    		frmIntegrationServiceApi.getContentPane().add(textArea);
     
    		JLabel lblNewLabel_1 = new JLabel("Response from API in HTML");
    		lblNewLabel_1.setBounds(389, 82, 138, 14);
    		frmIntegrationServiceApi.getContentPane().add(lblNewLabel_1);
     
    		JTextArea textArea_1 = new JTextArea();
    		textArea_1.setBounds(389, 103, 485, 257);
    		frmIntegrationServiceApi.getContentPane().add(textArea_1);
     
    		JLabel lblNewLabel_2 = new JLabel("Response from API in Text");
    		lblNewLabel_2.setBounds(389, 371, 138, 14);
    		frmIntegrationServiceApi.getContentPane().add(lblNewLabel_2);
     
    		JTextArea textArea_2 = new JTextArea();
    		textArea_2.setBounds(389, 393, 485, 148);
    		frmIntegrationServiceApi.getContentPane().add(textArea_2);
     
    		JButton btnCreateUrl = new JButton("Create URL");
    		btnCreateUrl.setBounds(20, 518, 112, 23);
    		frmIntegrationServiceApi.getContentPane().add(btnCreateUrl);
     
    		JButton btnPost = new JButton("Post");
    		btnPost.setBounds(142, 518, 89, 23);
    		frmIntegrationServiceApi.getContentPane().add(btnPost);
     
    		final JPanel panel = new JPanel();
    		panel.setBounds(20, 67, 300, 425);
    		frmIntegrationServiceApi.getContentPane().add(panel);
     
     
    	}
    }


    import java.awt.Color;
     
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JComboBox;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JTextField;
     
     
    public class IAgent extends JPanel {
    	private JTextField textField;
    	private JTextField textField_1;
    	private JTextField textField_2;
    	private JTextField textField_3;
    	private JTextField textField_4;
     
    	/**
    	 * Create the panel.
    	 */
    	public IAgent() {
    		setLayout(null);
     
    		setBorder(BorderFactory.createLineBorder(Color.black, 1));
     
    		JLabel lblIagentmanagement = new JLabel("IAgentManagement");
    		lblIagentmanagement.setBounds(10, 11, 124, 14);
    		add(lblIagentmanagement);
     
    		JLabel lblNewLabel = new JLabel("Agent Event");
    		lblNewLabel.setBounds(10, 36, 65, 14);
    		add(lblNewLabel);
     
    		JComboBox comboBox = new JComboBox();
    		comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "Agent Logon", "Agent LogOff"}));
    		comboBox.setBounds(85, 33, 205, 20);
    		add(comboBox);
     
    		JLabel lblAgent = new JLabel("Agent");
    		lblAgent.setBounds(10, 87, 36, 14);
    		add(lblAgent);
     
    		textField = new JTextField();
    		textField.setBounds(85, 84, 205, 20);
    		add(textField);
    		textField.setColumns(10);
     
    		JLabel lblAgentDs = new JLabel("Agent DS");
    		lblAgentDs.setBounds(10, 132, 46, 14);
    		add(lblAgentDs);
     
    		textField_1 = new JTextField();
    		textField_1.setBounds(85, 129, 205, 20);
    		add(textField_1);
    		textField_1.setColumns(10);
     
    		JLabel lblDevice = new JLabel("Device");
    		lblDevice.setBounds(10, 178, 46, 14);
    		add(lblDevice);
     
    		textField_2 = new JTextField();
    		textField_2.setBounds(85, 175, 205, 20);
    		add(textField_2);
    		textField_2.setColumns(10);
     
    		JLabel lblDeviceDs = new JLabel("Device DS");
    		lblDeviceDs.setBounds(10, 229, 65, 14);
    		add(lblDeviceDs);
     
    		textField_3 = new JTextField();
    		textField_3.setBounds(85, 226, 205, 20);
    		add(textField_3);
    		textField_3.setColumns(10);
     
    		JLabel lblAgentSkill = new JLabel("Agent Skill");
    		lblAgentSkill.setBounds(10, 283, 65, 14);
    		add(lblAgentSkill);
     
    		textField_4 = new JTextField();
    		textField_4.setBounds(85, 280, 205, 20);
    		add(textField_4);
    		textField_4.setColumns(10);
     
    	}
     
    }
    Thanks in Advance!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Add extended JPanel on selected index change of JComboBox

    That's a bit too much code to wade through, but you can't just add components to an already visible container. You have to revalidate afterwards. Google and the API are your friend there. You might also look into using a CardLayout.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Add extended JPanel on selected index change of JComboBox

    hey, you helped me more than you know! what you said about not being able to add components got me thinking. I figured it out I add the elements in an absolute layout and set the same bounds, then just toggled their visibility based on the selected index change. I feel a little silly that I couldn't arrive at that conclusion on my own, but I appreciate your reponse because it lead me to the solution.
                     /////////////////////////////////////////////////////////////////////////////////////////////
    		//event listener for Selected Item index change to detect which extended JPanel is visible//
    		////////////////////////////////////////////////////////////////////////////////////////////
    		class MyItemListener implements ItemListener
    		{
     
    			@Override
    			public void itemStateChanged(ItemEvent arg0) {
    				if(comboBox.getSelectedIndex() == 1)
    				{
    					ia.setVisible(true);
    					ic.setVisible(false);
    					is.setVisible(false);
    					IAShown = true;
    					ICShown = false;
    					ISShown = false;
     
    				}
    				else if(comboBox.getSelectedIndex() == 2)
    				{
    					ia.setVisible(false);
    					ic.setVisible(true);
    					is.setVisible(false);
    					IAShown = false;
    					ICShown = true;
    					ISShown = false;
    				}
    				else if(comboBox.getSelectedIndex() == 3)
    				{
    					ia.setVisible(false);
    					ic.setVisible(false);
    					is.setVisible(true);
    					IAShown = false;
    					ICShown = false;
    					ISShown = true;
    				}
    				else
    				{
    					ia.setVisible(false);
    					ic.setVisible(false);
    					is.setVisible(false);
    					IAShown = false;
    					ICShown = false;
    					ISShown = false;
    				}
     
    			}
     
    		}//end of action listener

    ia, ic, and is are composite JPanels with different form structures.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Add extended JPanel on selected index change of JComboBox

    That definitely sounds like a job for a CardLayout then.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. extended JPanel is empty
    By pottsiex5 in forum AWT / Java Swing
    Replies: 4
    Last Post: November 26th, 2011, 03:59 PM
  2. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 03:22 AM
  3. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  4. Imitating a JFrame extended program with JPanel; help needed...
    By emigrant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 02:30 PM
  5. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM