Question

Why can't I copy the text from a JTextField to a JTextArea? orders is a JTextArea and get is a JTextField. Status is JButton.

class client:

  public client() extends superclass{
        super("CLIENT");
        setLayout(new FlowLayout());
        update = new JLabel("status");
        add(update,BorderLayout.SOUTH);
        order = new JPanel();
        add(order,BorderLayout.SOUTH);
        menu = new JList(kveb);
        menu.setVisibleRowCount(8);
        menu.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        add(new JScrollPane(menu));
        get = new JTextField("chaweret rac gsurt");
        add(get);
        status = new JButton("ORDER!");
        status.setFont(new Font("Serif",Font.BOLD,16));    
        add(status);
        guga k1 = new guga();
        menu.addListSelectionListener(k1);
        get.addActionListener(k1);
        status.addActionListener(k1);
        server tes = new server();
        tes.setSize(300,300);
        tes.setDefaultCloseOperation(EXIT_ON_CLOSE);
        tes.setVisible(true);
    }

class server:

public class server extends superclass{
    public server() {
        super("SERVER");
        setLayout(new FlowLayout());
        // public JCheckBox ready;
         // public JCheckBox working;
         // public JCheckBox ordered;
          //public JCheckBox trans;
        //  public JTextField orders;
        ready = new JCheckBox("text");
        ready.setFont(new Font("Serif",Font.BOLD,16));
        add(ready);
         Font x = new Font("Serif",Font.BOLD,16);
         working = new JCheckBox("text here");
         working.setFont(x);
         add(working);
         migebulia = new JCheckBox("text heere");
         migebulia.setFont(x);
         add(migebulia);
         trans = new JCheckBox("mtext");
         trans.setFont(x);
         add(trans);
         orders = new JTextArea("text");
         orders.setFont(new Font("Serif",Font.BOLD,16));
         add(orders);
         guga k2 = new guga();
         ready.addActionListener(k2);
         working.addActionListener(k2);
         ordered.addActionListener(k2);
         trans.addActionListener(k2);
        // orders.addActionListener(k2);    
    }    
}

super class:

public class superclass extends  JFrame {
      //client
      public JList menu;
      public  JTextField get;
      public JPanel order;
      public JButton status;
      public String kveb[] = {"text","texti","text","text"};
      public JLabel update;
      //server
      public JCheckBox ready;
      public JCheckBox working;
      public JCheckBox ordered;
      public JCheckBox trans;
      public   JTextArea orders;

    public superclass(String string) {

    }

    class guga implements ActionListener, ListSelectionListener,DocumentListener{
        public void actionPerformed(ActionEvent event){
            if(event.getSource() == status){
                event.setSource(get);
                 orders.setText(get.getText());
            }
        }
    }
}
Was it helpful?

Solution

If you want to copy the text only when the button status is hit, then you can do:

status.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent pE) {
        orders.setText(get.getText());
    }
});

If you want to copy from Server Frame to Client frame or the opposite, you need to pass destination component to parent class in constructor:

JTextArea destTextArea;
public superclass(String string, JTextArea pDestTextArea) {
    destTextArea = pDestTextArea;
}
....
public void actionPerformed(ActionEvent event){
    if ((event.getSource() == status) && (destTextArea != null)) {
        destTextArea.setText(get.getText());
    }
}

Then to create your Client and Server:

super("CLIENT", null);
super("SERVER", orders);

or:

super("CLIENT", orders);
super("SERVER", null);

OTHER TIPS

I don't know what the structure of your application is, but try to merge what I post here with your current code with the least changes you can. Once it works you can start moving things around.

Client class:

public class Client {

    public JTextField get = new JTextField("chaweret rac gsurt");
    public JButton status = new JButton("ORDER!");

    Client() {

        status.addActionListener(new Superclass.MyListener());
    }
}

Server class:

public class Server {

    public JTextArea orders = new JTextArea("text");
}

Superclass class:

public class Superclass {

    static Client client = new Client();
    static Server server = new Server();

    public static class MyListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource().equals(client.status))
                server.orders.setText(client.get.getText());
        }
    }
}

Note: class names should start with an uppercase letter according to Java conventions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top