Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:18:31+00:00 2026-06-09T21:18:31+00:00

I’m working on my chat project. I’ve programmed server and client sides that works

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:18:32+00:00Added an answer on June 9, 2026 at 9:18 pm

    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();
            .....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.