Can i combine Java layouts in same JPanel. I'm stuck with with placing my components on JPanel. It shoudl be like this: JLabel, JButton, JButton , JLabel and new line and same. I used BorderLayout but it wont go to the next row, keep adding components to same row and I need a new row. Ideal sit combined with cardlayout or some other good solution.
EDIT: Solved with GridLayout (0,4) It will do the job till i learn to use GridBaglayout. Thank you for trying to help me.
Yes you can combine layouts.
Using a JPanel you are able to embed other JPanels:
JPanel back = new JPanel(new BorderLayout());
JPanel rows = new JPabel(new GridLayout(3,3));
back.add(rows, BorderLayout.CENTER);
Without seeing your code though it's difficult to know exactly what you are trying to achieve!
Yes you can combine java layouts.
A common pattern I use is BorderLayout first on a frame. The central component expands out, while the other components shrink in. Inside these panels I might have a Flowlayout to show buttons evenly spaced horizontally on top.
Another common approach for forms is using a Gridbaglayout, then adding all the form elements at gridX and gridY positions. I then later can stretch and teak these cells using other constraints in the Gridbaglayout repetoire.
Can you add a screenshot so that we can see what you want to do?
Related
I am learning to use swing so please bear with me. Have a look at the attached screenshot:
These are JLabels within a JLabel within a JFrame. I would like to move these inner JLabel (which are randomly generated in size and color at runtime) to the very bottom as though they were books on a shelf. I know about Layout managers, though I can't quite seem to find the proper one to do what I want. This screenshot shows the result of specifying none, thus it should default to FlowLayout.
The inner JLabels are just .add()ed without any placement done. SetLocation appears to do nothing whatsoever.
Can you help me out?
If I were you I'd add put those JLabels inside of a JPanel with a boxlayout then align that with a border layout. Here is some code to help you out:
JLabel outer = new JLabel();
outer.setLayout(new BorderLayout(0,0));
/** Add inner JLabels here. The other you add them is the order they will appear from to right**/
JPanel bookshelf = new JPanel();
bookshelf.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));
//Add your jlabels to the bookshelf
outer.add(bookshelf, BorderLayout.SOUTH);
Here is a great tutorial on layout managers.
Also here is a UI I designed which is similar to what I think you want.
Hope this helps you out.
So I was trying to google how to set a default size to JButtons so that they don't grow as the JFrame is resized. I didn't see a setDefaultSize method but the closest one I could find that does a similar job is setMaximumSize(). However, it doesn't seem to work in my situation and I'm guessing it's because I'm using Grid Layout for positioning my buttons in the frame, here's a small piece of my code:
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
Here's a picture of what happens:
I would also like to have my buttons in the middle of the right panel when I'm resizing (just like they are now but a lot smaller). Any idea of how I can fix this? I'm assuming that I have to use another layout or something.
Thanks
EDIT: I modified my code to use BoxLayout but it does not seem to put the buttons in the middle. The X Alignment is working but Y Alignment is not doing anything:
ButtonA.setAlignmentX(CENTER_ALIGNMENT);
ButtonA.setAlignmentY(CENTER_ALIGNMENT);
ButtonB.setAlignmentX(CENTER_ALIGNMENT);
ButtonB.setAlignmentY(CENTER_ALIGNMENT);
ButtonC.setAlignmentX(CENTER_ALIGNMENT);
ButtonC.setAlignmentY(CENTER_ALIGNMENT);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
EDIT2: Fixed with vertical glue.
A GridLayout will always resize the components to fill the space available.
Try using a vertical BoxLayoutinstead. See the section from the Swing tutorial on How to Use Box Layout for more information and examples.
Encapsulate each JButton in a JPanel with a FlowLayout, and then add those FlowLayout JPanels to the rightPanel instead of the JButtons themselves. This will allow you to keep your evenly spaced buttons, but won't make them expand to take up the entire space that the parent container has available.
If you don't want them evenly spaced, but to be three consecutive buttons one after another top down, you can make the right panel have a BorderLayout, add a sub panel to the north area of the BorderLayout with the original GridLayout that the right panel had, and then add those FlowLayout panels containing the JButtons.
I'm creating a Java swing app and I'm having a real hard time getting elements positioned nicely.
How would I go about making the input boxes and combo boxes to say 30px high? Also, how would I go about making the right edge of all the text line up vertically and the left edge of all the input boxes line up vertically?
I have a main JPanel which is BoxLayout.Y_AXIS, and then I have 6 JPanels on the main JPanel. These 6 JPanels are set to BorderLayout and as you can see I have used WEST for the JLabels and EAST for the input fields.
Here's a snippet of how I'm creating the 1st panel which is on top of the main panel.
private JPanel getProtocolPanel() {
protocolNumber.setBorder(BorderFactory.createLineBorder(Color.GREEN));
protocolNumberInput.setBorder(BorderFactory.createLineBorder(Color.CYAN));
protocolNumber.setVerticalAlignment(SwingConstants.TOP);
protocolPanel = new JPanel();
protocolPanel.setLayout(new BorderLayout());
protocolPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
protocolPanel.add(protocolNumber, BorderLayout.WEST);
protocolPanel.add(protocolNumberInput, BorderLayout.EAST);
return protocolPanel;
}
Let me know if there is a better way to do this type of layout.
Thanks.
You are either going to have to nest layouts inside each other, or use a more complex layout (Spring, GridBag, Mig), or both to get the desired effect.
I'm building a grid filled with labels. One of them contains html-text and should resize to maximum format and be scrollable. I found how to add a JScrollPane but it stays one line height, I just can't find how to resize it even when I give it a size of 400x400 ...
Removing getViewport() gives the same result.
JPanel grid = new JPanel(new GridLayout(1,2));
// first cell of the grid
grid.add(new JLabel("title"));
// second cell of the grid, this should be the scrollable one
JScrollPane scroll = new JScrollPane();
scroll.getViewport().setSize(400, 400);
scroll.getViewport().add(new JLabel("<html>long<br>html<br>text</html>"));
grid.add(scrollVersion, BorderLayout.CENTER);
Any ideas ?
Thanks a lot ...
GridLayout does not respect preferred size of the components which it lays out. It aims to make all grid cells the same size. An alternative is to use GridBagLayout, however I personally would recommend ZoneLayout which (in my opinion) is simpler, just as powerful, and much more intuitive. With the cheatsheet you can't go wrong.
As a side note, BorderLayout.CENTER is a constraint used for BorderLayout and is not compatible with GridLayout. When components are added to the owner of a GridLayout, you need not provide constraints. Components are added left to right starting at the top left corner cell using GridLayout.
Replace your GridLayout with a GridBagLayout. With the correct set of constraints, it should work like a charm. And obviously, take a look at some examples, as GridBagLayout seems quite complex, but is rather simple with some examples.
All cells of the GridLayout are designed to have the same size, so if you want one to be bigger than teh othes you must use another LayoutManager, like the GridBagLayout that Riduel suggest.
Also if your JLabel is going to have more than one line i suggest you to replace it by an uneditable JTextPane o JTextArea
I'm new to Java awt, so I am having trouble with setting up panels. I have one giant panel, which needs to hold 3 panels inside (photo is attached at the bottom). One will go on top(1), second one will be in the middle(3), and third goes on the bottom(2). Any remaining space has to be divided equally between (1)/(3) and (3)/(2). Also, the middle panel (3) is a table, so GridLayout has to be used.
How can I achieve this?
Thanks in advance!
P.S. I've tried to draw it in MS Paint (http://i45.tinypic.com/mwejkk.jpg)
I don't understand all, I suggest :
Use swing, not awt, so use JPanel
A BorderLayout, with your giant panel (jpanel) in middle, a jpanel at west ; for this jpanel
a BorderLayout, or BoxLayout, or GridLayout and put inside your 1 2 3 panels.
... or use netbeans and matisse.
This will help you a lot. It's a Sun tutorial on BoxLayout. It describes the stacked layout that you appear to need, and also how to make invisible components to add gaps in the extra space you mentioned. For the middle pannel, put a GridLayout in that panel to do the things you need.