This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

about swing--please give me an oracle!!

Gerald Kou
Greenhorn

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);
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();
}
}
please see valueChanged(),and give me a help.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gerald Kou
Greenhorn

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 reply.so 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?

Regards.


THINK!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Regards.

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.
Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: about swing--please give me an oracle!!
 
Similar Threads
JList deleting multiple items
my gui question--how to change panel dynamically
NullPointerException
array out of bounds and adding double contact
Error at addListSelectionListener(this)