aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes jButton revalidate, updateUI, repaint does not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jButton revalidate, updateUI, repaint does not working" Watch "jButton revalidate, updateUI, repaint does not working" New topic
Author

jButton revalidate, updateUI, repaint does not working

Julius Stecik
Greenhorn

Joined: Feb 19, 2011
Posts: 6
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Julius Stecik
Greenhorn

Joined: Feb 19, 2011
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Julius Stecik
Greenhorn

Joined: Feb 19, 2011
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Feb 19, 2011
Posts: 6
OK. So I go for jEdit and start to explore world of JAVA Thanks for advice
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

You're welcome
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jButton revalidate, updateUI, repaint does not working