Win a copy of The Business Blockchain this week in the Cloud forum!
    Bookmark Topic Watch Topic
  • New Topic

my gui question--how to change panel dynamically

 
Gerald Kou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Report post to moderator
I want to change content in panel when user click different items of list ,but I can't realize it. I process the event in valueChanged(),I used ervey approach such as repaint(),validate(),invalidate().Nevertheless there is effect by those.
Please see my code,and point out where are the errors.Thanks!
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class test_changeview implements ListSelectionListener {
JFrame topFrame = null;
JList itemList = null;
JLabel statuslabel = null;
JPanel mainPane = null;
JScrollPane infoPane = null;
public test_changeview() {
topFrame = new JFrame("hello!");
topFrame.setBounds(100, 100, 630, 400);
Container contentPane = topFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel tittlelabel = new JLabel("System", JLabel.CENTER);
statuslabel = new JLabel("hello");
mainPane = new JPanel();
mainPane.setLayout(new BorderLayout());
String[] item = { "item1", "item2" };
itemList = new JList(item);
itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
itemList.setSelectedIndex(0);
itemList.addListSelectionListener(this);
JPanel pane = new JPanel();
pane.add(new JButton("item1"));
infoPane = new JScrollPane(pane);
mainPane.add(itemList, BorderLayout.WEST);
mainPane.add(infoPane, BorderLayout.CENTER);
contentPane.add(tittlelabel, BorderLayout.NORTH);
contentPane.add(mainPane, BorderLayout.CENTER);
contentPane.add(statuslabel, BorderLayout.SOUTH);

topFrame.setVisible(true);
topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void valueChanged(ListSelectionEvent e) {
System.out.println("ListSelectionEvent: ");
if (e.getValueIsAdjusting())
return;
int index = itemList.getSelectedIndex();
statuslabel.setText("index :" + index);
if (index == 0) {

}
if (index == 1) {
infoPane = new JScrollPane(panelV2());
//infoPane.repaint();
//topFrame.repaint();
//topFrame.validate();
topFrame.invalidate();
}
}

private JPanel panelV2() {
JPanel pane = new JPanel();
pane.add(new JButton("item2"));
return pane;
}

public static void main(String[] args) throws Exception {
new test_changeview();
}
}
 
    Bookmark Topic Watch Topic
  • New Topic