我最近进入了java世界并开始了一个个人项目,但作为Java的新手,我在Java的Swing JTree组件中遇到了一个问题。
我的问题发生在“LoadingObserver”中的一个大型代码项目(https://github.com/adrian-tilita/jTreeHelper)中,所以,总结一下,我试图在3个文件中的一堆基本代码(无包)中复制问题:
主要
public class Main {
public static void main(String[] args) {
View.main(args);
}
}
事件处理程序
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.ExpandVetoException;
public class EventHandler implements TreeWillExpandListener, TreeExpansionListener {
private javax.swing.JLabel label;
public void setLabel(javax.swing.JLabel label) {
this.label = label;
}
@Override
public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
this.label.setText("Loading...");
try {
Thread.sleep(1000);
} catch (Exception e) {
// do nothing
}
}
@Override
public void treeExpanded(TreeExpansionEvent event) {
this.label.setText("Loaded");
}
@Override
public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {}
@Override
public void treeCollapsed(TreeExpansionEvent event) {}
}
查看
public class View extends javax.swing.JFrame {
public View() {
initComponents();
EventHandler handler = new EventHandler();
handler.setLabel(jLabel1);
this.jTree1.addTreeWillExpandListener(handler);
this.jTree1.addTreeExpansionListener(handler);
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jTree1);
jLabel1.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 233, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addContainerGap(15, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new View().setVisible(true);
}
});
}
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
}
视图只是一个生成的netbeans表单,构造函数有几行。
当我尝试更新swing组件(加载)时,我遇到的问题是关于“treeWillExpand”事件,但在我的情况下,我只得到“加载”文本。
我做错了什么或事件不是我想要的事件?
主要是,我的问题是:如何在“TreeWillExpand”事件中更新Swing组件?
PS:温柔,这是我第一次遇到java。