你好吗? 我的问题很简单,但是我还没有找到解决方案。 有没有办法绑定组件的可见性?
我知道有一种方法可以绑定为“可编辑”但不可见。
这是一个简单的例子:
public class Test extends javax.swing.JFrame {
/**
* Creates new form Test
*/
public Test() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
master = new javax.swing.JCheckBox();
disapear = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMaximumSize(new java.awt.Dimension(505, 333));
setMinimumSize(new java.awt.Dimension(505, 333));
getContentPane().setLayout(null);
master.setText("jCheckBox1");
getContentPane().add(master);
master.setBounds(34, 80, 93, 25);
disapear.setText("following");
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, master, org.jdesktop.beansbinding.ELProperty.create("${selected}"), disapear, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
getContentPane().add(disapear);
disapear.setBounds(200, 80, 69, 22);
bindingGroup.bind();
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField disapear;
private javax.swing.JCheckBox master;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
}
所以,而不是
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, master, org.jdesktop.beansbinding.ELProperty.create("${selected}"), disapear, org.jdesktop.beansbinding.BeanProperty.create("enabled"));//<<<<--------
bindingGroup.addBinding(binding);
类似
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, master, org.jdesktop.beansbinding.ELProperty.create("${selected}"), disapear, org.jdesktop.beansbinding.BeanProperty.create("visible")); //<<<<--------
bindingGroup.addBinding(binding);
我知道还有其他方法可以做到,但是我正在学习这个新工具,感觉比听众和事件更有趣