Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing Jpanel not updating on a certain call

 
Matthew Lannom
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello This will be my first post and my name is Matthew. I've read though head first java(and am doing it again also design by the same people)
I've been working on this game for some time and it worked in the command line, but I've run into problem after problem once
I've moved to Swing.

Main class(gutted to show where things are happening)


Gui class



When the new room is called to create buttons for movement there is no problem but when
this bit of code(below) is called below it freezes up on updates to the gui. However the system.out.println still work.
So the methods are run though but the updates can't be seen. If this is obvious I greatly apperciate being pointed in the right direction.
Thank-you for your help.


 
Campbell Ritchie
Sheriff
Pie
Posts: 49445
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I had to break some of your lines because they were too long.

Your code looks confused. I think what has happened is that you do not have an interface for your game. You should have a load of public methods which instruct the game to take the next move, retrieve details of the room baddie count, etc etc. Then you call those methods from the GUI.
Why are you removing buttons from the display? That will make it look very strange. You know you can enable or disable buttons?
You haven't shown us the code which causes changes to the GUI. Only the add buttons and remove buttons methods. How are you calling them? I don't think we can help until we see that.
Which thread are you running that code in? System.out.println(Thread.currentThread().getName());
Why are you using Flow Layout? I think flow is a particularly useless layout.
Never write == true or == false, which are both poor style and error‑prone if you write = instead of ==. Not if (b == true)... or if (b == false)... but if (b)... and if (!b)...
 
Rob Camick
Ranch Hand
Posts: 2600
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic code when you remove/add components to a panel is to use:

 
Matthew Lannom
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch
I had to break some of your lines because they were too long.

Your code looks confused. I think what has happened is that you do not have an interface for your game. You should have a load of public methods which instruct the game to take the next move, retrieve details of the room baddie count, etc etc. Then you call those methods from the GUI.
Why are you removing buttons from the display? That will make it look very strange. You know you can enable or disable buttons?
You haven't shown us the code which causes changes to the GUI. Only the add buttons and remove buttons methods. How are you calling them? I don't think we can help until we see that.
Which thread are you running that code in? System.out.println(Thread.currentThread().getName());
Why are you using Flow Layout? I think flow is a particularly useless layout.
Never write == true or == false, which are both poor style and error‑prone if you write = instead of ==. Not if (b == true)... or if (b == false)... but if (b)... and if (!b)...



Sorry I gutted those because I thought they would just add more lines that people would not want to see.

here is the complete main class code




Thank-you for that input I'm sorry things are confusing. I did not know I could enable and disable buttons
I'll look into making your suggested changes thank-you
EDIT: Running the suggest println segment only gives
AWT-EventQueue-0


EDIT2: if it helps I'll explain the basic idea of the game. The player is presented with text explaining what is
happening in a textArea below the text area is a panel that will fill with button that the player can then
choose from. In this case the buttons are move, search, and inventory. When move is pressed it then removes
those button and then adds the new set of options. On testing where the exits are in the room it will then display
buttons saying north, south, etc. The movement and those buttons work just fine and add/remove just fine. However
once we get to the area where it goes to call the battle it stops working. The updates to the gui stop however the methods all
get called and ran just fine.

Again I thank everyone and anyone for help given and offered.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic