Bookmark Topic Watch Topic
  • New Topic

my gui question--how to change panel dynamically  RSS feed

Gerald Kou
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);
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);

public void valueChanged(ListSelectionEvent e) {
System.out.println("ListSelectionEvent: ");
if (e.getValueIsAdjusting())
int index = itemList.getSelectedIndex();
statuslabel.setText("index :" + index);
if (index == 0) {

if (index == 1) {
infoPane = new JScrollPane(panelV2());

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
Boost this thread!