Wednesday, 7 August 2013

JAVA putting text in JTextfield

JAVA putting text in JTextfield

i'm writing this program where eventually i'm going to get info from a
multidimensional array into the JTextfields, the info will depend on what
the user inputs in the "item2". My problem is I cannot get any kind of
data from the "theHandler" class into the JTextfields. I try to use
"setText" but it tells me it cannot change void to string. I could also
use the value I get in theHandler class for "piezas" and use it on GUI,
but I can't return the value from piezas to GUI. Not sure what to do here.
I already have the array ready, I just need to get the values on the same
class to write a switch so I can get the info in the JTextfields.
So basically, I need to get the "piezas" value from thehandler class into
the Gui class (or be able to input text in the JTextfields from thehandler
class).
Thnx for the help!
PS: Buttons are not ready yet, right now i'm only trying to get the
variables ready to keep going.
import javax.swing.JFrame;
public class TerminalVenta {
public static void main (String[] args){
Gui ob = new Gui();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(700,300);
ob.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui extends JFrame {
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
private JTextField item1;
private JTextField item2;
private JTextField item3;
private JTextField item4;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
public Gui(){
super("Terminal Venta");
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
String cantidad = new String();
String descripcion = new String();
label1 = new JLabel("REVISIÓN DE PIEZAS");
c.gridx = 2;
c.gridy = 0;
c.insets = new Insets (10, 10, 10, 10);
add(label1, c);
label2 = new JLabel("Estatus de conexión");
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets (10, 10, 10, 10);
add(label2, c);
label3 = new JLabel("Número de pieza");
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets (10, 10, 10, 10);
add(label3, c);
label4 = new JLabel("Cantidad");
c.gridx = 2;
c.gridy = 2;
c.insets = new Insets (10, 10, 10, 10);
add(label4, c);
label5 = new JLabel("Descripción");
c.gridx = 3;
c.gridy = 2;
c.insets = new Insets (10, 10, 10, 10);
add(label5, c);
item1 = new JTextField(10);
c.gridx = 0;
c.gridy = 3;
c.insets = new Insets (10, 10, 10, 10);
add(item1, c);
item2 = new JTextField(10);
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets (10, 10, 10, 10);
add(item2, c);
item3 = new JTextField(cantidad, 10);
c.gridx = 2;
c.gridy = 3;
c.insets = new Insets (10, 10, 10, 10);
add(item3, c);
item4 = new JTextField(descripcion, 10);
c.gridx = 3;
c.gridy = 3;
c.insets = new Insets (10, 10, 10, 10);
add(item4, c);
button1 = new JButton("Conectar");
c.gridx = 5;
c.gridy = 1;
c.insets = new Insets (10, 10, 10, 10);
add(button1, c);
button2 = new JButton("Enviar");
c.gridx = 5;
c.gridy = 2;
c.insets = new Insets (10, 10, 10, 10);
add(button2, c);
button3 = new JButton("Otra consulta");
c.gridx = 5;
c.gridy = 3;
c.insets = new Insets (10, 10, 10, 10);
add(button3, c);
button4 = new JButton("Salir");
c.gridx = 5;
c.gridy = 4;
c.insets = new Insets (10, 10, 10, 10);
add(button4, c);
thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
button4.addActionListener(handler);
}
public class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String piezas = item2.getText();
System.out.println(piezas);
//String setValue = item1.setText("text");
}
}
}

No comments:

Post a Comment