Question

I posted this code earlier, and I got alot of helpful answers and the majority was that I need to change my code completely. Which I understand, and I will do tomorrow! But right now, this is eating away at me at why this won't work.

I am trying to get the sendText from the ChatBox class, to my MessageWindow class and output in the messagePane. That's it. It seems so simple, and it probably is...but I have literally been at this for 10 hours straight. I just want it to output what I put in the ChatBox to the MessageWindow, without completely changing my code. Please help :(

public class ChatBox extends JPanel {

private JScrollPane scrollPane;
private String sendText;

public ChatBox() {
    final JTextArea chatPane = new JTextArea();

    scrollPane = new JScrollPane(chatPane,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    add(scrollPane);
    scrollPane.setMinimumSize(new Dimension(550, 50));
    scrollPane.setPreferredSize(new Dimension(550, 50));

    chatPane.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if( e.getKeyCode() == KeyEvent.VK_ENTER ) {
                sendText = chatPane.getText();
                setText(sendText);
                chatPane.setText(null);
                // System.out.println(sendText); // I can see this in console
            }

        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

    });

}


public String getText() {
    return sendText;
}


public void setText(String sendText) {
    this.sendText = sendText;
}

}

In my head, I am setting the sendText -> whatever I input. Then in the MessageWindow class, I am trying to use the getter to get the text and output in the messagePane.

public class MessageWindow extends JPanel {

private ChatBox box = new ChatBox();

public MessageWindow() {
    JTextArea messagePane = new JTextArea();

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    gc.weightx = 1;
    gc.weighty = 1;
    gc.fill = GridBagConstraints.BOTH;
    gc.insets = new Insets(5, 5, 5, 5);
    add(new JScrollPane(messagePane), gc);

    System.out.println(box.getText());   // Getting null in the console.
    messagePane.append(box.getText());   // Not getting anything on messagePane.

}

}

I know I need to use ActionListeners and a JTextField, instead of a JTextArea. And I promise I will start that tomorrow. I will scrap this whole program as it is, I just need to know why this basic things fails me :( I knew while I was learning Java that getters/setters were going to be a problem for me to completely understand, and I guess I am right about that lol...

Thanks for any help!!!

NEW CODE

public class MessageWindow extends JPanel {

private ChatBox box = new ChatBox(this);

public void OnTextSet(String s) {
    System.out.println(s);
}

public MessageWindow() {
    JTextArea messagePane = new JTextArea();

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    gc.weightx = 1;
    gc.weighty = 1;
    gc.fill = GridBagConstraints.BOTH;
    gc.insets = new Insets(5, 5, 5, 5);
    add(new JScrollPane(messagePane), gc);

    System.out.println(box.getText()); // Getting null in the console.
    messagePane.append(box.getText()); // Not getting anything on
                                        // messagePane.

}

}

And

public class ChatBox extends JPanel {

private JScrollPane scrollPane;
private String sendText = "";
private MessageWindow mw;

public ChatBox() {
    final JTextArea chatPane = new JTextArea();

    scrollPane = new JScrollPane(chatPane,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    add(scrollPane);
    scrollPane.setMinimumSize(new Dimension(550, 50));
    scrollPane.setPreferredSize(new Dimension(550, 50));

    chatPane.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                sendText = chatPane.getText();
                setText(sendText);
                chatPane.setText(null);
                mw.OnTextSet(sendText);
                // System.out.println(sendText); // I can see this in
                // console
            }

        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

    });

}

public ChatBox(MessageWindow mw) {
    this.mw = mw;
}


public String getText() {
    return sendText;
}

public void setText(String sendText) {
    this.sendText = sendText;
}

}

Was it helpful?

Solution

You need a link from the Chatbox to the MessageWindow, inorder to send messages back and forth.

What could be done is modify as below

private ChatBox box = new ChatBox(this); //is this legal in java?
                                  ^^^^
public void OnTextSet(String s){
    System.out.println(s);      
}

//elsewhere
private MessageWindow mw;
public ChatBox(MessageWindow mw) {
               ^^^^^^^^^^^^^^^^
   this.mw = mw

...
public void keyReleased(KeyEvent e) {
...
   mw.OnTextSet(sendText);
}

Now type something and you should see the print out

OTHER TIPS

The variable sendText in ChatBox class isn't initialized on creation. Since keyReleased event is not fired yet, sendText stays NULL. Try to modifiy from your code in Chatbox

from:

private String sendText;

to:

private String sendText = "";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top