Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with JList and JPanels

Status
Not open for further replies.

welshspoon

Technical User
Dec 14, 2002
34
GB
Hiya

My problem is that I want to be able to update what is being displayed on a JPanel when an option is selected from a JList. Each item on the JList when selected adds a JPanel to the JPanel. When I run the code and select an item the JPanel appears correctly, but when I select a second from the list the second JPanel is added behind the first selected. Also, when I have managed to get the correct panel to display if i highlight over it the first displayed panel appears.

Code:
public TSettings() {
        setTitle("Learning Zone");
        setClosable(true);
        setMaximizable(true);
        setIconifiable(true);
        topPanelf = new JPanel();
        screen = new JPanel();
        topPanelf.setLayout( new BorderLayout() );
        screen.setLayout( new BorderLayout() );
        getContentPane().add( topPanelf, BorderLayout.CENTER);

        // sets propoerties of the JEditorPane for it to view web pages.
        String [] options = {"Add a User", "Remove User", "Edit User", "Add a Question", "Remove a Question", "Add a Test", "Remove a Test", "Set a Test", "View Test Marks", "Print Test", "Change Your Password"};
        topiclist = new JList(options);
        topiclist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        topiclist.addListSelectionListener(new
        ListSelectionListener() {
            public void valueChanged(ListSelectionEvent event) {
                try {
					Object cvalue = topiclist.getSelectedValue();
					String cvaluestring = cvalue.toString();

					if (cvaluestring == ("Add a User")) {
						AddUser1 auf = new AddUser1();
						screen.add(auf);
						invalidate();
						repaint();

					} else if (cvaluestring == ("Remove User")) {
						RemoveUser ru = new RemoveUser();
						screen.add(ru);
						invalidate();
						repaint();

						}
						else if (cvaluestring == ("Edit User")) {
							EditUser eu = new EditUser();
							screen.add(eu);
							validate();

							}

						else if (cvaluestring == ("Add a Question")) {
							AddQuestion aqf = new AddQuestion();
							screen.add(aqf);
							validate();
						}
                }
                catch( Exception e ) {
                    JOptionPane.showMessageDialog(TSettings.this,
                    e, "ERROR", JOptionPane.ERROR_MESSAGE);
                }

            }
        });

Thanks in advance for any help
 
What about adding all of them when initalizing an making the correct one visible on selection?

Cheers,
Dian
 
Thanks for your reply

No that didn't work, the panels display but i can still get back to the first displayed by selecting items on the panel. I had this problem before but i can't remember how i fixed it.

Thanks again for any help given
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top