I'm trying to create a relatively small-scale text based game inside of a JFrame
. I plan to keep this JFrame
at a constant size, so I've called setResizable(false)
.
My goal was to make a text I/O feature that somewhat acted like a console or command prompt, so I have two JTextArea
s, each inside a JScollPane
, and then I've added them both to a Box
, one on top of the other. I wanted these components to keep a constant size, so I called setPreferredSize()
and setMaximumSize()
with the appropriate dimensions.
However, when I add two of these Boxes to the JFrame
, there is a horizontal gap separating them. The gap appears to be attached to the right-hand border of the component. See the attached picture of this behavior:
).
I'm using a FlowLayout
in the content pane, with hGap set to 0, and yet there is still a gap between the components I've added, and I cannot figure out why.
To confirm it's not a problem with the JFrame
itself, or with its layout manager, I've also added two boxes containing buttons to illustrate that they do not have a gap between them.
If at all possible I'd like to remove the gaps, or at least determine what's causing them. At this point I have no idea what else might be causing the issue, and I'd appreciate any advice.
EDIT: Here is a picture of what I'd like this demo class to look like:
I have excerpted the relevant bits of code into this class to demonstrate my problem:
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
public Demo() {
super("Demo");
setSize(400, 400);
setResizable(false);
int Y_AXIS = BoxLayout.Y_AXIS;
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
getContentPane().setBackground(Color.RED);
Box boxA = new Box(Y_AXIS);
boxA.add(createComponent("output"));
boxA.add(createComponent("input"));
this.add(boxA);
Box boxB = new Box(Y_AXIS);
boxB.add(createComponent("output"));
boxB.add(createComponent("input"));
this.add(boxB);
Box boxC = new Box(Y_AXIS);
boxC.add(new JButton("Button"));
boxC.add(new JButton("Button"));
this.add(boxC);
Box boxD = new Box(Y_AXIS);
boxD.add(new JButton("Button"));
boxD.add(new JButton("Button"));
this.add(boxD);
this.setVisible(true);
}
private JScrollPane createComponent(String initial) {
JTextArea ta = new JTextArea(3, 10);
ta.setText(initial);
JScrollPane sp = new JScrollPane(ta);
Dimension pref = new Dimension(100, 100);
sp.setPreferredSize(pref);
sp.setMaximumSize(pref);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return sp;
}
public static void main(String[] args) {
Demo d = new Demo();
}
}
I figured out what was causing the gaps to appear, although I'm not familiar enough with Swing or LayoutManagers to say exactly why.
By removing just the call to setMaximumSize()
of the interior components, the program works the way I'd like it to. My guess is that one of the layout managers (possibly the BoxLayout
built into the Box
?) doesn't behave quite the way I expected when asked to use absolute maximum sizes, instead of relying on the preferred size and letting the layout manager do the work.
Here's the revised code, with the line in question commented out.
private JScrollPane createComponent(String initial) {
JTextArea ta = new JTextArea(3, 10);
ta.setText(initial);
JScrollPane sp = new JScrollPane(ta);
Dimension pref = new Dimension(100, 100);
sp.setPreferredSize(pref);
//sp.setMaximumSize(pref);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return sp;
}
Although this solution does what I want it to, it is probably not ideal , as Andrew Thompson mentioned in his comment, and I should try to avoid setting any of the preferred/maximum sizes.