• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jButton revalidate, updateUI, repaint does not working

 
Julius Stecik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

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.
 
Julius Stecik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...).
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of us use a regular text editor and do it all ourselves. It's a bit more work but you have more control over the code, and you learn how Swing works.
 
Julius Stecik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Julius Stecik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. So I go for jEdit and start to explore world of JAVA Thanks for advice
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic