0

I have two classes, in the first file there are JLabel components and in the other class, there is an input dialog from which I wanted the user-submitted number to be displayed in the first class but can't get it to work.

In the first file, labels are created with Netbeans Swing and are public.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Object[] values = { "Twenty", "Ten", "Five" };
    int pickedValues = JOptionPane.showOptionDialog(null, "Choose which label do you want to edit", "Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, values, values[0]);
    
    if(pickedValues == 2){
        Scanner sc = new Scanner(System.in);            
        String inputValue = JOptionPane.showInputDialog("Write the value");
        inputValue = sc.nextLine();
        int valueInt = Integer.parseInt(inputValue);
        labelFive.setText(String.valueOf(valueInt));
    }
}            

labelFive is the name of the jLabel in the first file, I have no idea what to try here.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dtomicic
  • 23
  • 5
  • 3
    Add a method in the other class to set the text. And create a reference to it in your first class. – Just another Java programmer Oct 18 '21 at 15:40
  • 2
    Don't let the GUI editor dictate your program's design; here's one [approach](https://stackoverflow.com/a/10523401/230513); please [edit] your question to include a [mre] that shows your revised approach. – trashgod Oct 18 '21 at 15:42
  • 1
    Besides the good advice from @trashgod & JaJP, don't mix console (`System.in`) and GUI operations. The `inputValue` should have a value by the moment the input pane has closed (unless the user cancels it). – Andrew Thompson Oct 18 '21 at 23:03

0 Answers0