0

I have public static variable in a JFrame. I have created a JLabel in another JFrame. I want to set the JLabel text to that variable. when I create the JLabel from netbeans, it create autogenerated code that can't be changed. I used netbeans 7.2.

I can change the JLabel using myJLabel.setText(JFrame.variableName);. but the problem is auto generated code doesn't allow me to edit above code snippet.

I would like to set the text to variable name by setting in property panel rather changing above code.

Is there a way to set the dynamic text using property panel?

P.S. - I noticed in property panel, we can set jLabel value from existing component but these components reside in same JFrame. My variable reside in another JFrame

aterai
  • 9,658
  • 4
  • 35
  • 44
lakshman
  • 2,641
  • 6
  • 37
  • 63
  • Make the variables public from the properties panel. And moreover using autogenerated code changes the view of the app on different OS's. Also some components may be snipped out – Sri Harsha Chilakapati Nov 04 '12 at 05:10
  • 1
    You could set it after the call to `initComponents` manually – MadProgrammer Nov 04 '12 at 05:17
  • since I can't change autogenerated code, I remove the existing text from property panel and used setText method in JFrame constructor after ``initComponents`` method. that works properly. but if there is a way to set the text from property panel.it would be better...:) – lakshman Nov 04 '12 at 05:17
  • 1
    *"variable in a JFrame. I have created a JLabel in another JFrame."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Nov 04 '12 at 07:12
  • IMO I would strongly recommend against using an IDE GUI Builder. An IDE is there to provide more productivity for those who already know the language. Now I am not saying whip out notepad and begin but rather code your UI etc manually this will give you a better grasp of the language – David Kroukamp Nov 04 '12 at 07:12

2 Answers2

3

You have two choices that I can see.

One, you simple set the text of the label after the call to initComponent

Or...

  • Click the label in question
  • Click the "Code" button on the properties sheet
  • Click the "..." button against "Post-Creation Code"
  • Enter the code you like to executed, something like myJLabel.setText(JFrame.variableName);. Remember, this code is inserted inline, so it must be well formatted and compilable

enter image description here

Which then produces something like...

private void initComponents() {    
    jLabel1 = new javax.swing.JLabel();
    jLabel1.setText("Hello");

    setLayout(new java.awt.GridBagLayout());
    add(jLabel1, new java.awt.GridBagConstraints());
}// </editor-fold>

NB- Make sure you clear the default text from the label ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Anything beyond the most basic of UI's, especially dynamic code, will require that you stop using the auto-generated features of almost any and all IDE's.

Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192