Есть ли способ связать видимость компонента? - PullRequest
0 голосов
/ 26 января 2019

как дела? Мой вопрос довольно прост, но я пока не нашел решения. Есть ли способ связать видимость компонента?

Я знаю, что есть способ привязки как «редактируемый», но не видимый.

Вот простой пример:

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);

Я знаю, что есть и другие способы сделать это, но я изучаю этот новый инструмент и чувствую себя более интересным, чем слушатели и события

...