Domanda

I'm working on my chat project. I've programmed server and client sides that works without GUI, just console UI. Now, while working on clients GUI (with Netbeans provided tools, not as I am used to code by myself), I've stuck on binding problem.

Inside of ClientGui class I have Client object. In my GUI I want to disable input textfield until client isn't connected to a chat server. I've tried to bind (via Netbeans GUI) my input textfield's property enabled to that client object's method isConnected() (that returns boolean). isConnected isn't just returning some variable's value, it's combined boolean expression. So when user clicks to connect, it succeeds, but input textfield doesn't change it's state to enabled.

So as I get it, I have to work with event and listeners and notify in my Client class? But what is point of binding then, as I could just have event fired on my Client and my input field listen to clients connected event?

So I provide chunks of my code.

The Client class: (You may see some lines with action listeners and event, I didn't remove them, just experimented)

public class Client {
    private ClientListener listener;
    private ClientSender sender;
    private Socket connection;

    private boolean finnish = false;
    private PropertyChangeEvent connected;

    public Client(String hostname, int port) throws UnknownHostException, IOException {
        connection = new Socket(hostname, port);
    }

    public void start() {
        try {
            connected = new PropertyChangeEvent(this, "connected", null, connection);

            sender = new ClientSender(new ObjectOutputStream(connection.getOutputStream()));
            Thread senderThread = new Thread(sender);
            senderThread.start();
            Logger.getLogger(Client.class.getName()).log(Level.INFO, "Sender thread has started");

            listener = new ClientListener(new ObjectInputStream(connection.getInputStream()));
            Thread listenerThread = new Thread(listener);
            listenerThread.start();
            Logger.getLogger(Client.class.getName()).log(Level.INFO, "Listener thread has started");


        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, "IO problems", ex);
        }
    }

    public ClientSender getSender() {
        return sender;
    }

    public void stop() {
        sender.stop();
        listener.stop();
    }

    public boolean isConnected() {
        return connection != null && !connection.isClosed();
    }
}

The Client GUI class:

public class ClientGui extends javax.swing.JFrame {
    private Client client;

    public boolean getConnected() {
        System.out.println( client != null && client.isConnected());
        return client != null && client.isConnected();
    }

    /**
    * Creates new form ClientGui
    */
    public ClientGui() {
        initComponents();
    }

    // GENERATED CODE

private void tfUserInputKeyPressed(java.awt.event.KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        Message message = new Message("user", tfUserInput.getText());
        client.getSender().add(message);

        tfUserInput.setText("");
    }
}

private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        client = new Client(tfHostname.getText(), Integer.parseInt(tfPort.getText()));
        client.start();

    } catch (UnknownHostException ex) {
        Logger.getLogger(ClientGui.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ClientGui.class.getName()).log(Level.SEVERE, null, ex);
    }

}

// and somewhere GUI generated code of my binding (also tried with custom code, but no success)
 org.jdesktop.beansbinding.Binding binding =
 org.jdesktop.beansbinding.Bindings.createAutoBinding 
(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this,  
 org.jdesktop.beansbinding.ELProperty.create("${connected}"), listConversation,  
 org.jdesktop.beansbinding.BeanProperty.create("enabled"), "listConversationBinding");

 bindingGroup.addBinding(binding);

In fact it's a JList, but doesn't matter, because I want such binding for few components. Here I try to use fake method in GUI Form, which calls clients connected (did it because don't how to add Client as a component).

I've read on forums, everywhere saying about beans and so on. I want my Client class to have least as possible code needed for GUI, interface implementations and calls for firing event and so on.

UPDATE

Very good! Thank you. Why can't I bind so I don't have to use setEnabled(value) method (make that enabled property keeps track of boolean expression "property" (connection != null && !connection.isClosed()). Also, because of this trick I have to do setConnected(value), even if this is resolved in runtime depending on a connection, and I even can't know old value (of course I can do private void setConnected(booleanvalue) and put calls to this with true or false depending on what happens in those places. Seems like my idea of using property is wrong, better do with actions or events.

È stato utile?

Soluzione

You should add PropertyChangeSupport to the Client.

    final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
             this.pcs.addPropertyChangeListener(listener);
    }


      boolean connected;

 public boolean isConnected() {
     return connected;
 }

 public void setConnected(boolean connected) {
     boolean oldValue = this.connected;
     this.value = connected;
     this.pcs.firePropertyChange("connected", oldValue, newValue);
 }

 .....


     public Client(String hostname, int port) throws UnknownHostException, IOException    {
    connection = new Socket(hostname, port);
    setConnected(connection != null && !connection.isClosed());
}

in the GUI

    public class ClientGui extends javax.swing.JFrame implements PropertyChangeListener
    .....
    propertyChanged(..){
      tfUserInput.setEnabled(true);
    }

     private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            client = new Client(tfHostname.getText(), Integer.parseInt(tfPort.getText()));
            client.addPropertyChangeListener(this);
            client.start();
        .....

Altri suggerimenti

Melasse framework ease the pain make such binding between model/view(ui component), in most case without even having to code extra anonymous classes: https://github.com/cchantep/melasse

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top