EDIT2: I also get a white/grey area, if I try to add a JLabel to the area, where the checkboxes are, that you see in the second image.
EDIT: Also note this: When I just create a JLabel and add it before totalResultArea is added to panelResults, it also only shows a white/grey area.
I created two JPanels panelResults and totalResultArea, but only see a white/grey area for the second panel (totalResultArea).
At first I set the layout of panelResults to GridBagLayout
JPanel panelResults = new JPanel();
JPanel totalResultArea = new JPanel();
panelResults.setLayout(new GridBagLayout());
Then I add the totalResultArea to panelResults:
GridBagContraints c = new GridBagConstraints();
totalResultArea.setLayout(new BoxLayout(totalResultArea, BoxLayout.Y_AXIS));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.weightx = 2;
panelResults.add(totalResultArea, c);
In a click-event-listener of a button I add a JLabel to the totalResultArea and set the JFrame visible:
JLabel totalResultsText = new JLabel("<html><body>...</body></html>");
totalResultArea.add(totalResultsText);
// revalidate and repaint totalResultArea and panelResults
totalResultArea.revalidate();
totalResultArea.repaint();
panelResults.revalidate();
panelResults.repaint();
// add panelResults to frame
frame.getContentPane().add(panelResults, BorderLayout.NORTH);
// invalidate, repaint the frame and make it visible
frame.invalidate();
frame.repaint();
frame.setVisible(true);
This all looks like this:
I hope you can reproduce this. If not see the picture.
What could be the reson, that I don't see the text from the JLabel, but an only white/grey area?
The background- and the font-color of the label are both white so you can't see the text.
When you take paint and fill the label with another color you will see that there is a text.
So check your style and/or add totalResultsText.setForeground(Color.BLACK);
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);
This seems like a simple thing to do, but I can't get it to work.
I have a BorderLayout. I want to use the top part for a title bar. I want to add a JPanel with labels, buttons and other components. However, the PAGE_START part of the border layout won't left align the panel. Here's the situation, with comments in where I've tried to set the alignment.
I noticed that when I don't add a panel to the border layout, and just write the JLabel straight in, it has left alignment by default.
This is not what I want, though, because I am planning on putting a BoxLayout.X_AXIS horizontally through the BorderLayout.PAGE_START title area. Seems to be a reasonable thing to do?
The Container pane argument to the static method is just the single panel on the main JFrame.
public static void addComponentsToPane(Container pane)
{
JLabel jlabel = new JLabel("I want to left align this inside a JPanel");
// Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel jpanel = new JPanel();
//Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
jpanel.add(jlabel);
pane.add(jpanel, BorderLayout.PAGE_START);
// Other parts of the BoxLayout (works fine)
JButton button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
Even when I tell the panel to left align the label, it doesn't appear left aligned.
Does anyone know what I am doing wrong?
By default a JPanel uses a FlowLayout with "center" alignment.
if you want components "left" aligned, then you need to set the layout on the panel to use a FlowLayout with "left" alignment.
Read the FlowLayout API for the proper constructor to use to set the alignment.
Or you can also read the Swing tutorial on How to Use FlowLayut which gives the constructors and valid values to specify the alignment.
When I insert any button or any component in my frame and set the component gridx and gridy to values 0, it does not put that component at the starting of the frame instead it put the component somewhere in the center.
How to remove the spaces and put my component at the very starting of the frame?
Following is the code:
JFrame frame = new JFrame("Grigbag layout");
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button = new JButton("First button");
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
I suggest you to use Flow Layout and also add this code
frame.pack();
as mentioned by Andrew Thompson above
for more :
https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
Use the frame.pack()method and remove the frame.setSize(); method.
frame is using the default layout. Set frame's layout with something like
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
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);
I am using a GridBagLayout to (currently) display two rows. I am aware this layout is overkill for this task, but am trying to learn how to use it. The problem is that I have added the two panels to the two separate rows and there is a huge gap around the content (see image and code below):
alt text http://www.imagechicken.com/uploads/1264533379009864500.png
Image background;
public Table(){
super();
ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
background = ii.getImage();
setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
JButton button = new JButton("hello world");
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(800,100));
panel1.add(button, BorderLayout.CENTER);
panel1.setBackground(Color.yellow);
add(panel1, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
JPanel middlePanel = new JPanel();
middlePanel.setPreferredSize(new Dimension(800,350));
middlePanel.add(button, BorderLayout.CENTER);
middlePanel.setBackground(Color.blue);
add(middlePanel, constraints);
}
Use
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1d;
constraints.weighty = 1d;
JavaDoc for weightx/weighty says:
Specifies how to distribute extra horizontal/vertical space.
JavaDoc for fill:
This field is used when the component's display area is larger
than the component's requested size. It determines whether to
resize the component, and if so, how.
Unfortunately, with GridBagLayout, if the contents do not fill the entire container that it is in, it will automatically center all its contents within its container. That is why you are getting a really large gap.
There are essentially two ways to fix this:
The hard way: Fiddle with the GridBagConstraints. I had limited success with this when trying to avoid the centre-ing behaviour.
The easy way: Put the GridBagLayout inside of a FlowLayout, and then set the alignment of the FlowLayout to top-left, or whatever you wish.
I have asked, and answered, this question myself sometime last week.
So in your case you are adding your panel1 and middlePanel directly to the JFrame (?), with a GridBagLayout
JFrame (GridBagLayout)
- panel1
- middlePanel
I would suggest this alternative structure instead, to get rid of all the extra space (and centre alignment as well, if you like).
JFrame (FlowLayout)
- JPanel (GridBagLayout)
- panel1
- middlePanel
HTH