Lout Manager for re-sizing of panels - java

I am making a gui application where I need a wide panel on one side of the frame, and a thinner panel on the other. The panels are the same height, but when I re-size them I want them to keep a consistent width difference from each other while still changing size.
Here is what it would look like when restored to a smaller size:
And here is what it would look like when it was maximized or made bigger:
I was trying to use Grid Layout for this, but I couldn't quite figure out how to make one smaller and change at a lesser rate when you were making the window bigger. My question is which layout would get me to what I want, and what functions within the layout manager would lead me to the result?

You could use GridBagLayout, but that always seems more trouble than its worth.
Instead, take a look at JGoodies, especially their FormLayout. It's been years since I've done Swing programming, but remember the JGoodies layouts making life a lot easier.

Related

Wrapping Text in Java Swing

Okay, I am kind of desperate right now. I hope you guys can help.
I need to layout content panels with Java Swing. The Problem is, that every content is different. So I need a panel that resize itself for every content. Basically what LayoutManagers are invented for.
I need a left panel and a right panel. The widths of the panels should be fixed. The heights should adjust to the given content
|<---- 30% ------->|<----- 70% -------------------->|
Easy going I thought, but it just wont work. I tried different layout managers. Some of them keep the 30% rule, but doesn't wrap the content and just display them in one single line (BorderLayout).
If a LayoutManager does support line-break (even if its just for HTML text but that is fine for me) it wont support the fixed width. A combination of both didn't worked for me either.
Note that I need to stick to Swing and can not use another more advanced library because the system I am developing for is stuck to Java 1.5. Furthermore, I know the total screenwidth so I could calculate the width of the panels to work with fixed widths, but I need to be flexible with the height.
You can achieve this by using nested BorderLayouts. Start by setting your Panel's layout as BorderLayout.
After that, for each left and right panels, set layouts as BorderLayout again. At this level, you will set %30 and %70 ratio.
Within this layouts, add your contents to NORTH layouts. This will enable your panels' height to match given content.

JAVA: How can I add SWING components to my game HUD with custom textures

-- Little note, I attempted to upload an image of my game to illustrate my question, however I do not yet have the required reputation to do so. I apollogise for this.
I would like to create a drop down screen from the top HUD element on my game which the player can type into, effectively becoming a chat window, the actually window is not an issue and I understand that you can disable background and boarder rendering of Java's Swing components so that isn't an issue.
My question is simple, can I take advantage of java's Swing components like JTextField and position them exactly within the bounds of this area, without having to deal with java's layout classes. So this is a summary:
How do I set the final size of the swing components MANUALLY and
How do I set coordinates of the components MANUALLY With out using a layout manager
Yes you can use a null layout on the container and call setBounds(...) on the component to MANUALLY place them. And this is usually a VERY BAD THING to do as it forces you to paint yourself into a layout corner making it very hard to upgrade or enhance your GUI later. It also guarantees that your GUI will look terrible on all platforms and screen resolutions other than one. Many newbies usually go this route initially, and then most leave it eventually after gaining more experience with Swing as they run into its failings, weaknesses and limitations.
For a more complete answer, consider giving more specifics and in image (we can help with this) of your GUI layout requirements.

Java Swing: Resizing component in JFrame without affecting other components

I have a very simple game I'm creating as a novice project. It's based on an online card game called "Castlewars". There are two players, each with a tower which is affected by cards they and their opponent play. At the moment I have the basic framework of collections and classes I need to make the game operate at a very simple level, but I'm having problems displaying these effects to the user. I have the following code, which should update, amongst other things, two jLabels on a jFrame GUI (constructed in NetBeans 7.4) which represent the player's towers:
private void adjustScreen(){
System.out.println (Integer.toString(jLabel1.getSize().height));
jLabel1.setSize(100, (playerRed.getTower().currentHeight() * 2));
System.out.println(Integer.toString(playerRed.getTower().currentHeight() * 2));
System.out.println (Integer.toString(jLabel1.getSize().height));
jLabel2.setSize(100, (playerBlue.getTower().currentHeight() * 2));
jLabel5.setText(Integer.toString(playerBlue.getTower().currentHeight()));
jLabel6.setText(Integer.toString(playerBlue.getGold()));
jLabel9.setText(Integer.toString(playerRed.getTower().currentHeight()));
jLabel10.setText(Integer.toString(playerRed.getGold()));
if (TurnBlue){
jPanel21.setBackground(inPlay);
jPanel10.setBackground(outPlay);
}else{
jPanel10.setBackground(inPlay);
jPanel21.setBackground(outPlay);
}
}
When I run it, i get the following output:
(Initial label height) - 200
(adjusted Tower().height) - 100
(adjusted label height) - 100
Which suggests that what I'm trying to do is working on some level, but the actual labels visually stay the same size.
At start (the initiation of the game should have set the labels to half their visible size):
After a couple of plays - the Blue's new tower height is shown in the top left
I've played around with enabling and disabling the resize property, both on the label and on the frame, and I did once manage to get it to resize, but it then shifted the other components of the frame in an unfortunate way. As you can see, the "Cards" at the bottom do seem to resize themselves, although I am not explicitly instructing them to do this (maybe an effect of the text length in the "Cards"?) What am I doing wrong?
After re-reading the question I realized the answer is not really an answer to the question but more of a list of suggestions. Rather than deleting it, I'll leave it up in case someone can gain something from it :-)
"What am I doing wrong?"
Welcome to the world of "Why I should use Layout Managers"
There's an ocean of problems that may arise from null layouts and trying to set size and location to everything. Swing was made to be used with Layout Managers.
Some layout managers will respect preferred sizes and some wont.
Use the correct layout manager and make use of nested JPanel with different layout managers to get your desired result
Make use of EmptyBorder and vgap and hgap for empty space.
Don't set size or location to anything. Let the layout managers take care of that for you.
Go over Laying out Components withing a Container to learn the different layout managers.
EDIT
If you're using Netbeans GUI BUilder take a look at this answer for some help with how to use different layout managers using the design tool.

How to resize components when resizing a JFrame? Using Null Layout

I am using the NetBeans Designer to create a JFrame. Also it is worth mentioning I am fairly new to Java so I might not understand some things / do things correctly. The frame has about 100 panels, more buttons than I would even think about counting, about ~40 tables, basically most swing components the NetBeans designer provides are being used within the frame. Also for the main frame I am using Null Layout (in order to have a background image inside a JLabel). I know it is not recommended but it doesn't affect the general layout of things as I'm using panels/LayeredPane/TabbedPane for everything, each with it's own design (most of them on Free Design with no Layout specified - that's how I started, didn't know about Layouts and it would take ages now to rearange everything after using Grid Bag Layout for example).
Now getting to my problem, I need to be able to resize the frame and make it resize all components contained. I have to carry a presentation tomorrow of it and I just noticed it doesn't fit on smaller displays (and resizing it doesn't do it properly, it just hides components). I do not care much if it's just an improvisation / not the best approach to the problem as after the 15 min presentation I will probably never open it again.
Thanks.
I would try to go through all the components tree and try to set them smaller font and reduce all their bounds to some static %.
In other words for each component multiply x,y,widht,height to e.g. 0.75 and call setFont() passing derived font of 25% smaller.

Can I fix the width of JRadioButton?

I have sever JPanels which have to be ordered vertically. For that I want to fix the width of the JPanels. Because if they are too short in comparison with the windows width, they will go horizontally (one after another) and I do not want it. At the moment the width of the JPanel is not constant because the width of the JRadioButton (included into JPabel) is not constant. How can I make the width of the JRadioButton constant? At the moment it is not constant because of the label (which can be different).
If the aim is laying out panels in a particular fashion, you must be using a LayoutManager, rather than thinking of resizing indivudual components just for the layout.
By default swing uses FlowLayout, and thats why your panels are falling one after another as long as there is space in the "line" (and then they would flow to the next line)
Try using GridBagLayout, its not as complex as may tutorials/articles say. The one or two hours you spend learning this will be a great help.
It would help you to arrange your components is a very flexible manner.
Btw, how are you developing your UIs? If you are using a IDE (Eclipse+plugins or NetBeans) then the IDE should help you do the laying out (say, via GroupLayout).
If you are trying to hand code the GUI, then GridBagLayout is the best way to get "in control".

Categories

Resources