Grid Layout view Java Swing component position - java

I have problem using Grid Layout in Java Swing. I create Panel and add GridLayout with 4 columns and 2 rows.
I try to add JButton inside it, but the JButton stretch the width.
Look this image :
I want create JButton position like this, because I want to make image gallery using Java Swing.
Look this image :
Any idea? Thanks before :)

Use GridBagLayout and specify GridBagConstraints. It will help you to render components as you want
Kindly refer GridBagLayout

You can try the layout http://java-sl.com/tip_columns_flow_layout.html
It's kind of Win Explorer layout when components flow to fill columns to available width.

Related

Keeping a component in a fixed position within a BoxLayout

I have a JPanel that uses a horizontal Box layout and contains a JLabel that I would like to keep in the exact same position as other components within the JPanel are setVisible(false). Currently, the JLabel moves to the left as other components become invisible.
What's the easiest way to go about this?
EDIT: Pics added
So this is what the JPanel look like with all components visible
When I set the three JTextFields on the right to invisible, the JLabel set to text X moves to the left like this:
But I would like it to stay where it was like this:
EDIT2: I'm actually using Netbeans GUI editor's Free Design for this particular JLabel. I'm sorry for the mistake - I've been using a lot of BoxLayouts recently and I got confused!
Currently, the JLabel moves to the left as other components become invisible.
Yes, layout managers are designed to only work with visible components. I'm not sure if any of the default layout manager will work, but I would look into using the GridBagLayout, since this layout is based on a grid structure so as long as you have components in that grid on another row the label should not shift.
Otherwise, you could dislay the "other components" in a panel using a CardLayout. Then instead of making the components invisible, you swap the panel with an empty panel.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Edit:
Based on your picture the easiest solution is to use "glue":
panel.add(Box.createHorizontalGlue);
panel.add(xLabel);
Now the label will always be displayed at the far right of the panel. Read the tutorial on How to Use BoxLayout for more information about "glue".

How to align buttons using a Grid Layout?

Is it possible to use the Grid Layout for just your text area within the program and have your buttons outside of the grid layout for them to align how you want them ??
I'm struggling to align my buttons and I was wondering if this sort of problem is possible to solve using the grid layout or would I have to change my layout all together to see the results I'm looking for.
I can suggest you to use extra Composites with own layouts (e.g. one composite for text area with fill layout and other one for buttons with grid layout).
You can read this good article about SWT layouts.

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).

java images to grid layout

Can you add an Image to a GridLayout?
Add the ImageIcon to a JLabel and add the label to the panel.
Again, using labels and Icons is discussed in the Swing tutorial. Take the time to read it!

Fixing a swing component's height within a Box layout

Good morning,
I am making a GUI thanks to javax.swing.Box class
Inside the panel:
JLabel
JTable with fixed height
JLabel
JTable with automatic height
I tried everything to fix the first JTable height but without any success.
I dedicate a Box.createHorizontalBox() for each component of the above rows and then I add them to the Box.createVerticalBox().
Instead of getting the first result I get a layout where both JTable has a automatic height, and I'd prefered the first JTable to have a fixed height...
Thanks for any answer,
Cheers
I found a solution and I shouldn't have annoyed you with such a silly problem:
For each horizontal box I created, I added an horizontal strut of 10 pixels to show a kind of padding. Thoses struts were the firsts in the rows and it was automaticly taken as the "height reference" for the box layout building, but I'm new to awt/swing layout so I may be mistaking saying that.
I removed those struts and inserted a vertical box which contained a horizontal struts of 10 pixels. It did the job.
Anyway, thanks for your time Markus & Michael, I'll dive deeper in sun's tutorial when my boss will let me the time to do so
Cheers
You can change to row height for example by calling
TableColumn column = table.getColumnModel().getColumn(0);
column.setPreferredWidth(150);
//set all rows height
table.setRowHeight(20);
//set specific row height
table.setRowHeight(2,50);
The table size you can update by calling
setPreferredSize(Dimension preferredSize)
You also have to decide, which layout the panel shoul have. Did you set a layout?
How about showing us the actual code?
It sounds like you're not using layout managers correctly. You should probably use a BorderLayout with the "automatic" table in its CENTER position and the rest inside a second panel in the NORTH position, with that second panel using either a Boxlayout or a FlowLayout.
Sun has a very good tutorial on using Layout managers that can probably help you a lot.

Categories

Resources