2

Making a small application for a school project, I'm basically done except for this one thing. In my first panel people can fill in hours for each day. Those numbers from the textfield need to get added up and the result shown in a textfield in a second panel.

Here the input gets added up in the action listener:

        public class knopHandler implements ActionListener
        {
        public void actionPerformed ( ActionEvent e )
        {
        JFrame frame2 = new JFrame ( "Total Hours" );
        frame2.setSize ( 600, 500 );
        JPanel uitvoerpanel = new uitvoerpanel();
        frame2.setContentPane( uitvoerpanel );
        frame2.setVisible( true );


        String invoerstring1 = maandaginvoer.getText();
        int getal1 = Integer.parseInt( invoerstring1 );

        String invoerstring2 = dinsdaginvoer.getText();
        int getal2 = Integer.parseInt( invoerstring2 );

        String invoerstring3 = woensdaginvoer.getText();
        int getal3 = Integer.parseInt( invoerstring3 );

        String invoerstring4 = donderdaginvoer.getText();
        int getal4 = Integer.parseInt( invoerstring4 );

        String invoerstring5 = vrijdaginvoer.getText();
        int getal5 = Integer.parseInt( invoerstring5 );

        String invoerstring6 = zaterdaginvoer.getText();
        int getal6 = Integer.parseInt( invoerstring6 );

        String invoerstring7 = zondaginvoer.getText();
        int getal7 = Integer.parseInt( invoerstring7 );

        int resultaat = getal1 + getal2 + getal3 + getal4 + getal5 + getal6 
        + getal7;

Now the int resultaat needs to show up in totaalurenvakin the new panel

   totaalurenvak = new JTextField ( 20 );
   totaalurenvak.setHorizontalAlignment ( JTextField.LEFT );
   totaalurenvak.setEditable ( false );
   totaalurenvak.setText(Integer.toString( resultaat) );

Now this is the latest line of code I tried, I have also tried totaalurenvak.setText("" + resultaat); or totaalurenvak.setText(Integer.parseInt ( resultaat) );

Might be worth noting that the input from 2 other textfields do show up in the second panel, only this does not so what is going wrong here exactly? whatever line of code I try the compiler tells me "cannot find symbol".

3 Answers3

1

The problem is: you are not adding the totaalurenvak into the uitvoerpanel

do:

JTextField totaalurenvak = new JTextField(20);
.....
uitvoerpanel.add(totaalurenvak); 

and then at the end of the event do

totaalurenvak.setText(Integer.toString( resultaat) );
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • It's added in the different part of the code, didn't show it here because it was not relevant to the problem. The panel shows the textfield, it just stays empty. – Stefan Buijs Jun 24 '17 at 12:01
  • S you're saying `totaalurenvak.setText(Integer.toString( resultaat) );` should be in the event. At the moment I have `event handler code` and then `class uitvoerpanel extends JPanel` with all the textfield codes and also this `totaalurenvak = new JTextField ( 20 ); totaalurenvak.setHorizontalAlignment ( JTextField.LEFT ); totaalurenvak.setEditable ( false ); totaalurenvak.setText(Integer.toString( resultaat) );` – Stefan Buijs Jun 24 '17 at 12:15
  • When I put `totaalurenvak.setText(Integer.toString(resultaat) );` at the end of event the compiler still tells error:cannot find symbol. I'm really at a loss here of what is going wrong. – Stefan Buijs Jun 24 '17 at 12:25
1

You can just leave the creation of totaalurenvak to the init function in a different part of the code. However, in your event handler you have to set the text for your new JTextField

int resultaat = getal1 + getal2 + getal3 + getal4 + getal5 + getal6 + getal7;
totaalurenvak.setText(Integer.toString(resultaat));

This is due to the fact that you try to change the value of one of your class variables. You have to explicitly call the function setText(value) on that variable.

By the way, it is cleaner to make totaalurenvak a JLabel instead of a JTextField which cannot be edited, since this is semantically more correct.

  • Just tried this but compiler tells me error:cannot find symbol on that line of code. Thanks for the tip about JLabel, obviously a better way yeah. – Stefan Buijs Jun 24 '17 at 12:27
  • as with this case: [link](https://stackoverflow.com/questions/15039852/cannot-find-symbol-in-java), do you perhaps have erroneous access modifiers? – Olaf Schüsler Jun 24 '17 at 12:34
  • You might be unto something, in build output i get this message: :\Program Files\JCreatorV4LE\MyProjects\Java01\java03\src\java03.java:192: error: cannot find symbol totaalurenvak.setText(Integer.toString( resultaat) ); ^ symbol: variable totaalurenvak location: class Paneelinvoer.knopHandler 1 error Process completed. I have no idea what or how this means.. – Stefan Buijs Jun 24 '17 at 12:36
  • Is your `totaalurenvak` a class variable, or declared locally? In the latter case, `totaalurenvak` is not in the correct scope, so cannot be reached in your eventhandler. (https://stackoverflow.com/questions/21197382/java-cannot-find-symbol-swing) – Olaf Schüsler Jun 24 '17 at 12:49
  • I believe locally.. How should I change that? This kind of stuff I'm still very much learning.. – Stefan Buijs Jun 24 '17 at 12:55
  • Yes, you should create the `private JLabel totaalurenvak` declaration in the scope of the class (usually, this is at the top, just below the class declaration, outside of the constructor). If it doesn't work with `private`, try `public`, although you should try to avoid public as much as possible. Regarding the scope of a variable: when you declare a variable inside a method, `for`, `if` etc (everything between { }, called a block statement), this variabele is only accessible within that block statement. – Olaf Schüsler Jun 24 '17 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147515/discussion-between-olaf-schusler-and-stefan-buijs). – Olaf Schüsler Jun 24 '17 at 13:01
0

I figured it out with the very insightful help of Olaf Schusler.

Turns out JPanel uitvoerpanel = new uitvoerpanel(); should be uitvoerpanel uitvoerpanel = uitvoerpanel();