2

I am trying to add numbers to the "add numbers to message" textfield and when i click the add button i want them to show down below as a list. Currently it is not working the numbers are just getting replaced each time i add a new number. It's probably an easy fix but i am new to this.

The second issue i am having, for example if i enter 5 in "Count of Numbers" text field, i would like the "add" button to disappear as soon as i entered my fifth number. When i enter 1 it seems to work and the "add" button disappears but for any other number it is not working. Image below for how the program should look.

enter image description here

package client;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import com.sun.glass.events.MouseEvent;
import com.sun.prism.paint.Color;

public class Client {

    public static void main(String[] args) {


        JFrame frame = new JFrame();
        frame.setBounds(100, 100, 700, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Client TCP" );
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);    

        JButton button = new JButton("Connect");
        button.setBounds(400, 40, 150, 20);
        frame.getContentPane().add(button);

        JLabel label = new JLabel("");
        label.setBounds(20, 40, 250, 20);
        label.setForeground(java.awt.Color.RED);
        label.setFont(new Font("Times", Font.BOLD, 12));
        label.setHorizontalAlignment(SwingConstants.LEFT);
        label.setVerticalAlignment(SwingConstants.CENTER);
        frame.getContentPane().add(label);
        label.setText("Connection Status: Not Connected");

         button.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {

                 label.setVisible(false);
                 button.setVisible(false);

                    JButton button = new JButton("Disconnect");
                    button.setBounds(400, 40, 150, 20);
                    frame.getContentPane().add(button);

                     button.addActionListener(new ActionListener() {
                         public void actionPerformed(ActionEvent e) {
                                 System.exit(0);
                            }

                     });    


                    JLabel label = new JLabel("");
                    label.setBounds(20, 40, 250, 20);
                    label.setForeground(java.awt.Color.BLUE);
                    label.setFont(new Font("Times", Font.BOLD, 12));
                    label.setHorizontalAlignment(SwingConstants.LEFT);
                    label.setVerticalAlignment(SwingConstants.CENTER);
                    frame.getContentPane().add(label);
                    label.setText("Connection Status: Connected"); 

                    JButton button2 = new JButton("Set");
                    button2.setBounds(400, 100, 150, 20);
                    frame.getContentPane().add(button2);

                    JLabel label2 = new JLabel("");
                    label2.setBounds(20, 100, 250, 20);
                    label2.setFont(new Font("Times", Font.BOLD, 12));
                    label2.setHorizontalAlignment(SwingConstants.LEFT);
                    label2.setVerticalAlignment(SwingConstants.CENTER);
                    frame.getContentPane().add(label2);
                    label2.setText("Count of Numbers:"); 

                    JTextField textfield= new JTextField("");
                    textfield.setFont(new Font("Times", Font.BOLD, 14));
                    textfield.setBounds(175, 100, 150, 20);
                    frame.getContentPane().add(textfield);

                     button2.addActionListener(new ActionListener() {
                         public void actionPerformed(ActionEvent e) {
                                 button2.setEnabled(false);
                                 label2.setEnabled(false);
                                 textfield.setEnabled(false);


                                    JButton button3 = new JButton("Add");
                                    button3.setBounds(400, 150, 150, 20);
                                    frame.getContentPane().add(button3);

                                    JLabel label3 = new JLabel("");
                                    label3.setBounds(20, 150, 250, 20);
                                    label3.setFont(new Font("Times", Font.BOLD, 12));
                                    label3.setHorizontalAlignment(SwingConstants.LEFT);
                                    label3.setVerticalAlignment(SwingConstants.CENTER);
                                    frame.getContentPane().add(label3);
                                    label3.setText("Add Number to Message:"); 

                                    JTextField textfield2= new JTextField("");
                                    textfield2.setFont(new Font("Times", Font.BOLD, 14));
                                    textfield2.setBounds(175, 150, 150, 20);
                                    frame.getContentPane().add(textfield2);              

                                    JLabel label4 = new JLabel("");
                                    label4.setBounds(20, 200, 250, 20);
                                    label4.setFont(new Font("Times", Font.BOLD, 12));
                                    label4.setHorizontalAlignment(SwingConstants.LEFT);
                                    label4.setVerticalAlignment(SwingConstants.CENTER);
                                    frame.getContentPane().add(label4);
                                    label4.setText("Numbers in Message:");  

                                    JTextArea textarea = new JTextArea();
                                    textarea.setFont(new Font("Times", Font.BOLD, 14));
                                    textarea.setBounds(20, 225, 600, 20);
                                    textarea.setEditable(false);
                                    frame.getContentPane().add(textarea);

                                     button3.addActionListener(new ActionListener() {
                                         public void actionPerformed(ActionEvent e) { 
                                             String data = "";
                                             int clicked = 0;
                                             clicked++;
                                             double x = Double.parseDouble(textfield.getText());
                                              if (clicked == x) {
                                                     button3.setVisible(false);
                                                     label3.setVisible(false);
                                                     textfield2.setVisible(false);

                                                     JButton butSend = new JButton("Send");
                                                     butSend.setBounds(275, 275, 100, 20);
                                                     frame.getContentPane().add(butSend);
                                              }      

                                             if (!textfield2.getText().equals("")) {                                                      
                                                 double y = Double.parseDouble(textfield2.getText());                                               
                                                 data += +y+ ",";                                                                                           
                                                 textarea.setText(data);

                                             }



                                         }
                                     });


                         }           
             });

         }

         });

    }
nitind
  • 19,089
  • 4
  • 34
  • 43

1 Answers1

0

When the add button is clicked it will overwrite the text in the textfield. To append you should do something like this

//a global String to store the number data
String data = "";

Then in the button code

//button listener code
if (!textfield2.getText().equals("")) {  
    double x = Double.parseDouble(textfield.getText());                                                      
    double y = Double.parseDouble(textfield2.getText()); 
    data += y + ",";                                                                                           
    textarea.setText(data);                                               
}

But for the second problem, you could add a listener to the text field and each time it is changed update a counter variable. There are examples of text field listeners at this post https://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield.

Overall as some general coding advice, I would name your buttons and labels with a name that denotes what they do, also some comments and spacing would be helpful for readability.

Ohh and in Java class names should be capitalized and package names should be lower case like you have it.

-dogtreat268

  • after you click connect and then click set, hover your mouse arrow over the area of the add button and it should show up. –  Feb 04 '19 at 22:42
  • Ahh that's strange, upon some further looking I saw that you set the text with textarea.setText(+y+","); which will override the text in the textarea. To append you should store the data in a String, append it to that and then update the text field to that. – dogtreat268 Feb 04 '19 at 22:45
  • i tried the above code, its still replacing the number each time i click add –  Feb 04 '19 at 23:38