Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying panels...

 
celine mcgowan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

Sorry to bother you all again. I am again having probs with an accursed LED panel thingy. I have finally got it to compile etc - thanks everyone for your help with that (see post here:Non-default constructor problem)! Anyway, I have a method which updates the gifs which are in an LEDPanel:


I am calling the above method 8 times in another class in order to output 8 LED displays. Here's that code:


When i run LEDPanel, i only get one panel displaying. It seems to be the last panel, but i'm not sure! Does anyone know why there is only one LED displaying, and not all 8?

Thanks again all.
Celine
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read up about layout managers in your Swing reference. You need to use one.

Jules
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's the size, possible this line
setBorder( BorderFactory.createEmptyBorder( 20, 20, 20, 20 ) );

should be
ledPanel.setBorder( BorderFactory.createEmptyBorder( 20, 20, 20, 20 ) );
 
celine mcgowan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks for your help. I have changed the code in LEDPanel so that there is a GridLayout manager (one row, eight columns) on the main Panel:


This still only outputs one LED... Am i doing something really silly?

Thanks,
Celine
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can't really tell from the code fragments, but it looks
like you're adding each LEDPanel (twice) and doing some
layout stuff in _updateLED:

CODE FRAGMENT...
//add the whole panel to the window
setLayout( new BorderLayout() );
add( ledPanel );
setBorder( BorderFactory.createEmptyBorder( 20, 20, 20, 20 ) );


Is there another 'shadow' window that you also populate
when you invoke main?

if not, it s.b. ok to remove/relocate this code.


My only other comment would be that your _updateLED method
also appears to allocate the *same* JLabel set (i'm guessing
they are JLabels...) to each panel. As I recall, Swing
won't like this. If you continue to have problems, try
replacing the .setIcon calls with new JLabel eg

eg replace

bottomRight.setIcon(new ImageIcon("vertical_lit.gif"));

with something like

bottomRight = new JLabel(new ImageIcon("vertical_lit.gif"));


good luck!
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok celine,
i looked at your other post with more of your code and i believe i see what you're asking ...

i think my previous response pretty much still applies: you have a couple of places where you are instantiating some objs you don't use, and the updateLED references LED obj that haven't been properly initialized ...

Last comment would be to check your code to see where you are making changes to the LED visual model - probably only want to do things like layout changes once: at init time etc

i couldn't resist re-organizing your code into one LEDPanel class:



hth
 
celine mcgowan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Clio,

You're a lifesaver! I changed "setIcon" to "new JLabel( new ImageIcon..." & took out the panel formatting stuff from the updateLED method as you suggested and it works!!! Makes sense i guess - how can you reset an icon you haven't initialised in the first place. I won't forget this lesson in a hurry!!! Thank you so much for your help & for giving so much of your valuable time to do so.

I have saved the code you posted which is a much more elegant solution to this problem - thanks again. I'm still very much a newbie though & i don't totally understand what's happening there, but i'm gonna spend a bit of time looking at it & running it etc so that i'll understand it by this evening.

Gratefully yours,
Celine
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bravo to you for your hard work and persistence, Celine! i myself learn better by example than by instruction, and so i have benefitted from the wealth of java examples on the web. i'm glad to give back when i can.

thank you for your feedback - 99% of the time i never hear anything so i don't know if i'm helping or harming ... it's nice to hear back, and nicer to have actually helped for a change:-)
 
celine mcgowan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to be modest Clio! I get the feeling you've helped many people before me!!!

Thanks again for your time...
Celine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic