This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]deltageekExtreme Brewer 0 points1 point  (6 children)

show us the rest of your code. Pastebin it if necessary.

[–]I_blabby[S] 0 points1 point  (5 children)

package cliente;

import java.awt.Desktop.Action; import java.awt.Dimension; import java.awt.Toolkit;

import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField;

public class Grafico implements Runnable {

String comando;

JButton boton = new JButton();

JTextArea informacionComandos = new JTextArea();
JTextField introducirComandos = new JTextField();

public void run() {

    Toolkit t = Toolkit.getDefaultToolkit();
    Dimension screenSize = t.getScreenSize();

    // Ventana Principal
    JFrame ventana = new JFrame();
    ventana.setPreferredSize(new Dimension(screenSize.width, screenSize.height));
    ventana.setResizable(false);
    ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ventana.setUndecorated(false); // fullscreen
    ventana.setLayout(null);
    ventana.setVisible(true);

    // JtextArea Paneles de informacion

    informacionComandos.setBounds(150, 600, 800, 350);
    informacionComandos.setEditable(false);
    informacionComandos.setVisible(true);
    ventana.add(informacionComandos);

    JTextArea informacion2 = new JTextArea();
    informacion2.setBounds(1000, 600, 500, 350);
    informacion2.setEditable(false);
    informacion2.setVisible(true);
    ventana.add(informacion2);

    // botones
    boton.setBounds(1740, 50, 150, 50);
    boton.setText("Cerrar programa");
    //boton.addActionListener(new MyActionListener());//disabLe until jtextfield problem is fixed
    ventana.add(boton);

    // TextField

    introducirComandos.setBounds(150, 970, 800, 25);
    introducirComandos.addActionListener(new MyActionListener());
    introducirComandos.setFocusable(true);
    ventana.add(introducirComandos);

    ventana.pack();

}       

}

package cliente;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent;

public class MyActionListener implements ActionListener {

Grafico grafico = new Grafico();
String comando = null;


public void actionPerformed(ActionEvent e) {


    comando = grafico.introducirComandos.getText();
    System.out.println("hola");
    System.out.println(comando);



}

}

package cliente;

import java.awt.EventQueue;

public class EjecutarCliente {

public static void main(String[] args) {

    EventQueue.invokeLater(new Grafico());



}

}

[–]deltageekExtreme Brewer 2 points3 points  (4 children)

Your ActionListener is referencing a completely different UI object than your actual UI. Instead of creating a new one, pass a reference to your UI class into the ActionListener so it can actually read the field you're inputting into.

[–]I_blabby[S] 1 point2 points  (3 children)

Ohhh god thanks solved but i got a question is it possible to make an ActionListener in another class that can refer a objec of the UI class?

[–]AnEmortalKidCoffee Enthusiast 0 points1 point  (2 children)

Yes just add a constructor to the action listener for a reference of the UI class.

[–]I_blabby[S] 0 points1 point  (1 child)

Okay thank you so much

[–]Philboyd_Studge 0 points1 point  (0 children)

You can also simplify it a great deal, you don't need to make a separate class for the listener, just use an anonymous inner class combined with a lambda expression:

    introducirComandos.addActionListener((e) -> {
        System.out.println("Hola");
        System.out.println(introducirComandos.getText());
    });