Using BoxLayout manager with JLabels in java - java

Hey all I am having some trouble trying to get my jlabels to line up the way I want them to. I am using 3 panels (Title Panel, Display Panel, and Button Panel) Inside of my DisplayPanel I have a JtextField, 3 jlabels and the next thing I want to have happen is for the rest of my JLabels which happen to be ImageIcons to be in a set location inside of my panel, which is in boxLayout.
MasterOffense1 = new JLabel(Mastery1);
MasterOffense1.setLocation(400, 100);
MasterOffense1.setSize(25, 25);
MasterOffense1.setToolTipText("<html>"+"Double-Edged Sword<br> Melee- Deal an additional 2% damage and receive an additional 1% damage<br> Ranged- Deal and additional 1.5% damage and receive an additional 1.5% damage"+"</html>");
DisplayPanel.add(MasterOffense1);
MasterOffense2 = new JLabel(Mastery2);
MasterOffense2.setLocation(400, 130);
MasterOffense2.setSize(25,25);
MasterOffense2.setToolTipText("<html>"+"Fury<br> Rank-1: +1.25% Attack Speed<br> Rank-2: +2.5% Attack Speed<br> Rank-3: +3.75% Attack Speed<br> Rank-Max: +5.00% Attack Speed"+"</html>");
DisplayPanel.add(MasterOffense2);
There is code for the 2 JLabels with ImageIcon inside them and they keep showing up right below each other. I have no idea how to make this happen and I am completely stumped..
Any help would be appreciated. Thanks!

There is code for the 2 JLabels with ImageIcon inside them and they keep showing up right below each other.
Then is sounds like you are using a vertical BoxLayout.
If you want the labels to be displayed horizontally, then you can add the labels to a panel and then add the panel to your "displayPanel".
That is you can nest panels that use different layout managers to get your desired layout.
Also, use standard Java variable names. Variable names should NOT start with an upper case character.

Related

Java JPanel Layout

Im new in Java Swing, and want to make my layout, but can't do this
Look Now :
Look I want :
Code Now :
JPanel MainPanel = new JPanel(new GridBagLayout());
JLabel MoneyLabel = new JLabel(MoneyIcon);
MoneyLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyLabel.setText("Money:" + CarMain.Money);
JLabel MoneyClicksLabel = new JLabel();
MoneyClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyClicksLabel.setText("Money Clicks: " + CarMain.MoneyClicks);
JLabel BoxesLabel = new JLabel(BoxLv9_10Icon);
BoxesLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesLabel.setText("Boxes: " + CarMain.Boxes);
JLabel BoxesClicksLabel = new JLabel();
BoxesClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesClicksLabel.setText("Boxes Clicks: " + CarMain.BoxesClicks);
MainPanel.add(MoneyLabel);
MainPanel.add(MoneyClicksLabel);
MainPanel.add(jbtnMoney);
MainPanel.add(BoxesLabel);
MainPanel.add(BoxesClicksLabel);
MainPanel.add(jbtnBoxes);
This is simple example of, what i want, becouse i'm building ingame shop, with 13 labels like these, in each tabbedpane window. How can i make it look, like in second picture, what I want?
Im new in Java Swing, and want to make my layout, but can't do this
Probably no single layout can suit everyone's needs. But combining several layouts can usually handle most scenarios.
From the image you showed in the question. There is no need to write your own layout. You can always use sub panels to hold your components and set a specific layout for each sub panel to handle what you need for those individual areas.
The reason for the alignment in your first attached image is because:
JPanel uses FlowLayout as its default layout. Hence all the components added will appear in a linear fashion and tries to fill up the row as much as possible the panel's width can hold. Once exceeded the panel's width, the components will be pushed to the next row.
If you want to achieve the alignment in the second attached image:
You may create a main panel to contain several sub-panels (see image below).
The red box is your main panel and you may continue to use the default FlowLayout.
Then add your components into sub-panels (orange boxes) before adding it to the main. You may then use BoxLayout, FlowLayout or even GridBagLayout for the sub panels (orange boxes).
Artis Uljanovs, at night after work i will give a look at this to help you.
I recommend you already to read the following: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
You need some foundations on Java Layouts.

Why are all my labels spaced by like 4 tabs while being boderLayout WEST?

I have a JPrame and it it set to borderLayout. Then i created another JPnael which uses BoxLayout. Now i have added labels(which contain text) and textFields to the JPanel. I have also setLayout of JPanel to WEST. Now i dont know why, but I have two problems with what is being displayed.
1) The labels are all indented 4-5 tab spaces, this is so random I dont even know why it is tabbed. All the labels in this panel are like this. Please note that this only happens if I add more stuff to the JPanel. If I only have 1 label in the JPanel it is correctly alligned to the west corner of the screen. Does anyone know why they get indented when i add more elements to the JPanel?
2)The Textfields size are huge, as in they take up many lines. I have set the size of the text fields only to 20. Yet they take up like 5 lines. Why is it not just a single line?
Im sorry guys, I have been trying to fix this for the past 2 hours and dont know what is causing this issue. I would post code, but this is an assignment and I dont feel comfortable posting it on to the internet. I hope you understand.
Just to make things clear, I have a JPanel for example called "aPanel" which is set to BorderLayout then I have another JPanel called 'subPanel' which uses boxlayout and set the layout of that to be west. After this I add stuff to this 'subPanel' expecting the elements to stack over eacher other towards the left border of the JFrame.
1) Read the section from the Swing tutorial on Fixing Alignment Problems. When you add different components to the panel they may have different "X alignment" so you get a different layout than you expect.
2) The box layout will expand to fill the available space. So you need to override the getMaximumSize() method of you text fields to return the preferred height of the text field and the maxiumum width of the text field.
Edit:
Just read the last part where you mention a FlowLayout. If this is what you are using then read the FlowLayout API. It has a parameter that controls left/center/right alignment of components added to the panel.

Java swing help needed

I need to build a GUI in java with the following format:
Without providing any large blocks of code I want to know how to get started.
After reading countless info on swing I am utterly confused. I'm wondering if I would need multiple Jpanels / frames or a gridlayout or flowlayout. Also a very general idea of how to get started on the GUI would help. I know what components I need (list, label etc..) and have example code to help. The data I will load myself.
I'm assuming I will need a separate class for each component and maybe only one for labels to make it easier to debug.
You will need to mix containers and layouts, knowing what properties different layouts have, and different parts of those layouts, and the different options of the layouts will help a lot in knowing how to nest components together to get what you want.
Without providing code that shows this, I would do the following container/layout nesting:
The root panel could have a BorderLayout, with two JPanels on the east and west sides of it.
West most JPanel could have a BorderLayout, with the JList being in the center so it expands and takes up as much room as possible, and the 4 labels and 4 text fields could be in another panel which has a GridLayout of 4 rows by 2 columns.
The far east JPanel could have a BorderLayout as well, with a JPanel in the north that has a GridLayout of 3 rows by 1 column, where each of those rows has another panel of a FlowLayout. The text area or whatever it is could be in a JPanel in the center position, and you could place the bottom text field and label in a JPanel with a FlowLayout in the south position of the far east JPanel.

java border gui

I am not good with GUIs or User Interfaces in Java.
Would a Border or JPanel be good for something like the image below?
So, what would be the best option for me? Thank you.
Read the section from the Swing tutorial on Using Layout Managers. You can easily nest panels to get the desired effect.
Maybe start with a BorderLayout. Then you can add a panel that uses a GridLayout, which contains all your image tiles, to the CENTER of the BorderLayout. Then you can add the scrollpane containing the text area to the SOUTH. Then you can create another panel to add to the EAST.
Be creative and experiment.
You can make 4 seperate panels for a border, using BorderLayout.NORTH,BorderLayout.EAST,BorderLayout.SOUTH,and BorderLayout.WEST, This is the easiest way in my opinion.
By the way, in the top right of your picture, where you wanted the information panel, you should put an information LABEL (JLabel) instead, because they hold text. JLabel topRight = new JLabel(); then set the text, position, etc.
p.s. to erase the borders around every tile (if you want to do so), use setBorderPainted(false).

How do I layout a panel on the top of my Dialog so that it has two buttons?

I need to layout a panel on the top of my Dialog so that it has two buttons (Save and Cancel).
I want the save to be on the left and Cancel to be on the right side.
I've created a JPanel using the MigLayout and docked it to the north of the content pane, and can't for the life of me figure out how to add the two buttons to it so that they appear as I want them. Docking them within the panel seems to get rid of all padding in the dialog (which looks terrible).
Any help would be greatly appreciated.
As an aside, you should probably not be dictating which button is on the left or right. That's one of the way cool things about MiGLayout (platform independence, even on things such as where the cancel button should go).
p.add(cancelButton, "tag cancel");
p.add(okButton, "tag ok");
Now the buttons will appear in the correct order, based on platform.
Here's an article with code doing what you are going for. I strongly recommend avoiding trying to force the size of components like buttons (these really should come from the platform look and feel). Also, docking is fine if it makes sense to do so, but I rarely find it to be necessary. Instead of building a totally separate panel for your buttons, just span the row that contains the buttons - much cleaner, and you don't wind up with all of the nested panels.
It's hard to break from the border layout technique of nested panels, but once you get the hang of it, MigLayout is a dream. BTW - I understand that there are times where you might want to build up the button panel in a library - if that's the case, then separate panels may make sense (although you could also have the library add a button row to an existing panel, instead of returning a panel that you then add to the layout).
Doh, always happens as soon as you ask a question, the answer pops out:
JPanel buttonPanel = new JPanel(new MigLayout("fill","[50][50]",""));
buttonPanel.add(saveChangesButton);
buttonPanel.add(cancelButton, "align right");
getContentPane().add(buttonPanel, "dock north");
Note that the content pane is using the MigLayout too.

Categories

Resources