-1

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!


I'm creating a JTextField like this:

JTextField item1 = new JTextField(10);

And here I tried to set text to it:

String setValue = item1.setText("text");

This doesn't work. Why?


About my edit: The full code is commented out, not deleted. --MightyPork

MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • 1
    for future reference, paste only the code in question not the whole program as you may find that no one will be bothered to scan all the lines of pointless code and eventually will not answer the question – Maciej Cygan Aug 07 '13 at 17:13

2 Answers2

6

setText() is the right choice, only you used it in a strange way.

String setValue = item1.setText("text");

setText() has no return value, hence the error about void.
You can't assign void to a variable.


Try this instead:

item1.setText("text");

Or, if you want the value:

String setValue = "text";
item1.setText(setValue);

Or:

String setValue;
item1.setText(setValue = "text");
MightyPork
  • 18,270
  • 10
  • 79
  • 133
1

I copied your original code prior to your edit. Swing applications should be created and started on the event dispatch thread (EDT). The TerminalVenta class should look something like this:

TerminalVenta.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TerminalVenta {
  public static void main (String[] args){
    new TerminalVenta().start();
  }

  public void start() {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        createAndShowGUI();
      }
    });
  }

  public void createAndShowGUI() {
    Gui ob = new Gui();
    ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ob.setSize(700,300);
    ob.setVisible(true);
  }
}

thehandler class:

  public class thehandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      String piezas = item2.getText();
      item1.setText(piezas);
    }
  }

Please note that I kept the name for this class from your original code. But this class does not adhere to the Java Beans naming conventions.

Using item1.setText(item2.getText()); works.

Community
  • 1
  • 1
axiopisty
  • 4,972
  • 8
  • 44
  • 73