0

I have a JLabel into JFrame, I want in windowOpened event of JFrame set label to 'Hello Java' but i get following error :

Syntax error on token(s), misplaced construct(s)

code:

public p5() {
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent arg0) {

            lblA.setText("Hello Java"); // Error is here
        }
    });
    final JLabel lblA = new JLabel("New label");
    lblA.setBounds(91, 68, 46, 14);
    contentPane.add(lblA);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63
  • 2
    please post a SSCCE to receive better help for your problem. Read more about it here: http://sscce.org/ – Markus Oct 02 '13 at 10:52
  • That error message suggests to me that you have code that's not inside a method or something. Post the code you are using (create an SSCCE as suggested in the other comment that demonstrates exactly what you are doing) – Goibniu Oct 02 '13 at 10:58
  • .. 2) `lblA.setBounds(91, 68, 46, 14);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Oct 02 '13 at 11:13

1 Answers1

0

Place

final JLabel lblA = new JLabel("New label");

before

addWindowListener(new WindowAdapter() {
    @Override
    public void windowOpened(WindowEvent arg0) {

        lblA.setText("Hello Java"); // Error is here
    }
});

not after it and your syntax error should go away.

predi
  • 5,528
  • 32
  • 60