• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about swing--please give me an oracle!!

 
Gerald Kou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duplicate post has been deleted.
Gerald, please try to avoid posting the same question twice. I am sure it was an accident.
Mark
 
Gerald Kou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can u use a tabbedPanel instead?

Regards.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic