JScrollPanes inside GridBagLayout gets resized randomly - java

Here is a tough one for you guys :)
Basically I have a GridBagLayout with 2 columns : a list wrapped in a scrollpane in each one. The scrollpanes are stretched in BOTH direction.
If I progressively reduce the height of this panel (by dragging the window's edge), I see "random" resizing happening on the scrollpanes:
second one's width gets reduced when first's Hscroll bar appears
then, second one's width gets shrinked again for no reason...
If i don't wrap my components, don't get this behavior.
And if you replace the right hand list with a tree, it will behaves differently:
shrinking the window's height bellow 380-ich px, the tree gets resized...
If i don't wrap my components, the tree gets resized anyway if you keep resizing the window !
Do you guys have any idea what's going on ???
PS: the actual layout i am trying to build is more complex than this example. In the mean time i use SpringLayout to do what i want but it requires to much (not so beautiful) things to setup
protected static ListModel newListModel(int n) {
DefaultListModel lm = new DefaultListModel();
for (int i = 0; i < n; ++i)
lm.addElement("AAA");
return lm;
}
protected static JComponent createContentPane() {
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
gbc.gridx = 0;
pane.add(new JScrollPane(new JList(newListModel(12))), gbc);
++gbc.gridx;
pane.add(new JScrollPane(new JList(newListModel(4))), gbc);
return pane;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(createContentPane());
f.setSize(800, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setVisible(true);
}

I have an application with several ScrollPanes in a GridBagLayout, and they also exhibit sudden resizes when I resize the window. It seems that a jump occurs when the actual size of the ScrollPane steps over its "preferred size". I have found a workaround: set the preferred size to 1x1 pixels. If the component has positive weight and stretches BOTH, it will still occupy the whole cell, but will not jump. If you need several cells to resize in different proportions, you can set preferred size of another one to, say, 2x1 pixels.

this is basic principles of GridBagLayout, you forgot for define anchor, then you can be able to fix placed JComponent to the rellative Point, and I think that GridBagLayout complicated your GUI, this LayoutManager is better use for placing lots of JComponents to the one container,(without using nested layout)
for example
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderPanels extends JFrame {
private static final long serialVersionUID = 1L;
public BorderPanels() {
setLayout(new GridBagLayout());// set LayoutManager
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel1 = new JPanel();
Border eBorder = BorderFactory.createEtchedBorder();
panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 20;
add(panel1, gbc); // add compoenet to the COntentPane
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "60pct"));
gbc.gridy = 1;
gbc.weightx = gbc.weighty = 60;
//gbc.insets = new Insets(2, 2, 2, 2);
add(panel2, gbc); // add component to the COntentPane
JPanel panel3 = new JPanel();
panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
gbc.gridy = 2;
gbc.weightx = gbc.weighty = 20;
gbc.insets = new Insets(2, 2, 2, 2);
add(panel3, gbc);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
pack();
setVisible(true); // important
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() { // important
public void run() {
BorderPanels borderPanels = new BorderPanels();
}
});
}
}
to avoiding against to complicating simple things
use GridLayout (all JComponents have got same Dimmension on the screen)
use BoxLayout or BorderLayout

Related

GridBagLayout not lining up

I have 5 JPanels lined up vertically. Each JPanel is filled with the same elements, but different values (JPanel, JButton, JLabel). I want them to look like this:
Panel Button Label
Panel Button Label
Buts it's turning out like this
Panel Button Label
Panel Button Label
The spacing is a little off, but the code is exactly the same for each container. How can I fix this?
public class AnswerChoice extends JPanel {
private static final long serialVersionUID = 1L;
private AnswerButton button;
private JLabel answerLabel;
public AnswerChoice(String imageURL) {
setBackground(Color.RED);
setLayout(new GridBagLayout());
button = new AnswerButton(imageURL);
answerLabel = new JLabel();
answerLabel.setFont(new Font("Times New Roman", Font.PLAIN, 32));
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel = new JPanel();
panel.setBackground(Color.ORANGE);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.weightx = 0.2;
add(panel, gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 2;
gbc.weightx = 0.0;
gbc.insets = new Insets(0, 0, 0, 30);
add(button, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 3;
gbc.weightx = 0.8;
add(answerLabel, gbc);
}
}
The JLabel does have text in it but I set it somewhere else.
but the code is exactly the same for each container.
The layout is done independently for each container based on the components added to the containter. So the size of each component on the container matters. Each container doesn't know that you have 4 other containers.
I have 5 JPanels lined up vertically.
So then you need to create a single panel using a GridBagLayout and add all 15 components to that panel. Then all 3 columns will be sized based on the components in each of the 5 rows.
Or I see in your logic that you try to assign relative sizes to each of the 3 components as .2, 0, .8. In this case you could use the Relative Layout on each of the panels. Using the Relative Layout you would display the button at its preferred size and then use 0.2f and 0.8f as the contraints for the panel and label respectively.

GridBagLayout erratic resizing and displaying

I'm making a program that uses a GridBagLayout in a container. My primary use for this is to have two JPanels, which occupy 75% and 25% of the horizontal space of the window. For some reason though, the two panels look more like 90/10, and when resizing, the smaller one rapidly changes in size, between it's apparent minimum size, and what I believe is the desired 25%.
Here is the relevant code.
frmReedreadV = new JFrame();
frmReedreadV.setBounds(x, y, width, height);
frmReedreadV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmReedreadV.getContentPane().setLayout(new BoxLayout(frmReedreadV.getContentPane(), BoxLayout.Y_AXIS));
JPanel stretchyPanel = new JPanel();
frmReedreadV.getContentPane().add(stretchyPanel);
stretchyPanel.setLayout(new CardLayout(0, 0));
JPanel textAndUsers = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
textArea = new JTextArea();
textArea.setMargin(new Insets(2, 5, 5, 2));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
gbc.weightx = 0.8;
textAndUsers.add(scrollPane, gbc);
list = new FriendsList(listUpdate);
gbc.weightx = 0.2;
textAndUsers.add(list.frmUserList, gbc);
stretchyPanel.add(textAndUsers);
FriendsList is a JList contained in a JPanel.
There are other buttons and text fields in the main CardLayout content pane, but those shouldn't affect what is inside of this GridBagLayout, correct?
I made another copy of this JPanel as a standalone application, and it displays and resizes perfectly. See here:
JFrame frame = new JFrame();
frame.getContentPane().setLayout((new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 550, 600);
JPanel stretchyPane = new JPanel();
frame.getContentPane().add(stretchyPane);
stretchyPane.setLayout(new CardLayout(0, 0));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextArea text = new JTextArea();
text.setMargin(new Insets(2, 5, 5, 2));
JScrollPane panel1 = new JScrollPane(text);
FriendsList panel2 = new FriendsList(new Object());
c.fill = GridBagConstraints.BOTH;
c.weightx = .8;
c.weighty = 1;
panel.add(panel1, c);
c.weightx = .2;
//c.fill = GridBagConstraints.HORIZONTAL;
panel.add(panel2.frmUserList, c);
stretchyPane.add(panel);
frame.setVisible(true);
What could be causing the difference between the two, since I've replicated my original line by line into the copy?
The weightx and weighty properties might appear to act as proportional sizes, but that is not what they do. In fact they determine the distribution of extra space in the layout.
If you set everything to its preferred size by calling pack() on your JFrame, there will be no extra space. Which means the weightx and weighty properties have no effect while it's in that state.
Once the user starts resizing the window to be larger, there will be extra space, and only then will GridBagLayout consult the weightx and weighty properties to determine how to apportion that extra space to each column and row. Until then, it's entirely possible for a component with a small weightx to be wider than a component with a larger weightx, if their preferred sizes dictate it.
Hopefully this simple program will demonstrate this concept. Try using the mouse (or keyboard) to resize the window to be wider, and observe how each of the textfields grows:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridBagProportions {
static void buildAndShowWindow() {
JTextField small = new JTextField("small (0.8)", 5);
JTextField large = new JTextField("LARGE (0.2)", 30);
small.setMinimumSize(small.getPreferredSize());
large.setMinimumSize(large.getPreferredSize());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.left = 6;
gbc.insets.top = 6;
gbc.insets.bottom = 6;
gbc.weightx = 0.8;
panel.add(small, gbc);
gbc.weightx = 0.2;
gbc.insets.right = 6;
panel.add(large, gbc);
JFrame frame = new JFrame("GridBagLayout Proportions");
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
buildAndShowWindow();
}
});
}
}
So what can be done about it? Well, this is one layout scenario that GridBagLayout cannot do. I would try using a SpringLayout:
SpringLayout layout = new SpringLayout();
JPanel textAndUsers = new JPanel(layout);
SpringLayout.Constraints scrollPaneConstraints =
new SpringLayout.Constraints(scrollPane);
Spring scrollPaneWidth = scrollPaneConstraints.getWidth();
SpringLayout.Constraints listConstraints =
new SpringLayout.Constraints(scrollPaneWidth,
scrollPaneConstraints.getY(),
Spring.scale(scrollPaneWidth, 0.25f),
scrollPaneConstraints.getHeight());
layout.putConstraint(SpringLayout.EAST, textAndUsers, 0,
SpringLayout.EAST, frmUserList);
layout.putConstraint(SpringLayout.SOUTH, textAndUsers, 0,
SpringLayout.SOUTH, scrollPane);
textAndUsers.add(scrollPane, scrollPaneConstraints);
textAndUsers.add(frmUserList, listConstraints);
Notice that the creation of listConstraints specifies a width argument which is Spring.scale(scrollPaneWidth, 0.25f). This ensures the list is always one-fourth as wide as the scrollPane containing the JTextArea.
SpringLayout is tricky to use, in that you have to make sure to link the far edges of the layout container to child components explicitly, because SpringLayout won't grow to accommodate all the child components automatically. That's what the putConstraint calls are doing.

Component in JScrollView in GridBagLayout drawn too small

I've got a custom component, derived from JComponent. I'm placing that into a JScrollPane and that into a container using GridBagLayout. Now if the container gets too small, i.e. has to start displaying scroll bars, the component becomes tiny.
The example below renders as follows after starting the application:
But after resizing the frame a bit, it becomes this:
Here is the code:
import java.awt.*;
import javax.swing.*;
class SO26736343 extends JPanel {
#Override public void paintComponent(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(0, 0, getWidth(), getHeight());
}
#Override public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public static void main(String[] args) {
JFrame frm = new JFrame();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frm.getContentPane();
cp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
cp.add(new JButton("NW"), gbc);
cp.add(new JButton("N"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
cp.add(new JButton("NE"), gbc);
gbc.gridwidth = 1;
cp.add(new JButton("W"), gbc);
cp.add(new JScrollPane(new SO26736343()));
gbc.gridwidth = GridBagConstraints.REMAINDER;
cp.add(new JButton("E"), gbc);
gbc.gridwidth = 1;
cp.add(new JButton("SW"), gbc);
cp.add(new JButton("S"), gbc);
cp.add(new JButton("SE"), gbc);
frm.setSize(500, 500);
frm.setVisible(true);
}
}
I executed this on OS X 10.8.5 with Java 1.7.0_25.
I guess there might be something wrong with my component, but I don't know what. Shouldn't the layout make an attempt to display that component as large as possible, even if it can't be displayed at its preferred size?
I think you forgot to set the constraints when you are adding your custom panel and thus the behavior is unpredictable (or at least unexpected):
gbc.gridwidth = 1;
cp.add(new JButton("W"), gbc);
cp.add(new JScrollPane(new SO26736343())); // gbc missing here
If you want this panel fill all available space, then set both weightx and weighty properties to a value greather than 0 and add your panel using this constraint:
gbc.weightx = 1;
gbc.weighty = 1;
cp.add(new JScrollPane(new SO26736343()), gbc);
Screenshots
This sort of problem happens a lot when I use textfields in Swing. I always set the minimum size of the problem component to its preferred size :
myComponent.setMinimumSize(myComponent.getPreferredSize());
You can also set weight and fill on the gbc.
////////////
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
////////////
This prevents, when the scrollbars kick in, the collapsing effect. Most commonly happens in textfields and text areas.
http://blue-walrus.com/2011/04/swing-textfieldtextarea-collapse-on-dialog-resize/

Placing buttons in a specified location using swing in java

I am trying to learn how to make JAVA programs and I am working with Swing. I am trying to place a button in the top left corner of the window and it keeps going to the top center.
public void createGUI(){
JFrame frame = new JFrame("My Project");
frame.setDefaultCloseOperation(3);
frame.setSize(400, 350);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
addButtonGUI(panel, new JButton(), "test", 1, 1);
}
public void addButtonGUI(JPanel panel, JButton button, String text, int x, int y){
GridBagConstraints gbc = new GridBagConstraints();
button.setText(text);
button.setEnabled(true);
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 2;
gbc.weightx = 1.0D;
gbc.fill = 2;
panel.add(button, gbc);
}
What am I doing wrong or is there a better way to do this?
Please help
You need to set the layout of the JPanel to GridBagLayout to use GridBagConstraints:
JPanel panel = new JPanel(new GridBagLayout());
Also as you only have one effective 'cell' you need to use an anchor and set weighty for the JButton to allow movement in the Y-axis.
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weighty = 1.0;
Also I would set the fill setting to NONE:
gbc.fill = GridBagConstraints.NONE;
so that the button does not occupy the full width of the panel. (2 = HORIZONTAL fill).
instead of
addButtonGUI(panel, new JButton(), "test", 1, 1);
}
what would happen if you used
addButtonGUI(panel, new JButton(), "test", 0, 0);
}

Java-Swing: A problem using layout managers!

I'm working on a JDialog (by hand, no GUI builders) and I'm having a problem doing the layout.
I have this:
My problem is that I don't know what how to tell that JList (within a JScrollPane) to have a Maximum width, I used the setSize, setMaximumSize and nothing works! I need that JList's width to be the half of the picture's size.
Explain the layouts:
The "Gene Information" is a GridLayout 2x4, it's contained by a JPanel with BoxLayout, the +/- JButtons is a BoxLayout also, all what I said before is within a BoxLayout.
Now, the "Genes" JPanel is a GridBagLayout.
What can I do?
Thanks in advance!
PD: The other borders are just for seeign the boundaries of the components.
Source Code:
scpGenesList.setViewportView(lstGenesList);
pnlGeneInfo.setLayout(new GridLayout(4, 2, 10, 10));
pnlGeneInfo.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Gene Information"),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
lblGeneSymbol.setText("Symbol:");
lblGeneSymbol.setHorizontalAlignment(SwingConstants.RIGHT);
lblGeneChromosome.setText("Chromosome:");
lblGeneChromosome.setHorizontalAlignment(SwingConstants.RIGHT);
lblGeneStartPosition.setText("Start Position:");
lblGeneStartPosition.setHorizontalAlignment(SwingConstants.RIGHT);
lblGeneStopPosition.setText("Stop Position:");
lblGeneStopPosition.setHorizontalAlignment(SwingConstants.RIGHT);
pnlGeneInfo.add(lblGeneSymbol);
pnlGeneInfo.add(lblGeneSymbolValue);
pnlGeneInfo.add(lblGeneChromosome);
pnlGeneInfo.add(lblGeneChromosomeValue);
pnlGeneInfo.add(lblGeneStartPosition);
pnlGeneInfo.add(lblGeneStartPositionValue);
pnlGeneInfo.add(lblGeneStopPosition);
pnlGeneInfo.add(lblGeneStopPositionValue);
pnlGWASAddRemoveButtons.setLayout(new BoxLayout(pnlGWASAddRemoveButtons, BoxLayout.X_AXIS));
pnlGWASAddRemoveButtons.add(Box.createHorizontalGlue());
pnlGWASAddRemoveButtons.add(cmdGenesAdd);
pnlGWASAddRemoveButtons.add(Box.createHorizontalStrut(10));
pnlGWASAddRemoveButtons.add(cmdGenesRemove);
pnlGWASAddRemoveButtons.add(Box.createHorizontalGlue());
pnlGeneInfoButtons.setLayout(new BoxLayout(pnlGeneInfoButtons, BoxLayout.Y_AXIS));
pnlGeneInfoButtons.add(pnlGeneInfo);
pnlGeneInfoButtons.add(Box.createVerticalStrut(10));
pnlGeneInfoButtons.add(pnlGWASAddRemoveButtons);
pnlGenesPanel.setLayout(new GridBagLayout());
pnlGenesPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Genes"),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
GridBagConstraints ctrGenes = new GridBagConstraints();
ctrGenes.fill = GridBagConstraints.BOTH;
ctrGenes.gridx = 0;
ctrGenes.gridy = 0;
ctrGenes.gridwidth = 1;
ctrGenes.gridheight = 1;
ctrGenes.weighty = 1.0;
ctrGenes.weightx = 1.0;
ctrGenes.insets = new Insets(0, 0, 0, 10);
pnlGenesPanel.add(scpGenesList, ctrGenes);
GridBagConstraints ctrGenesInfoButton = new GridBagConstraints();
ctrGenesInfoButton.fill = GridBagConstraints.BOTH;
ctrGenesInfoButton.gridx = 1;
ctrGenesInfoButton.gridy = 0;
ctrGenesInfoButton.gridwidth = 1;
ctrGenesInfoButton.gridheight = 1;
ctrGenesInfoButton.weighty = 1.0;
ctrGenesInfoButton.weightx = 1.0;
pnlGenesPanel.add(pnlGeneInfoButtons, ctrGenesInfoButton);
contentPane.add(pnlGenesPanel);
pack();
Why not give the "Genes" panel a 2x1 GridLayout? That should ensure that both sides have the same size.
But actually, it would make more sense to me to give the list all space not taken by the controls, since those require a fixed amount of space while the list may benefit from all additional space it can get, if there are wide entries.
To that end, I would give the "Genes" panel a BorderLayout, put the list in the CENTER slot and the controls in the EAST slot.
Following #Michael Borgwardt's suggestion to let the list grow, you can use setVisibleRowCount() to produce a convenient initial panel size. If necessary, you can also examine the Dimension returned by getPreferredScrollableViewportSize(), which "computes the size of viewport needed to display visibleRowCount rows."
Without seeing all the code here it may be impossible to tell you what is wrong. One thing I would suggest if you have time is to take a look at MigLayout. You can use it with Swing & SWT and once you learn it is a pretty powerful layout manager IMHO.
Hope this helps, good luck.
It doesn't answer your question - but I've found that the JGoodies FormLayout to be more intuitive than the GridBagLayout. The library, as well as some examples, can be found here:
http://jgoodies.com/freeware/forms/index.html
I think the earlier solutions are all valid, and it is more of a coding preference in terms of which layout managers to use. Based on your requirement, here is a working one with standard layout managers only (Grid, GridBag and Border). Have fun, - MS.
import java.awt.;
import javax.swing.;
import javax.swing.border.*;
public class GeneDialog extends JDialog {
private String[] plusMinus = {"+","-"}, tfNames = {
"Symbol", "Chromosome", "Start position", "Stop position"},
listData = {"Gene01", "Gene02", "Gene03", "Gene04", "Gene05", "Gene06",
"Gene07", "Gene08", "Gene09", "Gene10", "Gene11", "Gene12"};
private JTextField[] gtField= new JTextField[tfNames.length];
private JList list = new JList (new DefaultListModel());
public GeneDialog (Frame f, String title) {
super (f, title, true);
Container cp = getContentPane();
cp.setLayout (new GridLayout(1,2));
JScrollPane listScrollPane = new JScrollPane (list);
listScrollPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Genes"),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
DefaultListModel lm = (DefaultListModel) list.getModel();
for (int k = 0 ; k < listData.length ; k++)
lm.addElement (listData[k]);
cp.add (listScrollPane);
cp.add (controlPanel());
pack();
}
private GridBagConstraints makeGBC (int inset) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets (inset, inset, inset, inset);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
return gbc;
}
private JPanel controlPanel() {
JPanel cp = new JPanel (new BorderLayout()),
bp = new JPanel (new GridBagLayout()),
tp = new JPanel (new GridBagLayout());
GridBagConstraints gbc = makeGBC (10);
for (int i = 0 ; i < tfNames.length ; i++) {
JLabel label = new JLabel (tfNames[i], JLabel.TRAILING);
tp.add (label, gbc);
}
gbc.gridx++; gbc.weightx = 1.0f;
for (int i = 0 ; i < tfNames.length ; i++) {
gtField[i] = new JTextField(12);
tp.add (gtField[i], gbc);
}
gbc = makeGBC (10);
for (int i = 0 ; i < plusMinus.length ; i++) {
JButton b = new JButton (plusMinus[i]);
bp.add (b, gbc);
gbc.gridx++;
}
cp.add (tp, "Center");
cp.add (bp, "South");
return cp;
}
public static void main (String[] args) {
new GeneDialog (null, "Genes").setVisible (true);
}}
Try setting the maximum width for both the JScrollPane and the JList that is within it.
I just want to tell that I share the same opinion as javamonkey79. Take a look at MigLayout, you'll love it, and from Java 7 on it will be standart java-onboard layout.

Categories