• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI: Space not enough

 
Joshua Daniels
Greenhorn
Posts: 11
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! So this is my first time starting a new topic (Yay!)
Anyway, so I'm creating a program that acts like a donut shop. I have no problems regarding the whole syntax and functionality of my code. It's safe to say it works as it should But the problem arises when I run the program and noticed that the North of the border layout is lacking enough space to show every button. Here's the code and the result. Also, I'm a genuine green horn in Java and any suggestions to improve the syntax of my code is much appreciated!


The current result


Notes:
1. The DonutShop class is already fine and decided not to post it.
2. The window size has to be exactly that, so no resizing!
3. It should show 2 text fields, 4 buttons and 1 text area.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't know about your layout problem, but I do not like your listener, with all those if‑elses. I also don't think it will work because you are comparing the button with a String. That is neither functional code nor object‑oriented. You should write something like
startButton.addActionListener(evt -> start());
… where you have a start() method that does all the work. The -> idiom only works in Java8.

Shall move your discussion to our GUIs forum.
 
Piet Souris
Rancher
Posts: 1635
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that you do not issue a "frame.pack()" in the right place.

Your code does not run, since you did not deliver your DonutShop class, and also is your code
lacking a 'main' method. So I had to improvise a little. This works for me:

(by the way: I had to make the parameter 'y' final for the code to compile).
(Also: you might want to wrap your textarea into a scrollpane).

 
Knute Snortum
Sheriff
Posts: 3267
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't mind seeing the source for DonutShop.java, just so I could get the thing running.

(Edit: looks like you might have an answer, so never mind.)
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call pack() and setVisible() from the constructor though. Constructors should only contain initialization code, while pack() and setVisible() may spawn new threads. Always call these in a separate method.

Secondly, all Swing code should be run on the EDT. Use SwingUtilities.invokeLater() to layout and display the frame.

I'm sorry for beating a dead horse Piet, but even if you don't think it's important, newcomers *really* should get used to using the EDT as soon as possible.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The window size has to be exactly that, so no resizing!


That is not how layout managers work. You add components to your frame and then you use the pack() method. The pack() method will then determine the proper size of the frame based on the preferred size of components you added to all the panels.

Somewhere I'm guessing your use frame.setSize(...) which would cause the problem.

any suggestions to improve the syntax of my code is much appreciated!


You must be using an old tutorial. I suggest you get rid of that tutorial and use the examples from the Swing Tutorial. These examples will show you how to better structure your code so you DON'T extend JFrame and you create your GUI on the Event Dispatch Thread.



Don't use magic literals. People don't know what those literals mean. The API will define variables for your to use:





I'm not crazy about that structure. We don't know how all the methods are related to one another. Instead I would suggest a structure like:



Then the createNorthPanel code might look something like:


Then the code for the createSellButton might be something like:



Note, the logic for each of your buttons is different so create a separate ActionListener. Don't try to force all the logic into one ActionListener and create 4 instances of it and use if/else statements.
 
Joshua Daniels
Greenhorn
Posts: 11
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for responding, and as requested by Knute.
Here's the DonutShop class and the DonutRunner class (Main) respectively.


 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to incorporate the advice you received into your code, and let us know if you're still having problems.
 
Joshua Daniels
Greenhorn
Posts: 11
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved it by changing northPanel into a grid layout (3, 1) and adding a bunch of other panels into the grid. As you might have noticed, this is still the old code, just added new panels.



Here it looks like now, just what I wanted.

A big thank you to all who posted and gave amazing advice! Rest assured all of your help will not go to waste and will incorporate it to my future coding ventures! Cheers ~ 'til next time!
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minor style thing: your line 74 was too long, so I corrected it and you can see how to do it. It would probably have been better to break the lines after the plus signs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!