aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Trouble with repainting Jframe (i think) 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 "Trouble with repainting Jframe (i think)" Watch "Trouble with repainting Jframe (i think)" New topic
Author

Trouble with repainting Jframe (i think)

axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
not so good at English but I try to explain my problem.
I've put a background image in a frame, over which I've put a panel, on the pannel is a RadioGrup button. The problem was that Radiobuttonss remained selected when clicking them so I've put a Listener on each button, which triggered a repaint () on the panel ( "basicPanel") which has resolved some of the trouble.
(1). Now when I click to select a 2nd radiobutton(holding the clik down) it seems that the first selected buton selects it self also (at least so it seems)
(2). when you hold the mouse down while selecting a radio button, dragging it a way from the panel makes buttons stay selected,
so if yo do this a number of times with all the buttons you get the impression that all buttons are selected.
I think it has something to do with panel overlap or repainting the "basicPanel"... i just want a clean panel without any selecting stuff in there...it's very annoying to see that....any help?... someone? please i've also attatched the image for the code to work properly



[Thumbnail for anyImage.jpg]

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
You may want to start by isolating the problem. Remove all of the buttons except for 2 of them and get that working. This would reduce the amount of code that others on here would have to look through.


SCJA
~Currently preparing for SCJP6
axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
i've tried that..the reason for the big code was to help other see the problem... if some body knows a solution a small code providing the solution would help....thks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
We usually discuss this sort of question on the Swing forum. Moving.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
axel axek wrote:not so good at English but I try to explain my problem.
I've put a background image in a frame, over which I've put a panel, on the pannel is a RadioGrup button. The problem was that Radiobuttonss remained selected when clicking them so I've put a Listener on each button, which triggered a repaint () on the panel ( "basicPanel") which has resolved some of the trouble.

...



Intuition tells me that having an actionListener call for a repaint just so a RadioButton will work properly is not a desirable situation and only serves to obscure the problem.In addition to isolating the problem as Brian suggested, I'd also remove or at least disable the actionListener on the radio button.

In addition to your visual inspection of the radio button to see if it selected, you might also use the Boolean isSelected() method on your buttons to test that your buttons are selected, not just look like they are selected.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I copied and pasted the code and added the following line to the actionPerformed method

System.err.println(command);

I was unable to elicit an error. It all seemed to work correctly.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:I copied and pasted the code and added the following line to the actionPerformed method

System.err.println(command);

I was unable to elicit an error. It all seemed to work correctly.


interesting. That suggests to me a problem or incompatibility with Axel's video card driver.

axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
yes it works fine ....no errors....but still selecting betwen radiobuttons is buggy. I think it has something to do with panel overlap ,i cant't think of something else...
axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
i think if i put a mouse listener for clicking the mouse down (onMouseDown) with a basicPanel.repaint() method i think it will work, but there must be a more simple solution..no?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
In the situation where you can see that a Radio button is selected when it really shouldn't be, does the Boolean isSelected() method confirm your visual inspection that the button really is selected?

An answer to this question might save you a lot of time barking up the wrong tree.

good luck.
axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
isSelected() returs false....so....that is not the case here...the problem is in updating the panel because of panel overlap components are not redrawing correct, so they leve a mark on the panel after a change has ocurred (select, dragging...etc)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
axel axek wrote:isSelected() returs false....so....that is not the case here...the problem is in updating the panel because of panel overlap components are not redrawing correct, so they leve a mark on the panel after a change has ocurred (select, dragging...etc)


OK I see what you mean. Still, I don't imagine you are too happy about all those action listeners just to trigger a repaint. If I was looking for an easier way I'd try without all that. But I guess you thought of that.
axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
OK I see what you mean. Still, I don't imagine you are too happy about all those action listeners just to trigger a repaint. If I was looking for an easier way I'd try without all that. But I guess you thought of that.


in spite of my search I haven't managed to find details about this kind of problem, and nobody seems to know the cause of the problem...i find it really strange
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
One other posisibility. If basicPanel is the only thing you are adding to the contentPane, then instead of adding it as BorderLayout.CENTER this way: getContentPane().add(basicPanel, BorderLayout.CENTER);

consider instead setContentPane( basicPanel );

You wouldn't think that makes a difference, but I have seen a strange problem solved this way.

check out the following thread.

http://www.coderanch.com/t/446219/Swing-AWT-SWT-JFace/java/Sizing-application-image
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
The problem is with putting a JRadioButton on top of a JPanel that is translucent. Even if you set the JRadioButton#setOpaque(false), it's still going to do some painting when selected or when focused, and this painting will be seen on the translucent pane. Here is a somewhat more abbreviated bit of code that I posted in the OP's cross-post (to the OP, please don't cross-post):

see code in reply 3
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
pete stein wrote:(to the OP, please don't cross-post)
Agree. See this link in the other posting. Notification like that should apply to both fora.
axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
Agree. See this link in the other posting. Notification like that should apply to both fora.

yes..sorry for that...Sorry if I upseted somebody I just wanted to get a answer right away....the problem that i'm having is realy annoying, and the code is an example part of a project with GUI, and time is pressing me down to solve it realy quick....so...i tried to get help from here also....and still nobody knows solving it....
axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
One other posisibility. If basicPanel is the only thing you are adding to the contentPane, then instead of adding it as BorderLayout.CENTER this way: getContentPane().add(basicPanel, BorderLayout.CENTER);

consider instead setContentPane( basicPanel );

You wouldn't think that makes a difference, but I have seen a strange problem solved this way.


There is no difference in using "getContentPane().add(basicPanel, BorderLayout.CENTER);" or "setContentPane( basicPanel );", i've tried them both and the same thing happends. Anyway... the link
the guy seems to have truble with resizing the frame, to match his img...and i don't think that borderLayout have someting to do with it...Anyway we're deviating from my problem....
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Yes, in hindsight BorderLayout wasn't the issue in the other thread, but setContentPane was, when it shouldn't have been. So in the absense of a definitive answer, we try things.

Anyways, Mr Stein seems to be suggesting that you shouldn't put radio buttons on a translucent panel, if that is true then the rest of this discussion becomes rather pointless.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Fred Hamilton wrote:Anyways, Mr Stein seems to be suggesting that you shouldn't put radio buttons on a translucent panel,...

Not quite: it's that putting the radio buttons in the translucent panel is associated with these problems, not that it shouldn't be done. I am quite hopeful that one of the experts here will come up with a very clever and succinct solution!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Well, for starters, you dont need all those if(command.equals() ) statements. It would work equally well if your entire actionPerformed() method consisted of a single statement



Of course, if you need the action listener for other purposes besides repainting, then this might not be appropriate.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Fred Hamilton wrote:Well, for starters, you dont need all those if(command.equals() ) statements. ...
Of course, if you need the action listener for other purposes besides repainting, then this might not be appropriate.


Problem solved in cross-post, thanks to Maxideon and without need for actionlisteners at all. Quite elegantly too, I may add (I wish the solution were mine). See link here: Sun cross-post

The solution involves using an inner JPanel that set's opaque to false, and paints itself by drawing a rectangle the size of the JPanel using the background color in the paintComponent method. Note that super.paintComponent(g) is not called in this paintComponent override.

Here is my SSCCE based on Maxideon's solution:
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
What about this?



you could apply setBackground(new Color(0,0,0,0)) to the button panel , if you wanted it to disappear.


[Thumbnail for anyImage.jpg]

axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
Problem solved in cross-post, thanks to Maxideon and without need for actionlisteners at all. Quite elegantly too, I may add (I wish the solution were mine).


yes ....it seems that Maxideon found it http://forums.sun.com/thread.jspa?messageID=10724204 ...I never thought about it in this way....
I want to thank those who were involved in discussing and investigateing my problem..specialy to "Fred Hamilton" and "pete stein" "keep up the good work!!!"
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
axel axek wrote:
Problem solved in cross-post, thanks to Maxideon and without need for actionlisteners at all. Quite elegantly too, I may add (I wish the solution were mine).


yes ....it seems that Maxideon found it http://forums.sun.com/thread.jspa?messageID=10724204 ...I never thought about it in this way....
I want to thank those who were involved in discussing and investigateing my problem..specialy to "Fred Hamilton" and "pete stein" "keep up the good work!!!"



Have you looked at my last solution here Alex? As far as I can see it solves everything, and it seems a lot simpler than Maxideon's.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Fred Hamilton wrote:Have you looked at my last solution here Alex? As far as I can see it solves everything, and it seems a lot simpler than Maxideon's.

I think that Maxideon's solution gets to the root of the actual problem and seems cleaner to me. Also, your solution still has problems that are seen when the a radio button gains and then loses focus: a rectangle appears and persists in the button. To see the two solutions side-by-side, compile and run this, then tab through the radiobuttons. Your solution is on the right:

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Ok Pete, I see what you mean. I went back and verified the same problem with mine. Thanks for taking the time to lay it out. My eyes must have been failing last night, it was late.

Anyways, in my program, I was able to deal with the border problem by adding the line
setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); to the constructor, but I suppose if you want that tiny border on the selected Radio Button, then that's no good either.

Interestingly enough, adding that line to my contructor in your ExampleBoth had no effect, I'm not sure what's happening there.

As far as the other solution being cleaner, I don't quite see it that way, but I can see why Maxideon's solution might be consider closer to the essense of the problem, and therefore perhaps more generally applicable.

You may be interested in the following link.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5001982

best regards, I learned a lot from this thread.

Fred.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Trouble with repainting Jframe (i think)