File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes about swing--please give me an oracle!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "about swing--please give me an oracle!!" Watch "about swing--please give me an oracle!!" New topic

about swing--please give me an oracle!!

Gerald Kou

Joined: Jun 03, 2003
Posts: 14
I want to change compnnents of panel in rin time 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());
// infoPane.repaint();
// topFrame.repaint();
// topFrame.validate();
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();
please see valueChanged(),and give me a help.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11777

Hi Gerald,
Please don't post the same question twice. If you don't get an answer, you could try posting your own reply to your question asking people nicely if they have any ideas.
Moderators: would you like to close and/or delete this thread.
Also, could you please try and use the UBB codes which indicate that source code should appear as source code ... it will make your post much easier to read.
Now for an answer. Try this code:

Does this do what you want?
Note: I have completely replaced your calls where you creating a new JScrollPane each time. In my version, I just change what the view is.
Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Duplicate post has been deleted.
Gerald, please try to avoid posting the same question twice. I am sure it was an accident.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Gerald Kou

Joined: Jun 03, 2003
Posts: 14
OK! Thanks Andrew !
As you kown. Twice Post isn't my mind.but I wait a whole day for the I post twice to attract attention. I think this case don't occur.
George Ren
Ranch Hand

Joined: Aug 17, 2002
Posts: 58
Can u use a tabbedPanel instead?


Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Originally posted by George Ren:
Can u use a tabbedPanel instead?


Yes, but why?
I think your GUI can be simple and laid out on one Panel. Now there might be a Panel inside of this Panel, but it is still one screen.
But that is just my opinion, there is no right answer really. Just don't ovre complicate things for the user.
I agree. Here's the link:
subject: about swing--please give me an oracle!!
It's not a secret anymore!