welshspoon
Technical User
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.
Thanks in advance for any help
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