JScrollPane in GridBagLayout affecting size of application - java

I've been practicing Java GUI development and on one of my projects I'm using a GridBagLayout with various controls such as JLabel, JComboBoxe, and a JTable encased in a JScrollPane.
However, I've noticed the JScrollPane seems to affect my JPanel where unless my height is a certain amount (522 px or greater according to .Pack()) my JComboBoxe seem to "compact" and the width will be set to fit the text instead of the size I specified (200 px).
So my question is what is causing this and how would I fix it so the JFrame's height can be smaller without causing my JComboBoxes to behave like that.
Screenshot of what my app looks like before/after resize:
Current code for my application:
package gui;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingConstants;
public class MainWindow {
public static void main(String[] args) {
MainWindow main = new MainWindow();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JComboBox<String> cmbTeam, cmbPosition;
JLabel lblTeam, lblPosition, lblResults;
JTable table;
JSeparator sep1,sep2;
int rowCount;
GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(new GridBagLayout());
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 0;
lblTeam = new JLabel("First Label");
panel.add(lblTeam, gbc);
gbc.gridx = 1;
lblPosition = new JLabel("Second Label");
panel.add(lblPosition, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
cmbTeam = new JComboBox<>(new String[]{"Data1","Data2","Data3"});
cmbTeam.setPreferredSize(new Dimension(200,25));
panel.add(cmbTeam, gbc);
gbc.gridx = 1;
cmbPosition = new JComboBox<>(new String[]{"Data1","Data2","Data3"});
cmbPosition.setPreferredSize(new Dimension(200,25));
panel.add(cmbPosition, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
sep1 = new JSeparator(SwingConstants.HORIZONTAL);
panel.add(sep1,gbc);
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
table = new JTable(new String[][]{{"AA","AB","AC"},{"BA","BB","BC"},{"CA","CB","CC"}}, new String[]{"Col1","Col2","Col3"});
table.setFillsViewportHeight(true);
panel.add(new JScrollPane(table),gbc);
gbc.gridy = 4;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
sep2 = new JSeparator(SwingConstants.HORIZONTAL);
panel.add(sep2,gbc);
rowCount = table.getRowCount();
lblResults = new JLabel(rowCount + " results found.");
gbc.gridy = 5;
panel.add(lblResults, gbc);
frame.setTitle("GridBagLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
frame.pack();
}
}
EDIT: I fixed the height issue by setting a minimum dimension for each JComboBox, but that white space that's generated from .pack() is still not fixed. I also noticed if I resize the window vertically and make it smaller the table's width seems to decrease by a single pizel. Maybe that has something to do with it?

what is causing this and
In general, when there is not enough space to display a component at its preferred size in a GridbagLayout the component will snap to its minimum size. I'm not sure why changing the height is causing the preferred width to change but somehow this is resulting in the combo boxes being displayed at their minimum width.
how would I fix it so the JFrame's height can be smaller without causing my JComboBoxes to behave like that.
Take advantage of the default BorderLayout of the frame. Add your components separately to the frame:
//frame.add(panel);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(table));
frame.add(lblResults, BorderLayout.SOUTH);
Of course you will need to tidy up the rest of the code since you no longer add the scrollpane and label to the panel.
You will still see the combo boxes shrink when the width is decreased.
Edit:
but that whitespace that's generated from .pack() is still not fixed.
JTable has a hardcoded preferred size for the "viewport". You can override this by doing something like:
table.setPreferredScrollableViewportSize(table.getPreferredSize());

That is probably because Scollpane tries to fill entire space. The setPrefferedSize sets, as you may notice from its name, not strict size. Methods setMaximumSize and setMinimumSize set strict limitations (in fact not, but almost :) ). Since that, add these 2 rows of code:
cmbTeam.setMinimumSize(new Dimension(200,25));
cmbPosition.setMinimumSize(new Dimension(200,25));

As camickr said, a GridBagLayout will revert all components to their minimum sizes whenever there isn't enough room for every component's preferred size.
An easy solution is to make the minimum size the same as the preferred size:
// Make sure these lines come AFTER you've set the preferred sizes.
cmbTeam.setMinimumSize(cmbTeam.getPreferredSize());
cmbPosition.setMinimumSize(cmbPosition.getPreferredSize());

Related

JEditorPane is blocking the divider of a JSplitPane

In my application I'm using a JSplitPane to devide the GUI into two sides. Every side will contain some components, one of them being a JEditorPane. The components are placed horizontally next to each other. All components should have a fixed size exept for the JEditorPanel. This should take all the available horizontal space. So, if the divider of the splitPane is moved the JEditorPane should change its width. My current implementation works fine if the divider is moved in a direction where the with of the JSplitPane increases. However, it is not possible to move the divider in the other direction. Below is a minimalistic example of my problem. In the example I'm using a GridbagLayout. However, this problem remains also for other layout manager. Also interesting is that this problem seems to be unique for the JEditorPane. The implementation works fine when the JEditorPane is replaced, for example by a JButton.
Here is my code:
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panelRight = new JPanel();
JPanel panelLeft = new JPanel();
panelRight.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton b = new JButton("button");
c.gridx = 1;
c.gridy = 0;
panelRight.add(b, c);
JEditorPane ep = new JEditorPane();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
panelRight.add(ep, c);
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight);
sp.setResizeWeight(0.5);
f.add(sp);
f.setVisible(true);
}
}
Does anyone have an idea why this is happending and/or how to fix it?

Java LayoutManagers with JPanels

I'm really struggling with creating a complicated layout. Here is a picture of what I want in the end:
(source: fbcdn.net)
I've attempted to divide and conquer, creating small panels and putting those in other panels. At first I figured a borderlayout for the main container panel which is in the initial JFrame, with a bunch of GridBagLayouts for the details in those panes. When that wasn't working, I figured I would try my hand at an all out GridBagLayout. Right now my goal is to get the top group of character and location panels/cards to be displayed in their correct aspect ratio (about 4:3) and have them resize correctly (maintaining aspect ratio as best as possible) as the window is resized. What I am getting is a super small square panel for each card when the window first comes up. I want them to start in the aspect ratio (4:3) to begin with.
Am I going about this correctly?
Should I stick with all GridBagLayouts?
If not, what combinations of layouts do you see that may work?
Lastly and probably most importantly, how come they do not start out with the correct gridwidth or gridheight?
It would be nice if all the cards maintained a 4:3 or 3:4 aspect ratio when the window is near a 4:3.
public class OverPower {
public static void main(String[] args) {
JFrame table = new JFrame();
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.setLayout(new BorderLayout());
JPanel contentPane = new OppCharsPanel();
table.setContentPane(contentPane);
table.setLocationByPlatform(true);
table.pack();
table.setLocationRelativeTo(null);
table.setVisible(true);
}
}
public class OppCharsPanel extends JPanel {
public OppCharsPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(20, 15, 20, 15);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 4;
gbc.gridheight = 3;
gbc.weightx = 4.0;
gbc.weighty = 3.0;
//back row
gbc.gridx = 4;
gbc.gridy = 0;
JPanel oppHomebase = new JPanel();
oppHomebase.setBackground(Color.RED);
this.add(oppHomebase, gbc);
gbc.gridx = 8;
gbc.gridy = 0;
JPanel oppReserve = new JPanel();
oppReserve.setBackground(Color.RED);
this.add(oppReserve, gbc);
//front row
gbc.gridx = 0;
gbc.gridy = 3;
JPanel oppBattlesite = new JPanel();
oppBattlesite.setBackground(Color.RED);
this.add(oppBattlesite, gbc);
gbc.gridx = 4;
gbc.gridy = 3;
JPanel oppChar1 = new JPanel();
oppChar1.setBackground(Color.RED);
this.add(oppChar1, gbc);
gbc.gridx = 8;
gbc.gridy = 3;
JPanel oppChar2 = new JPanel();
oppChar2.setBackground(Color.RED);
this.add(oppChar2, gbc);
gbc.gridx = 12;
gbc.gridy = 3;
JPanel oppChar3 = new JPanel();
oppChar3.setBackground(Color.RED);
this.add(oppChar3, gbc);
}
}
Start by breaking down each layout section into it's individual requirements and focus on those needs, for example...
Area #1
Okay, so basically, this has three main components (each coloured section is it's own panel), which could be used with a BorderLayout
Area #2
So here, you have another three areas/panels, because of the different requirements for vertical space, I might be tempted to use a GridBagLayout, this would allow you to define more space to the two top components then the bottom one...
Area #3
So, again three main areas. Here I'd be tempted to use a BorderLayout again. For the indivdual containers, I might use a GridBagLayout or Rob Camick's WrapLayout (for laying out the images within each of the three areas)
Scaling
Don't worry about the layout manager trying to maintain the aspect ratio, give enough information to the layout manager so that it make better choices, overriding the getPreferredSize and getMinimumSize methods of the image components and providing appropriate sizing hints. Within these image components, I would make sure that the image is scaled to the proper aspect ratio and rendered correctly

Line wrapped JTextArea using too much space with GridBagLayout

I've been trying to get a scrollable JTextArea to appear to the right of a JList where the JTextArea takes up most of the space but leaves a good portion for the JList (3/4 of the frame for the JTextArea, 1/4 for the JList). The problem is that the weights of the constraints seem to be ignored when LineWrap on the JTextArea is set to true.
Without line wrap, the JTextArea takes up about 3/4 of the frame and the JList takes up the other 1/4, which is what I want. But when line wrapping is turned on, the JTextArea attempts to take up as much space as possible and crushes the JList down to the width of the strings it contains. The interesting thing is that this only appears to happen once you resize the window (looks fine initially), and if you type anything into the JTextArea, it slowly expands, pushing the JList back. This has been confusing me to say the least.
I made a screenshot of what it looks like without line wrapping and with line wrapping after resizing the window:
Here's some example code showing you what I mean. Remove the line wrap statements and it'll look fine, but keep them there and you'll see that upon resizing, the JList is squished.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
JTextArea area = new JTextArea();
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> list = new JList<String>(model);
JScrollPane scroll = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
for (int i = 0; i < 5; i++)
model.addElement("Example" + i);
area.setLineWrap(true);
area.setWrapStyleWord(true);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.weightx = 0.3;
panel.add(scroll, c);
c = new GridBagConstraints();
c.gridx = 1;
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.weightx = 0.1;
panel.add(list, c);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
}
}
You can use ipadx to define minimal possible width
c.weightx = 0.1;
c.ipadx=desiredPixelsWidthForTheList;
panel.add(list, c);
UPDATE
Also you can place both in a JSplitPane and define desired proportions.
The short answer is that the text area gets more width than you want because its preferred width is much larger than that of the list.
Keep in mind that weightx doesn't determine what percentage of space a component will occupy but the proportional amount of space that's added to or taken away from the component's size. In this case, for example, the "extra" space is the difference between the frame's width (or more accurately, its content pane's width) which you explicitly set to 500 and the combined widths of the text area and the list. In other words, both the scroll pane / text area and the list will be displayed at their preferred sizes PLUS 75% of the left-over space for the text area and 25% for the list. Again, since the text area's preferred size is large to begin with, it occupies a bigger portion of the screen than what you want.
As a couple of people have already pointed out, a simple way to get the results you want is to explicitly specify the number of rows and columns for the text area. On the other hand, you should avoid explicitly setting a size, preferred or otherwise as discussed in questions like this.
As far as the mysteriously shrinking list goes, I suspect that's a bug in GridBagLayout. It seems to ignore the maximum size of the JList initially but then respects it when it lays out the components as a result of the frame resizing, which is why it suddenly shrinks and then never gets larger again. That's not normally an issue because like JTextArea, a JList is normally contained within a scroll pane, which is something you should probably also consider doing.
try using .setPreferredSize() to components
I have modified your code. Just specified the size of textarea and list like this
list.setPreferredSize(new Dimension(frame.getWidth()/4, frame.getHeight()));
area.setPreferredSize(new Dimension(frame.getWidth()*(3/4), frame.getHeight()));
check the full code below:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
JTextArea area = new JTextArea();
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
JScrollPane scroll = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
for (int i = 0; i < 5; i++)
model.addElement("Example" + i);
frame.setSize(500, 300);
list.setPreferredSize(new Dimension(frame.getWidth()/4, frame.getHeight()));
area.setPreferredSize(new Dimension(frame.getWidth()*(3/4), frame.getHeight()));
area.setLineWrap(true);
area.setWrapStyleWord(true);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.weightx = 0.3;
panel.add(scroll, c);
c = new GridBagConstraints();
c.gridx = 1;
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.weightx = 0.1;
panel.add(list, c);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

GridBagLayout panel expands ignoring BoxLayout's vertical glue

In the example below I have a JPanel with BoxLayout which contains another JPanel with GridBagLayout.
By adding a vertical glue I expect the contents of the inner panel to be "glued" to the top of the outer panel.
But I see the label "A" in the center. If I remove GridBagLayout, A is shown at the top.
Why does this happen?
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args) {
JPanel contentPane = new JPanel();
JFrame frame = new JFrame();
frame.setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel gridBagPanel = new JPanel();
gridBagPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gridBagPanel.add(new JLabel("A"), gbc);
contentPane.add(gridBagPanel);
contentPane.add(Box.createVerticalGlue());
frame.setSize(800, 200);
frame.setVisible(true);
}
}
Because the default value of the anchor property of GridBagConstraints is CENTER. If there is more space in the inner panel than the preferred size of the JLabel, the JLabel would be centered with respect to the available space in the inner panel (since you set fill to NONE).
You can use the anchor value NORTHWEST to glue the label to the top of the panel:
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weighty = 1;
gbc.weightx = 1;
You also need to set the weights per Java documentation: "Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container."
It's probably because GridBagLayout is a pain in the butt and you should consider using other layout managers. Or it could be that you need to set the horizontal and vertical alignment of your components. Take a look at setAlignmentX and setAlignmentY of JComponent. Whenever I've had misbehaved components it's because I didn't align everything the same.
Although Kavka s' Answer solves the requirement of having A on the top of the panel by using an anchor, I believe the declared vertical glue in the BoxLayout Panel doesn't actually do anything. So, I am going to expand on this to contribute what I figured out about using the weights and the vertical glue in GridBagLayouts.
Since GridBagLayout is a complex one, there may be more than one reason, but as Kavka already pointed out:
the weightx and weighty modification from default value 0.0 to a non-zero value is the key.
As pointed out in the Java Tutorial Documentation on GridBagConstraints , in the description section of the JavaDoc for GridBagLayout and the specific parameter description in the JavaDoc for GridBagConstraints.
If all the weights are zero, all the extra space appears between the grids of the cell and
for weightx the extra space appears additionally between the left and right edges.
for weighty the extra space appears additionally between the top and bottom edges.
So, if you wanted to use some vertical glue to fill empty cells you need to make sure you give a non-zero weight to the glue component in the direction you would like it to expand. I am going to solely use the GridBagLayout, so the gridy parameter is going to allow me to define the vertical distribution of the components.
Taking the original code as example base and extending it a bit to demonstrate the capabilities of vertical glue in combination with weights. This code will position A at 20% from the top of the panel, maintaining this ratio when resized:
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel gridBagPanel = new JPanel();
gridBagPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.weighty = 0.2;
gridBagPanel.add(Box.createVerticalGlue(), gbc);
gbc.gridy = 1;
gbc.weighty = 0;
gridBagPanel.add(new JLabel("A"), gbc);
gbc.gridy = 2;
gbc.weighty = 0.8;
gridBagPanel.add(Box.createVerticalGlue(), gbc);
frame.setContentPane(gridBagPanel);
frame.setSize(800, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
For specifics on how the glue works see Java Tutorial on How to Use BoxLayout, Filler section

How to set the component size with GridLayout? Is there a better way?

I am working on a larger GUI with Java and I am becoming angry on Layout Managers.
I have a "Settings-Panel" with a variable number of JComponents in it (Labels, Buttons, JSpinners, JSliders,...). I just want the following:
JLabel <-> JComponent
JLabel <-> JComponent
JLabel <-> JComponent
...
My Panel has a size of 500px, so that there is enough space for a lot of components. Unfortunately the GridLayout always stretches the size of the Components to the whole Panel, even if I set a MaximumSize for every component. It looks stupid if there are only two buttons each with a height of 250px.
I tried FlowLayout, but I cannot figure out a way to make new lines properly. I tried BoxLayout.Y_AXIS, but the Components are always centered, and Label and Component are not in the same line.
Does anybody know a good and short way with LayoutManagers to handle this properly?
An alternative to other layouts, might be to put your panel with the GridLayout, inside another panel that is a FlowLayout. That way your spacing will be intact but will not expand across the entire available space.
Don't use GridLayout for something it wasn't meant to do. It sounds to me like GridBagLayout would be a better fit for you, either that or MigLayout (though you'll have to download that first since it's not part of standard Java). Either that or combine layout managers such as BoxLayout for the lines and GridLayout to hold all the rows.
For example, using GridBagLayout:
import java.awt.*;
import javax.swing.*;
public class LayoutEg1 extends JPanel{
private static final int ROWS = 10;
public LayoutEg1() {
setLayout(new GridBagLayout());
for (int i = 0; i < ROWS; i++) {
GridBagConstraints gbc = makeGbc(0, i);
JLabel label = new JLabel("Row Label " + (i + 1));
add(label, gbc);
JPanel panel = new JPanel();
panel.add(new JCheckBox("check box"));
panel.add(new JTextField(10));
panel.add(new JButton("Button"));
panel.setBorder(BorderFactory.createEtchedBorder());
gbc = makeGbc(1, i);
add(panel, gbc);
}
}
private GridBagConstraints makeGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = x;
gbc.weighty = 1.0;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = (x == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
gbc.fill = GridBagConstraints.HORIZONTAL;
return gbc;
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Layout Eg1");
frame.getContentPane().add(new LayoutEg1());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
For more complex layouts I often used GridBagLayout, which is more complex, but that's the price. Today, I would probably check out MiGLayout.
In my project I managed to use GridLayout and results are very stable, with no flickering and with a perfectly working vertical scrollbar.
First I created a JPanel for the settings; in my case it is a grid with a row for each parameter and two columns: left column is for labels and right column is for components. I believe your case is similar.
JPanel yourSettingsPanel = new JPanel();
yourSettingsPanel.setLayout(new GridLayout(numberOfParams, 2));
I then populate this panel by iterating on my parameters and alternating between adding a JLabel and adding a component.
for (int i = 0; i < numberOfParams; ++i) {
yourSettingsPanel.add(labels[i]);
yourSettingsPanel.add(components[i]);
}
To prevent yourSettingsPanel from extending to the entire container I first wrap it in the north region of a dummy panel, that I called northOnlyPanel.
JPanel northOnlyPanel = new JPanel();
northOnlyPanel.setLayout(new BorderLayout());
northOnlyPanel.add(yourSettingsPanel, BorderLayout.NORTH);
Finally I wrap the northOnlyPanel in a JScrollPane, which should behave nicely pretty much anywhere.
JScrollPane scroll = new JScrollPane(northOnlyPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Most likely you want to display this JScrollPane extended inside a JFrame; you can add it to a BorderLayout JFrame, in the CENTER region:
window.add(scroll, BorderLayout.CENTER);
In my case I put it on the left column of a GridLayout(1, 2) panel, and I use the right column to display contextual help for each parameter.
JTextArea help = new JTextArea();
help.setLineWrap(true);
help.setWrapStyleWord(true);
help.setEditable(false);
JPanel split = new JPanel();
split.setLayout(new GridLayout(1, 2));
split.add(scroll);
split.add(help);
You need to try one of the following:
GridBagLayout
MigLayout
SpringLayout
They offer many more features and will be easier to get what you are looking for.
I used WrapFlowLayout instead
JPanel yourPanel = new JPanel(new WrapFlowLayout(10, 10);

Categories