Well, I have "why does it work this way question"....
I have a simple JPanel (MealsPenel) for entering Meals into a Database which is added to JTabbedPane. The JTabbpedPane is added to the Frame.
In my MealsPanel I have a JButton (btnActn) that I use for different actions ("Save","Delete", "Edit" etc). I change the text and ActionCommand for the btnActn
dynamically, based on the Menu Actions selected by the user. I ran into a situation where if I select "Browse" from the Menu the text for btnActn is set to "Edit".
If I then select "Delete" from the menu, the btnActn text should be updated to "Delete" but it's not.... it stays "Edit".
I have tried using setVisible(false),revalidate(),repaint() on the JButton, the JPanel (this.revalidate() etc), and the JTabbedPane with no change to the text of
the btnActn. The only way I could get the panel redisplayed with the proper text is by calling the JFrame.repaint() method. I don't understand why I "have" to call
repaint() for the frame to get it to redisplayed....
Can someone explain this to me? From my tests it seems like the panel did not change enough to force a redraw of the panel? I also saw one or 2 posts on the web
that mentioned redrawing the highest level components (like JFrame) in order to have things redrawn. This seems kind of silly...
reinitPanel() - Updates the text of the btnActn button:
the best way *always* to get help is to post the code in a way that we can
and just enough code to reproduce the problem
i.e. get rid of everything unrelated to the problem
Joined: Oct 28, 2011
Sorry, I just thought that this was a swing behavior question, I didn't think code would be needed....
Is there anywhere on this site for guidelines on how/what code should be included in posts?
Anyway, here is the code for the Panel:
And the tabbed Pane:
Joined: Jun 09, 2003
> Is there anywhere on this site for guidelines on how/what code should be included in posts?
no - it's mostly common sense on what to include in posts.
consider this fairly common type of question/scenario:
(OP = Original Poster, R = Response)
OP. my JButton doesn't appear, is this a bug in Swing?
R. no, post your code
OP. here's the code JButton btn = new JButton("OK");
R. that's useless, post a demo program (just a frame with button so we can see what you're doing).
OP. OK - here's the whole code
(posting 500 to 1000 lines of code including database connectivity code, menuItems, listeners = 480 lines of useless code)
R. no-one is going to read that and we can't run it to see what's happening - strip it down to the bare minimum to reproduce the problem
OP. now posts a short compilable/runnable demo program along the lines of
JButton btn = new JButton("OK");
JFrame f = new JFrame();
R. you're setting the frame visible before adding any components.
this whole thing takes about 2 days to get the solution.
had the OP posted the short demo program initially, the solution would have been within hours.
now, for the code you posted, look at it from our point of view:
1. try to compile/run the posted code from only what has been posted
2. the topic is "JButton Text Not Updated unless JFrame is redrawn" - how many lines of the posted code are
totally unrelated to that problem.
when you say this
"I just thought that this was a swing behavior question"
the swing behaviour part is that JButton text will be redrawn/repainted automatically/immediately by java,
unless there's a problem in the code.
Joined: Oct 28, 2011
Ok, it took some doing, but I think I have finally figured this out...
Michael, you were right, the problem was in my code. It turns out the problem was in my ActionListener. I was updating the panel and adding it to the Tabbed Pane but I never bothered to remove it first to get the latest updated version of the panel. Once I added the proper logic in the right sequence things are working as expected.
My apologies Michael, for complicating things needlessly, I appreciate the help you attempted to give me.
subject: JButton Text Not Updated unless JFrame is redrawn