I have a panel which is divided by two parts with BoxLayout.X_AXIS:
public TabsPanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createLeftPanel());
add(createRightPanel());
}
Each left and right panels have the following structure: an outer panel with BorderLayout, and an inner panel in BorderLayout.CENTER of the outer panel, which in its turn has BoxLayout.Y_AXIS and several components from top to bottom. The right panel has JTextArea with JScrollPane as one of its components:
protected JPanel createRightPanel() {
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JTextArea label = createLabel();
JScrollPane scroll = new JScrollPane(label);
scroll.setMaximumSize(new Dimension(500, 200));
panel.add(Box.createRigidArea(new Dimension(0,106)));
panel.add(scroll);
JPanel panel_buttons = new JPanel();
panel_buttons.setLayout(new BoxLayout(panel_buttons, BoxLayout.LINE_AXIS));
panel_buttons.setAlignmentX(Component.CENTER_ALIGNMENT);
Font font_text = new Font("Georgia", Font.PLAIN, 20);
JButton[] buttons = new JButton[2];
buttons[0] = new JButton("Clear");
buttons[1] = new JButton("Exit");
for (int i = 0; i < buttons.length; i++) {
buttons[i].setMaximumSize(new Dimension(120, 40));
buttons[i].setFont(font_text);
panel_buttons.add(buttons[i]);
if (i == 0)
panel_buttons.add(Box.createRigidArea(new Dimension(40, 0)));
buttons[i].addActionListener(new TextActionListener(label));
}
panel.add(Box.createRigidArea(new Dimension(0,20)));
panel.add(panel_buttons);
pane.add(panel, BorderLayout.CENTER);
return pane;
}
When text goes beyond the borders, scroll bars appear and I can move them and read the text. Looks like everything is ok, but when I either click any place outside the scroll pane or even just move the pointer, the scroll pane moves to the left and grows down. It doesn't change its width, but it shifts to the left because the area between it and the right panel's borders increases. Accordingly, size of the left panel shrinks. When I clear the text area and again either click or move the pointer, it is back to its normal size.
What is the reason its height grows and its left and right margins increase? What am I doing wrong?
UPDATE. I've found the problem. The thing is that I didn't create JTextArea correctly. I initialized it without parameters:
JTextArea text = new JTextArea("Some initial text");
Now I have rewritten:
JTextArea text = new JTextArea(5,10);
It is now shifted to the left by about 5 mm and do not changes its height. Still not perfect, but looks like I am on the right track.
Thank you everybody for your help!
BoxLayout accepting Min, Max and PreferredSize override those methods for JPanel
use JSPlitPane, there you can to hide Divider
2 steps to correct:
Set the size of the JTextArea: JTextArea text = new JTextArea(row, col);
Still shifts to the left by the size of the vertical bar:
either add ChangeListener to adjust the size of the JScrollPane
scroll.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (scroll.getVerticalScrollBar().isVisible())
scroll.setPreferredSize(480, 200);
}
}
});
or add scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Related
I have a custom panel (APanel). Two questions:
I need the label ("Top Label") and sub panel (bPanel) to align on the left side of the panel. bPanel is aligning on the left side but Top Label is being place in the middle. How do I fix?
How do I set the label and bPanel so they don't change in vertical size and maintain the same vertical distance between each other when the entire frame is expanded? In other words, how do I prevent the below from happening?
Code for APanel and bPanel are below. In case it matters, APanel is loaded as the CENTER component in a JFrame set to BorderLayout (not including the frame code here).
public class APanel extends JPanel {
private JLabel jLabel1;
private BPanel bPanel;
public APanel () {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
jLabel1 = new JLabel("Top Label", JLabel.LEFT);
jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
jLabel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(jLabel1);
bPanel = new BPanel();
bPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(bPanel);
this.setAlignmentX(LEFT_ALIGNMENT);
}
private class bPanel extends JPanel {
private JLabel sLabel;
private JLabel tLabel;
public bPanel() {
sLabel = new JLabel("Jeepers");
tLabel = new JLabel("Creepers");
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 1;
gc.weighty = 1;
gc.fill = GridBagConstraints.HORIZONTAL;
add(sLabel, gc);
gc.gridx = 1;
add(tLabel, gc);
}
}
}
I need the label ("Top Label") and sub panel (bPanel) to align on the left side of the panel.
jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
Wrong method to control alignment with respect to the container. Read the JLabel API. That method is used to align the text within the bounds of the JLabel, in case the size of the label is greater than the preferred size of the text.
this.setAlignmentX(LEFT_ALIGNMENT);
Close, but that is not needed because if refers to the alignment of this entire panel in its parent container.
Instead, you need to set the "alignment" of all the components you are adding the this panel using the vertical BoxLayout. Each component may have a different default value. Some will default to "left" and some to "center".
Read the section from the Swing tutorial on Fixing Alignment Problems for more information.
How do I set the label and bPanel so they don't change in vertical size and maintain the same vertical distance between each other when the entire frame is expanded?
The BoxLayout will allow components to grow (up to their maximum size) when extra space is available. For a JLabel the maximum size is the same as its preferred size. For a JPanel the maximum size is the value of Integer.MAX_VALUE.
So you can override the getMaximumSize() method of you panel with code something like:
#Override
public Dimension getMaximumSize()
{
Dimension preferred = getPreferredSize();
Dimension maximum = super.getMaximumSize();
maximum.height = preferred.height;
return maximum;
}
Or another option is to wrap the panel in another panel that will respect the height. For example.
JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(bPanel, BorderLayout.PAGE_START);
add(wrapper);
//add(bPanel);
So I have a problem that I have to make a questionnaire about something and I have to use multiple Layouts.
My problem is that when I add 2 JPanels to a Grid layout (only to 1 side) my first Panel takes up most of the space.
Code:
public class MainFrame extends JFrame implements ItemListener{
JPanel mainPanel,rightSideAge,rightSideGender,leftSide,rightSideBox,leftSideBox;
JTextArea nameArea;
JSpinner ageSpinner;
JRadioButton genMale,genFema;
ButtonGroup genderGroup;
MainFrame(){
this.setSize(1000, 800);
this.setLocationRelativeTo(null);
this.setTitle("Közvélemény kutatás a zenei ízlésekről");
mainPanel = new JPanel(new GridLayout(0, 2));
this.setContentPane(mainPanel);
/* --- RIGHT PANEL --- */
rightSideBox = new JPanel();
rightSideBox.setLayout(new BoxLayout(rightSideBox, BoxLayout.Y_AXIS));
rightSideAge = new JPanel(new FlowLayout(FlowLayout.LEFT));
rightSideAge.setBorder(BorderFactory.createLineBorder(Color.BLUE));
//rightSide.setLayout(new BoxLayout(rightSide, BoxLayout.Y_AXIS));
mainPanel.add(rightSideBox);
//Age label
//JLabel labelAge = new JLabel("Kor: ");
//labelAge.setSize(100, 30);
//Age Spinner
ageSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 120, 1));
ageSpinner.setPreferredSize(new Dimension(40, 20));
Component mySpinnerEditor = ageSpinner.getEditor();
JFormattedTextField jftf = ((JSpinner.DefaultEditor) mySpinnerEditor).getTextField();
jftf.setColumns(5);
//New box for zenei ízlés
rightSideGender = new JPanel();
rightSideGender.setBorder(BorderFactory.createLineBorder(Color.GREEN));
rightSideGender.setLayout(new BoxLayout(rightSideGender,BoxLayout.Y_AXIS));
//Gender ComboBox
genderGroup = new ButtonGroup();
genMale = new JRadioButton("Férfi");
genderGroup.add(genMale);
genFema = new JRadioButton("Nő");
genderGroup.add(genFema);
/* --- LEFT SIDE --- */
rightSideBox.setBorder(BorderFactory.createLineBorder(Color.RED));
/* ADD STUFF TO PANELS */
/* RightSideBox */
rightSideBox.add(rightSideAge);
rightSideBox.add(rightSideGender);
/*RIGHT SIDE PANELS*/
rightSideGender.add(new JLabel("Nem:"));
rightSideGender.add(genMale);
rightSideGender.add(genFema);
rightSideAge.add(new JLabel("Kor"));
//rightSide.add(labelAge);
rightSideAge.add(ageSpinner);
/*LEFT SIDE PANEL*/
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
The Blue lineout sould be only under the JSpinner:
ageSpinner.setPreferredSize(new Dimension(40, 20));
First of all, you should not be manually setting the size of a component. Each Swing component is responsible for determining its own size.
The Blue lineout should be only under the JSpinner:
The box layout will resize a component to its maximum size if there is space available. For some reason a JSpinner doesn't appear to have a maximum height so it expands to fill all the available space.
To fix this you can do something like:
//ageSpinner.setPreferredSize(new Dimension(40, 20));
ageSpinner.setMaximumSize( ageSpinner.getPreferredSize() );
mainPanel layout is set to have two columns:
mainPanel = new JPanel(new GridLayout(0, 2));
You only add one panel to mainPanel which uses the GridLayout:
mainPanel.add(rightSideBox);
Note: the first component you add, in this case rightSideBox will occupy the first column, meaning it will be the LEFT one.
To add rightSideGender to mainPanel you need to :
mainPanel.add(rightSideGender);
The second component you add, in this case rightSideGender will occupy the second column, in this case the RIGHT one.
I am currently trying to create a script editor. But the lineNumber JPanel is not top aligned next to the JTextArea. The lineNumber JPanel appears at the center on the right side of the JTextArea.
It looks like this:
This is the class which instantiates both of these components:
private ScriptEditor() {
((FlowLayout) this.getLayout()).setVgap(0);
((FlowLayout) this.getLayout()).setHgap(0);
//This is the lineNumber JPanel which has no LayoutManager set.
lineNumPanel = new LineNumberPanel();
//I tried setAlignmentY but it did not work
lineNumPanel.setAlignmentY(TOP_ALIGNMENT);
//The text area.
scriptArea = new JTextArea(22,15);
scriptArea.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
scriptArea.setMargin(new Insets(3, 10, 3, 10));
//This JPanel contains the two components: lineNumber JPanel and the JTextArea
JPanel temp = new JPanel();
temp.add(lineNumPanel);
temp.add(scriptArea);
//Set the scrollPane
JScrollPane scrollPane = new JScrollPane(temp);
scrollPane.setPreferredSize(new Dimension(width, height));
//Add the scrollPane to this JPanel.
add(scrollPane);
}
JPanel temp = new JPanel();
By default a JPanel uses a FlowLayout. a FlowLayout vertically centers the components added to the panel. If you don't like this behaviour then try a different layout manager like a horizontal BoxLayout, which will allow you to align the component at the top/center/bottom depending on the components vertical alignment.
However, using a JPanel is not the best approach. Instead you should be adding the line number component to the row header of the scroll pane. See Text Component Line Number for an example of this approach.
I need to know how to build a java layout that will be dynamically resized. The layout is set up with 4 panels, stacked top to bottom:
the top panel is a GridLayout(4,2). each rown is a label and a text field
the second panel is a single button
the third panel is a grid layout (n,2). I will get to n in a minute
the bottom panel is also a single button
the n is a dynamically changing number. the third panel contains a text field, then a panel with 2 buttons to the right. the problem I have is that I need to be able to add and remove these rows and have the JFrame resize automatically as I do. I tried setting the Frame as a GridLayout(4,1), but when I change the size on actionPerformed(), it spreads the extra space evenly. I want to add only space to the third panel.
Thanks
EDIT: ActionPerformed Method
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == addTarget) {
targetChoosers.add(new JFileChooser(new File("C:\\")));
targets.add(new JTextField());
targetButtons.add(new JButton("Browse..."));
targetDeleters.add(new JButton("Delete"));
int numTargets = targets.size();
targetButtons.get(numTargets - 1).addActionListener(this);
targetDeleters.get(numTargets - 1).addActionListener(this);
bottomPanel.setLayout(new GridLayout(numTargets, 2));
bottomPanel.add(targets.get(numTargets - 1));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
panel.add(targetButtons.get(numTargets - 1));
panel.add(targetDeleters.get(numTargets - 1));
bottomPanel.add(panel);
}
//...
else if (targetDeleters.contains(src)) {
int index = targetDeleters.indexOf(src);
targets.remove(index);
targetChoosers.remove(index);
targetButtons.remove(index);
targetDeleters.remove(index);
this.remove(submit);
this.remove(bottomPanel);
int numTargets = targets.size();
bottomPanel = new JPanel(new GridLayout(numTargets, 2));
for (int i = 0; i < targets.size(); i++) {
bottomPanel.add(targets.get(i));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
panel.add(targetButtons.get(i));
panel.add(targetDeleters.get(i));
bottomPanel.add(panel);
}
this.add(bottomPanel);
this.add(submit);
}
//...
pack();
invalidate();
validate();
}
GridBagLayout may help for the (n,2) problem.
Another great LayoutManager is MigLayout.
I have got a window that should display the following:
JLablel "Have you used GUI before?" on the top, centered
two radioButtons "Yes" and "No" below it, somewhat in the center, a little bit towards the left
a JButton "NEXT" in the bottom-right corner
All three elements should have green font and darkGrey background.
The problem is that the window which is showing up, does not look like I would like it to.
And this is my code:
yesButton = new JRadioButton(yes);
//yesButton.setMnemonic(KeyEvent.VK_B); // doesn't work?
yesButton.setActionCommand(yes);
noButton = new JRadioButton(no);
// noButton.setMnemonic(KeyEvent.VK_C); // doesn't work?
noButton.setActionCommand(no);
ButtonGroup group = new ButtonGroup();
group.add(yesButton);
group.add(noButton);
nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
yesButton.addActionListener(this);
noButton.addActionListener(this);
nextButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);
add(radioPanel, BorderLayout.WEST);
// setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
// radioPanel.setBorder(new EmptyBorder(250, 250, 20, 20));
// there is no difference between the above two, right?
String q = "Have you used GUI before?";
JPanel area = new JPanel(new BorderLayout());
area.setBackground(Color.darkGray);
JLabel textLabel2 = new JLabel("<html><div style=\"text-align: center;\">"
+ q + "</html>", SwingConstants.CENTER);
textLabel2.setForeground(Color.green);
Font font2 = new Font("SansSerif", Font.PLAIN, 30);
textLabel2.setFont(font2);
//textLabel2.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
area.add(textLabel2, BorderLayout.NORTH);
area.add(nextButton, BorderLayout.EAST);
add(area, BorderLayout.CENTER);
I feel I'm nearly there, thanks for any help!
--EDIT--
A screenshot:
You need to use nested panels.
for the BorderLayout.NORTH you can add the JLabel directly. You will need to set the horizontal text alignment to center.
for the radio buttons you can create a JPanel with a FlowLayout and then add the buttons to the panel and add the panel to the CENTER.
for the button you add the button to a panel using a FlowLayout that is right aligned, then add the panel to the SOUTH.
There are other choices. You could also use a Vertical BoxLayout as the layout of the main panel and then add child panels to it.
You won't be able to get much control with just a BorderLayout. Try something else like MigLayout or one of the other many many layout managers Java has (GridBag, Box, etc).
In MigLayout it would look something like:
area.setLayout(new MigLayout("fill"));
area.add(textLabel2, "wrap");
area.add(radioPanel, "wrap");
area.add(nextButton, "tag right");