2011-09-14 20 views
6

Ho tentato di associare un JList a una proprietà della classe di associazione, Vector. Nella classe di associazione, lo Vector viene aggiornato quando viene aggiornata una JTextField nell'interfaccia utente.Come associare una proprietà JList a una classe Bean

public void setName(String name) { 
    String oldName = this.name; 
    this.name = name; 
    Vector oldList=this.list; 
    list.add(name); 
    propertySupport.firePropertyChange("name", oldName, this.name); //textField updates 
    propertySupport.firePropertyChange(PROP_LIST, oldList, list); // JList 
} 

Anche un altro setter separato è disponibile per l'aggiornamento del Vector. Ho impostato aggiungere/rimuovere anche PropertyChangeListeners.

Il mio vero requisito è quello di aggiornare il JList secondo i dati in BeanBinding classe. Ad esempio, quando l'utente digita JTextField, i dati rilevanti dal database e le variabili esistenti in classe devono essere caricati nello JList.

prega, qualcuno me lo faccia sapere come associare i dati BeanBinding classe (fonte) in un JList in NetBeans. Almeno qualsiasi link tutorial. C'è un esempio nel sito NetBeans, ma è per ottenere i dati direttamente dal database.

risposta

3

Non so su Netbeans, solo la codifica menial :-) In questo, avvolgi la tua lista in una ObservableList e tutte le modifiche sull'osservabile dovrebbero funzionare. Ecco un codice eseguibile snipped (scusate per la lunghezza, non hanno l'energia per striscia verso il basso in questo momento)

public class SimpleListBinding { 
    @SuppressWarnings("unused") 
    private static final Logger LOG = Logger 
      .getLogger(SimpleListBinding.class.getName()); 
    private JComponent content; 
    private JList list; 
    private JTextField textField; 

    private List<MyBean> beanList; 
    private JButton modifyButton; 
    private JButton addButton; 


    /** 
    * Binds list and simple properties. 
    */ 
    private void bindBasics() { 
     BindingGroupBean context = new BindingGroupBean(); 
     JListBinding listBinding = SwingBindings.createJListBinding(UpdateStrategy.READ_WRITE, 
       beanList, list); 
     listBinding.setDetailBinding(BeanProperty.create("value")); 
     context.addBinding(listBinding); 
     context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, 
       list, BeanProperty.create("selectedElement.value"), 
       textField, BeanProperty.create("text"))); 
     context.bind(); 
     Action action = new AbstractAction("programatically change") { 
      public void actionPerformed(ActionEvent e) { 
       int selectedBean = list.getSelectedIndex(); 
       if (selectedBean < 0) return; 
       MyBean bean = beanList.get(selectedBean); 
       bean.setValue(bean.getValue() + "*"); 
      } 

     }; 
     modifyButton.setAction(action); 

     Action add = new AbstractAction("add bean") { 
      int count = 0; 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       beanList.add(new MyBean("added: " + count++)); 
      } 

     }; 
     addButton.setAction(add); 
    } 

    private void initData() { 
     MyBean[] beans = new MyBean[] { 
       new MyBean("first"), new MyBean("second"), new MyBean("third") 
     }; 
     beanList = ObservableCollections.observableList(new ArrayList(Arrays.asList(beans))); 
    } 

    public static class MyBean extends AbstractBean { 
     private String value; 
     private boolean active; 
     public MyBean(String value) { 
      this.value = value; 
     } 
     public String getValue() { 
      return value; 
     } 

     public void setValue(String value) { 
      Object old = getValue(); 
      this.value = value; 
      firePropertyChange("value", old, getValue()); 
     } 

     public void setActive(boolean active) { 
      boolean old = getActive(); 
      this.active = active; 
      firePropertyChange("active", old, getActive()); 
     } 

     public boolean getActive() { 
      return active; 
     } 
    } 

    private JComponent getContent() { 
     if (content == null) { 
      initComponents(); 
      content = build(); 
      initData(); 
      bindBasics(); 
     } 
     return content; 
    } 

    private JComponent build() { 
     JComponent comp = Box.createVerticalBox(); 
     comp.add(new JScrollPane(list)); 
     comp.add(textField, BorderLayout.SOUTH); 
     comp.add(modifyButton); 
     comp.add(addButton); 
     return comp; 
    } 

    private void initComponents() { 
     list = new JList(); 
     textField = new JTextField(); 
     modifyButton = new JButton("modify programmtically"); 
     addButton = new JButton(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       final JXFrame frame = new JXFrame("List binding", true); 
       frame.add(new SimpleListBinding().getContent()); 
       frame.pack(); 
       frame.setSize(400, 300); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

} 

Edit: sostituito JXList da JList (nessuna differenza nel comportamento, proprio per renderlo compileable :-)

+0

Grazie mille :) –

+0

Diresti cosa succede quando si usa ObservableList? –

+0

Quello che ho fatto è che ho appena completato la mia lista solo in ObservableList. Ha funzionato perfettamente. :) Ancora una volta Grazie mille:):) –

Problemi correlati