• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI troubles  RSS feed

 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I tossed some swing code into an existing project of mine, and am having a bit of trouble with it. First and foremost, my panel appears completely blank until I resize it. Additionally it seems to stop responding when I allow an earlier aspect of my class to work, which is a loop in DOS asking for user input. Does gui not get along well with DOS?

Edit: By DOS I mean the ordinary non-graphical command prompt.

Secondly, I want my JTextArea to simply expand to be the same size as the JPanel which it contains, as opposed to having to guess how many rows/columns is equal to 800x500 pixels. (And what if the user resizes the window?)

Thanks in advance! Here's the code:


[ June 04, 2008: Message edited by: Alex Birmingham ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding a call to frame.pack() at the end of the "gui" method. That should cause the components to be drawn.

I don't think that a JTextArea can automatically adjust to the space that's available.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't, unless the LayoutManager is telling it to do so.

Alex, in your code, you add your JScrollPane to a JPanel (with a FlowLayout). Then you add this JPanel to your frame's content pane with BorderLayout.CENTER. This means that your JPanel will be stretched, but the JScrollPane inside won't.

Now I don't see you adding anything else to the JPanel, so why not remove that, and add the JScrollPane directly to the content pane? That means the JScrollPane will take all available space and the JTextArea will stretch as well.
 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A note on your loop checking the command line:

Your GUI will not update because your application is looping on the same thread as your GUI; until the loop releases control, the GUI will be unable to respond to events (like clicks and such).

Try using a Thread, or maybe SwingWorker.
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses all! Pack() and so on and so forth fixed all my troubles.

1. As for threads, I learn about them next chapter, and am really excited

2. As I further implement swing(), I'm having a bit of trouble incorporating it into my programs that use multiple classes. It's great for a single-class program, because every method has access to the class variables JFrame and JPanel and so on and so forth, but what about when I instantiate another class?

For example, let's say I create the GUI above with JTextArea and JTextField, but then want to invoke something like player.characterCreation() in order to getText() for all the fields required in an instantiation of a Player class. Name, profession, starting area, etc. etc. Do I have to pass TextArea/TextField to characterCreation()? And then create another ActionListener in Player?

Or do I have no option but to put all of this within Input.java? Both options sound super clunky!
 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Birmingham:
Thanks for the responses all! Pack() and so on and so forth fixed all my troubles.

1. As for threads, I learn about them next chapter, and am really excited

2. As I further implement swing(), I'm having a bit of trouble incorporating it into my programs that use multiple classes. It's great for a single-class program, because every method has access to the class variables JFrame and JPanel and so on and so forth, but what about when I instantiate another class?

For example, let's say I create the GUI above with JTextArea and JTextField, but then want to invoke something like player.characterCreation() in order to getText() for all the fields required in an instantiation of a Player class. Name, profession, starting area, etc. etc. Do I have to pass TextArea/TextField to characterCreation()? And then create another ActionListener in Player?

Or do I have no option but to put all of this within Input.java? Both options sound super clunky!


A good Swing application typically makes use of what's known as the MVC design pattern (Model, View, Controller). In a nutshell, it breaks the program into a Model, which represents the data being displayed or managed by the View (whatever you see on the screen). The Controller keeps a hand on the View, and a hand on the Model, making sure they stay in synch - a change to the Model should update the View, and so on.

You can google it for some examples/lessons/articles/etc... or ask your Prof (that is, if you have one!)

In the mean time, I envision all of your Character data encapsulated in a Character class...


So, you can just create a new character like this in your actionPerformed method, which is tied to the button you created:



...done. Now you can deal with the new character as you like. You could use different components (like a JComboBox) to handle number values like level. Hope that helps, let me know!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!