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.
Related
I am making a game, and i'm in the menu, however, I have to put one button. And, to make this, I created a JLabel with one image, and I used the MouseListener, but I have to prescribe the proportion of this JLabel, how can I do this? Thanks for the answer, and sorry my bad english.
Use a layout manager to control the position and size of the components. Take a look at Laying Out Components Within a Container for more details
Use a Border (like EmptyBorder) to affect the "padding" to the label, which will change it's overall size. Take a look at How to Use Borders for more details
Consider using an undecorated button instead of a JLabel. See How to Use Buttons, Check Boxes, and Radio Buttons for more details
JLabel j = new JLabel();
j.setSize(x,y);
where X is the width in pixels and Y is the height in pixels.
JLabel#setPrefferedSize is probably what you're looking for, but I recommend looking into Layout Managers that will set the size for you.
In my JPanel, I have 6 buttons laid out in a row (using FlowLayout as of now). The default size of the panel is enough to accommodate these buttons in one row. But, when the frame is resized it gets stuck at the size that is the sum of the minimum sizes of each button.
I need a layout manager that simply puts the buttons in a new row on re-sizing of the panel.
I'm pretty new to Java Swing so I apologize in advance if this is a trivial question.
MigLayout is by far the best layout manager I've ever used. Things that used to require nested containers and lots of hard to understand code can be done in a single container with simple to understand (and maintain) string constraints.
The flow layout is capable of your desired behavior (moving components into new row if they cannot fit). Check out the swing tutorial (run FlowLayoutDemo). You'll have to show us your source code to find out, whether there is some other constrain which prevents it.
FlowLayout does actually paint components on a new row, but the problem is that the preferred size of the panel doesn't change so in many cases you can't see the components (unless you happen to add the panel to the CENTER of a BorderLayout).
One solution is to use the Wrap Layout, which extends FlowLayout to recalculate the preferred size of the panel so that you see the buttons on a new row.
I'd like to achieve the following:
The first column 20% of the total width and the second the 80%. And it should be dynamic(ex. when i expand/shrink it should change accordingly like liquid layout in css)
MigLayout is your best friend: http://www.miglayout.com
The code would look like this:
JPanel panel = new JPanel(new MigLayout("wrap 2, fill", "[fill,20%][fill,80%]"));
panel.add(panel1);
panel.add(panel2);
For Swing you'd use a gridbag layout.
You have two columns, one row.
The gridwidth properties will be set so that the first column's is .2 and the second column is .8 or any set of numbers such that the first column's value is 1/4 the second column's value.
The gridheight for both columns should be the same.
You can experiment a bit with fill. If you don't mind space in your UI which is not filled with a component, then use none. If you want them to resize nicely but keep the .2 to .8 ratio then try horizontal and see if that keeps the proper ratio automatically.
If it doesn't then try setting weight to .2 for the first column and .8 for the second. Yo're trying to keep that.2 to .8 ratio no matter how big the JPanel is made by the user.
Let me know if you need more help.
If you are asking about desktop java application (Swing based), you can use GridBagLayout - a standard layout comes with JDK. Although it is somewhat hard to understand.
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
The best solution would be making your own layout - it's not that hard at all.
GridBagLayout is more feared than understood.
I had worked on an application with most screens having a header footer and a left navigation panel. I used Gridbag layout then and only that gave the required behaviour during resize. Had used visualcafe.
Your two column requirement seems to be a good one to start using Gridbag since only a couple of gridbag constraints will be affected.
I suggest to use a tool to build the UI if you have more rows/colums.
I’m making a sample application that uses MigLayout in a very cool way. Unfortunately after reading through the quickstart and the whitepaper I still have questions and can’t do my desired layout. The sample application lets you add/remove games which are basically an Info Panel + JLabel. The layout should have two rows, one for the info panel and the other one to the JLabel.
Layout:
Row 1 (Info Panel) : [grow][grow][grow][grow]
Row 2 (JLabels) : [grow][grow][grow][grow][/list]
Here is an image so you guys can see clearly:
So when I add a Game the layout should shrink the other to fit, like on this image:
And when I delete the layout should grow the remaining one:
But it’s not working with the given layout info, can you guys give me a hand? Also the shrinking JLabel should be handed by me, since it can’t resize automatically???
Sounds like a simple GridLayout will do the trick.
Yes, you will need to do custom paint to resize the image as the space available to it changes. This means you will probably need to use a JPanel and draw the image manually so you can scale the image on the fly.
OK - I give up.
How can I set width and height of a JFace ListViewer?
This should be self-evident but can't seem to find anything and there's no obvious way of doing it looking at the methods of the class.
Tried this with no luck:
myListViewer.getControl().setSize(1000, 1000);
Any help appreciated.
To do this you can set a layoutdata to your List associated with the ListViewer
myListViewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
to resize the control to fill the cell horizontally and to fit the remaining horizontal space.
You don't set the size explicitly. you use layouts instead. Layouts manage the size and position of your widgets in a container. Read this eclipse article that will help you understand SWT layouts.