This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hello, I am trying to make jButton in Netbeans and what i want is: When I click on button it change its color and text. code for changing color and text i want as code in properties of jButton, because in final there will be about 300 buttons, and i want each one to change dynamically its color and text. So parameter color is something like jButton1.setBackground(new Color(variable,255-variable,20,255) and text is jButton1.setText("" + variable);. Action itself looks simple. variable++; But after this action color and text does not change nor with repaint() nor revalidate() neither updateUI(). Can anybody help ? And one more question, why netbeans is locking its generated code? Sorry for poor english and thanks for help.
Why are you using revalidate,updateUI and repaint? A simple setBackground() and setText() should work inside the action performed.
Can you post your SSCCE which demonstrates your problem? It will help us help you better. (Please do take out time to click on the SSCCE word to find out what it really means)
I am sorry I do not understand the net beans part of your question. I am not a net beans user.
Thanks for help. It looks that i do not really understand these methods, I am totally new to JAVA. And I was thinking that if I put some code to the init.Component , I can recall it with repaint or something. As i said, I am new to JAVA.May I ask What IDE do you use for programing in JAVA. I was trying EasyEclipse but it not work well on my W7x64(there was no wizard for creating new file...).
Maybe its not bad idea. Netbeans driving me crazy with that locked generated code . Can You suggest some good text editor for me (beginner) please? I´ve heard about eclipse it is a good editor ? Thank You for your time and advice.
Eclipse is more than just an editor, but as long as you don't use its GUI builder (it has one as some plugin) that shouldn't be a problem. Real text editors are JEdit and Notepad++, both are free and support syntax highlighting.
Joined: Feb 19, 2011
OK. So I go for jEdit and start to explore world of JAVA Thanks for advice
Julius Stecik wrote:Maybe its not bad idea. Netbeans driving me crazy with that locked generated code . Can You suggest some good text editor for me (beginner) please? I´ve heard about eclipse it is a good editor ? Thank You for your time and advice.
NetBeans is pretty neat if you learn how to use it. Hint: forget about editing generated code. If you think you need to, you're doing something wrong.
Regarding your problem, you just need to tell the editor to generate action-related code.
Select the actionPerformed method from the drop-down, then add whatever you want it to. Write a main method to put it an a frame so you can test it.
Run the file
You can also add your own listeners as inner classes, then manually add a new listener in the constructor, like this:
As for generating 300 buttons... using the GUI editor might not be the best way of doing this, because you'll want to store the buttons in an array or ArrayList, whereas the GUI editor will store them as individual variables, and you'll need to type a line for each to add a listener, which is a bit unwieldly and unmaintainable. You could design most of the GUI on the editor, then create a custom panel containing the 300 buttons and add this to your generated panel's source.
As a beginner, check out the book Head First Java - the Beat Box example is fun to do and goes over a lot of this material.