Java Applet, GridLayout stretching TextFields Horizontally - java

I'm writing a Java Applet and on one of my CardLayout Panels, the GridLayout within the BorderLayout.CENTER is streching the TextFields horizontally and vertically (this is ok because I can set vertical spacing between the parts of the grid). Can I stop the TextFields from being so wide and can I center the text within them?

Consider nesting the JTextField-containing JPanels inside of another FlowLayout using JPanel. This way the FlowLayout using JPanel is the one that gets stretched while the JTextFields are sized based on what you set the int col property to be. Note that this will fail miserably if the nesting FlowLayout using JPanel is smaller than the preferredSize of its child JPanel.

Related

How to get JButton below the JTextFields

I have created a GridLayout for my GUI to show 6 rows and 4 columns. I am trying to get the two buttons I have "Calculate" and "Exit" to show in the 6th row. I only have two pane.add of four for row 5 (which is the weight label and textfield). I'm just trying to get the calculate and exit buttons to be on their own row. I tried to do pane.add(); to fill the gap and couldn't get it to work. What am I missing so that I can get the buttons on their own row?
basically I have
pane.setLayout(new GridLayout(6, 4));
pane.add(score1L);
pane.add(score1TF);
pane.add(weight1L);
pane.add(weight1TF);
pane.add(score2L);
pane.add(score2TF);
pane.add(weight2L);
pane.add(weight2TF);
pane.add(score3L);
pane.add(score3TF);
pane.add(weight3L);
pane.add(weight3TF);
pane.add(score4L);
pane.add(score4TF);
pane.add(weight4L);
pane.add(weight4TF);
pane.add(weightAvgL);
pane.add(weightAvgTF);
pane.add(calculateB);
pane.add(exitB);
Possible options:
Add empty JLabels to fill in any gaps.
Use mixed layouts -- nest JPanels, each using its own layout and add components to them. For example, the overall JPanel could use BorderLayout. The textfields/labels could be in a GridLayout or GridBagLayout using JPanel added BorderLayout.CENTER to the main JPanel. The JButtons could be held in their own GridLayout using JPanel that is then added BorderLayout.PAGE_END to the main JPanel.
Use a layout manager that allows for more complex layouts such as a GridBagLayout, or better perhaps, a 3rd party layout manager such as MigLayout.
Perhaps the best of all --use a JTable to hold your data grid, add it to a JScrollPane, and then place your buttons below the JScrollPane.

How to resize JPanels individually?

I am trying to design a simple gui interface. The frame consists of a main panel which contains another four panels "Staff Details", "Job Details" "Photo" and buttons. The output is as follows:
I used gridLayout for all the panels except for the "buttons panel" which has borderLayout.
Now I want to reduce the height of the bottom two panels so it won't have the extra space. How do I do that?
Here is the code for the "photo" panel and "button" panel. Both panels now have flow layout.
photoPanel.setPreferredSize(new Dimension(300,70));
photoPanel.add(browsebx);
photoPanel.add(browseBtn);
btnPanel.setPreferredSize(new Dimension(300,70));
btnPanel.add(addBtn);
btnPanel.add(editBtn);
btnPanel.add(deleteBtn);
btnPanel.add(cancelBtn);
Your main panel is using a GridLayout, which means all four panels will be the exact same size. Don't use a GridLayout.
Instead maybe you can use a GridBagLayout. This allows your to add each panel to a specific grid, but each panel will maintain its preferred size as determines by the layout manager used on each of the individual panels.
Read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples.
Remember each of the 4 child panels can use a different layout manager if desired. For example you would probably want to use a FlowLayout for the buttons panel.
you can use this.
jPanel.setPreferredSize(new Dimension(x, y));
jPanel.setMaximumSize(new Dimension(x, y));
jPanel.setMinimumSize(new Dimension(x, y));

Make buttons unresizable

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.

How do I limit vertical JDialog resizing to a single component within the dialog?

I have a JDialog that consists of two JPanels, one above the other. Currently, when I resize the JDialog only the bottom panel resizes in the vertical direction. However, I only want the top panel to resize. The only component that the top panel contains is a JScrollPane, so I want any vertical resizing to result in an increased/decreased view of the top panel's content. What is a good way to do this?
Thanks in advance!
elise
, I only want the top panel to resize
dialog.add(topPanel, BorderLayout.CENTER);
dialog.add(anotherPanel, BorderLayout.SOUTH);
This is a job for the proper LayoutManger. Here is a good link that explains LayoutManagers visually and does it quite well.

Java Swing elements sizing and positioning issue

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.

Categories