触发addMouseListener事件后Java GUI未完全加载
我有以下代码来跟踪用户在表中选择的内容,在用户选择聊天对话后,我想隐藏包含该表的触发addMouseListener事件后Java GUI未完全加载,java,swing,jtable,jpanel,mouse-listeners,Java,Swing,Jtable,Jpanel,Mouse Listeners,我有以下代码来跟踪用户在表中选择的内容,在用户选择聊天对话后,我想隐藏包含该表的JPanel,并显示包含聊天对话的JPanel。请参阅我当前的代码以执行此操作: table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { JT
JPanel
,并显示包含聊天对话的JPanel
。请参阅我当前的代码以执行此操作:
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTable target = (JTable) e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
// loop through all elements in the table
for (int i = 0; i < listmodel.size(); i++) {
// get the table item associated with the current element
final Object object = listmodel.get(i);
if (object instanceof ListItem) {
ListItem listitem = (ListItem) object;
if (table.getValueAt(table.getSelectedRow(), 0).toString() == listitem.getText()) {
// Show the chat conversation (this is where the GUI for some reason does not fully load)
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pnlMainTableWrapper.setVisible(false); // Contains the table
pnlChatMsgs.setVisible(true);
pnlSendMsg.setVisible(true);
pnlRight.setVisible(true);
pnlChatMsgs.validate();
pnlChatMsgs.repaint();
}
});
}
}
}
}
}
});
table.addMouseListener(新的MouseAdapter(){
公共无效mouseClicked(MouseEvent e){
如果(如getClickCount()==2){
JTable target=(JTable)e.getSource();
int row=target.getSelectedRow();
int column=target.getSelectedColumn();
//循环遍历表中的所有元素
对于(int i=0;i
出于某种奇怪的原因,JPanel
pnlChatMsgs
中的所有GUI组件都没有加载,这些组件只是白色的
你知道是什么导致了这种行为吗 每当我看到代码试图在同一个位置使用两个组件时,最好使用,并让it经理在任何时候都可以看到哪个组件 如果您试图自己管理它,那么在设计时代码应该是这样的:
JPanel parent = new JPanel();
JPanel child1 = new JPanel();
JPanel child2 = new JPanel();
child2.setVisible(false);
parent.add( child1 );
parent.add( child2 );
然后在运行时交换面板时,您将执行以下操作:
child1.setVisible(false);
child2.setVisible(true);
parent.revalidate();
parent.repaint();
我仍然推荐卡片布局,这样你就不会重新发明轮子了
此外,invokeLater()可能不是必需的,因为所有Swing事件代码都已在EDT上执行。您应该在
done
方法中处理此问题吗?@Pranalee您对done
方法的意思是什么?使用jpopmenu不容易我认为您正在使用Swing,Swing不是线程安全的,试着看看JavaAPI文档,他们在其中解决了这个线程问题。