• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with setPreferredSize  RSS feed

 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used the following statement in my program:



The idea was to establish a 4 x 4 grid with respective horizontal and vertical spacing.

I want to use the following code to lock the dimensions to 200 by 150:



However, when I add the setPreferredSize statement I repeatedly receive various errors that as I try to address them, cause more errors.
When I remove the statement, ALL the errors "disappear".

I spent hours researching before and after implementation, and cannot find a resolution. So, I decided to ask for help.

Any suggestions would be greatly appreciated.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing? AWT? JavaFX?
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your panel called 'a' or 'aPanel'?
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeatedly receive various errors

Please copy the full text of the error messages and paste it here so we can see what the problems are.
 
Fred Kleinschmidt
Bartender
Posts: 509
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you setting the preferred size of the panel, or of each of the components you add to the panel?
 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Swing? AWT? JavaFX?


Below is a suggestion I was given for excerpts of code that I could integrate into the "appropriate" areas of my program.



After the appropriate definitions for a class and a method I will use the following statements



However, I don't understand the difference between JPanel and the panel statement below:



Please Note:I do not have the original code nor do I have the original error messages.
After working on the code for three days straight I threw out everything and started over.

I just want to understand if this approach is in the right direction for using setLayout or setPreferredSize .

Then I can address the error messages one at a time.

To address some of the other questions:
I want to set the preferred size of the panel NOT the size of each components that I add to the panel. (That's why I'm not sure if this approach is correct.)

The panel is called box. I used the two original statements as an example. I'll correct the code.

I tried to use

Actually I used


That should address most of your questions. If not, then I apologize. My understanding of these statements is limited at best
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to understand if this approach is in the right direction
It's not.  There are missing definitions for a class and a method.
The code that calls a method needs to be inside of a method that is inside of a class.  The code you posted doesn't have either a class or a method.
The definition and initialization of a variable can be at the class level.
 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please understand, this is not my program or my entire code.
I threw out my code because I wanted to understand what I did wrong.

I'm really trying to understand how to configure the statements.

That's why I'm asking for help.
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is not my program or my entire code. 

That makes it harder.  I would rather not guess what is in the program or what error messages there are when it is compiled.
Can you post a small, complete program that shows the problem and the error messages it gets?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post more code than that, and the full text of the error messages.

I shall move this discussion to our GUIs forum.
 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'll look for help in understanding how to write the code.
Thanks anyway.
 
Fred Kleinschmidt
Bartender
Posts: 509
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, I don't understand the difference between JPanel and the panel statement below:
Panel calculatorPanel = new Panel();



A JPanel is a Swing container, while a Panel is an AWT container - don't use it. Use all JPanels.

To begin with, don't call setPreferredSize() on anything. Just add each item to your panel that has a GridLayout and let them dictate the size based on their default preferred sizes (this happens when you call frame.pack() after adding everything to the panel and adding the panel to the frame).
 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred,

Thank you so much.

You have no idea how much that simple explanation helps me understand how to write this code.
I searched online for hours an couldn't find a clear explanation as to the difference.

So if I understand:

Use JPanel
Don't use setPreferredSize()
Maintain the grid layout
Add all of the items to the panel
use the frame.pack() statement

I will research frame.pack() so I know what to import.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sally Jenkins wrote:
Bear Bibeault wrote:Swing? AWT? JavaFX?
You didn't actually answer that, and that is important. Let us assume you are using Swing, then. Use Swing components and don't use any components whose class name doesn't have a J added to its beginning.


Below is a suggestion I was given for excerpts of code that I could integrate into the "appropriate" areas of my program.

Those are only import declarations, so they don't get you very far with the problem.
. . .
Don't call a panel box; there is such a thing as a Box, so that name can cause confusion.
However, I don't understand the difference between JPanel and the panel statement below:
That means you have slipped into using AWT components. Don't mix AWT and Swing. Don't use the AWT component at all; use JPanel. Simply, JPanel is written all in Java® and Panel is written in (I think) C++, at least in part. Panel uses the OS to display itself and JPanel does the displaying itself.
Please Note:I do not have the original code nor do I have the original error messages.
After working on the code for three days straight I threw out everything and started over.
. . .
I didn't realise that when I told you to post more code last night; sorry.

You have got yourself into a loop where you try things without asking for help and end up panicking; if you had asked earlier we would have helped you earlier.
. . . Then I can address the error messages one at a time.
No, you should not address error messages like that. You should start with very simple code and compile and run it. You start like this:-I did nothing about size or location, so you will get a tiny frame barely large enough to see at the top left of the screen. Don't worry. When you set the preferred size for the panel and use pack(), the frame will enlarge.
Run that sort of code and add one line at a time and compile every time. Then you will only get a few error messages and you can sort them all out. It might appear slower, but you will finish the process with a blood pressure below 220/145 and your paper tissue budget can be reduced.
Add the panel to the frame (and at this stage nothing else), set its preferred size and call pack on the frame. Remember the setVisible call should be the last line in the method. Give the panel the grid layout and then start adding components to the panel. Remember to compile the code every line (or at least for every component added). You probably only need to set the preferred size on the panel; if I remember correctly grid layout gives you a grid of components all the same size, so each component you add to that panel will be ┬╝ the width and height of the panel.
If you do things in stages like that, you will be able to see what is happening, and sort out problems as they occur. And when you ask for help, you will have some idea where the errors are occurring and can give us more directed questions.

If you are writing a calculator, you need a calculator app without a GUI, and you add the GUI to that. So make sure you have a Calculator class (or similar) and make sure it is working before you write any more Swing code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!