I'm trying to make something that looks like this:
As you can see, I want the labels on opposite sides of each other on the same line in the same parent container.
I tried using the GridBagLayout, and here is my code:
JPanel cont = new JPanel();
JLabel left = new JLabel("left");
JLabel right = new JLabel("right");
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
cont.add(left, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
cont.add(right, gbc);
You can use GridBagLayout, but I find using the constraints painful and sometimes unpredictable. This is easier with GridLayout and specifying the justification of the JLabel.
Here's an example that puts two JLabels in a row as in your drawing:
JPanel labelrow = new JPanel(new GridLayout(1,2));
JLabel left = new JLabel("left side", JLabel.LEFT);
JLabel right = new JLabel("right side", JLabel.RIGHT);
You can do the same with other controls, like buttons, if you put them inside JPanels with FlowLayouts that are left and right-justified.
Here's an example showing it with buttons off to each side:
JPanel buttons = new JPanel(new GridLayout(1,2));
JPanel left = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel right = new JPanel(new FlowLayout(FlowLayout.RIGHT));
left.add(new JButton("Left"));
right.add(new JButton("Right"));
buttons.add(left);
buttons.add(right);
my problem is that although the scroll bar is appearing on the specific panel I want it to it's not extending as labels are added. I would like it to extend when the labels start going off the panel that they are being added to.
I have a main JPanel 'panel' which uses GridBagLayout, within that I have 5 other panels, the ones that needs a scroll bar is boardPanel which has null value for setLayout()
panel = new JPanel();
panel.setLayout(new GridBagLayout());
...
1Panel = new JPanel();
1Panel.setPreferredSize(new Dimension(480, 800));
1Panel.setLayout(null);
scrollPanel = new JScrollPanel(1Panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
...
c.weighty = 1.0;
c.weightx = 0.6;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
panel.add(scrollPanel, c);
...
I hope this is enough information, thank you so much for your help in advance.
Don't use a null layout!!!
Don't use setPreferredSize()!!!
Scrollbars will appear when the preferred size of the component is greater than the size of the scroll pane. By hardcoding a size you break this functionality.
Use a layout manager and the preferred size will change dynamically as you add components to the panel.
Read the Swing tutorial on Layout Managers
I am making a program with 3 JSliders, for r,g,b and i want to add a panel that will change it's color for the chosen color in the sliders, everything works for me except one thing, I don't know how to make the panel in the full size of the screen, this is the best I could do, but this is still kind of small and I want to make the panel full size. can any one show me how to do it?
The program is kind of long so I will only send the part of the gridbaglayout and the panel.
private JPanel panel;
public delta(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
panel = new JPanel();
panel.setBackground(new Color(0 ,0 ,0));
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 6;
c.gridwidth = 3;
c.gridheight = 3;
add(panel ,c);
Simply add your panel to Jframe
jframe.add(yourpanel)
This code without any Layout will fill the window.
Edit:
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("HELLO");
frame.add(label);
This fills the JFrame with JLabel.
I need some help in JInternalFrame within JPanel's Area.I have a JFrame which contains
JPanel added to its ContentPane.JFrame Contains Menu when i click one of its Menu item i
need JInternal Frame to be added on top of the contentpane.The Code i have given so far,
JDesktopPane desktop = new JDesktopPane();
desktop.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, 6.0, 1.0,
Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 0.0, 8.0, 0.0,
Double.MIN_VALUE };
topPanel.setLayout(gbl_contentPane);
JPanel left = new JPanel();
GridBagConstraints gbc_left = new GridBagConstraints();
gbc_left.insets = new Insets(0, 0, 5, 5);
gbc_left.fill = GridBagConstraints.BOTH;
gbc_left.gridx = 0;
gbc_left.gridy = 1;
topPanel.add(left, gbc_left);
JPanel middle = new JPanel();
GridBagLayout gbl_middle = new GridBagLayout();
gbl_middle.columnWeights = new double[] { 1.0 };
middle.setLayout(gbl_middle);
GridBagConstraints gbc_middle = new GridBagConstraints();
gbc_middle.insets = new Insets(0, 0, 5, 5);
gbc_middle.fill = GridBagConstraints.BOTH;
gbc_middle.gridx = 1;
gbc_middle.gridy = 1;
topPanel.add(middle, gbc_middle);
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel1 = new JPanel();
Border eBorder = BorderFactory.createEtchedBorder();
panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = gbc.weighty = 30;
middle.add(panel1, gbc);
panel1.setLayout(new MigLayout("", "[944.00,grow][353.00]",
"[6.00][128.00,grow][]"));
/*lblHeader = new JLabel(
"<html>Indira Institute of Technology<br>Tatabad<br>Karpagam Complex Stop<br>Coimbatre</html>");
lblHeader.setIcon(new ImageIcon(
"C:\\Users\\Prakash\\Desktop\\images.jpg"));
panel1.add(lblHeader, "cell 0 1 2 1,alignx center,aligny center");*/
JPanel panel2 = new JPanel();
gbc = new GridBagConstraints();
panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
gbc.gridy = 1;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 70;
gbc.insets = new Insets(2, 2, 2, 2);
middle.add(panel2, gbc);
panel2.setLayout(new MigLayout(
"",
"[30px][69.00px][144.00][68.00][][159.00px][59.00px][65px][28.00][]",
"[20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][30.00][48.00:n,grow 50,shrink 50]"));
getContentPane.add(topPanel);
I have never used the DesktopPane in this(I don't know how to make use of this in this situation) And The Screen So far is as follows,
Now I need the JInternalFrame to be added for the Previous Screen as Follows,
I am aware that i can only be able to add a JInternalFrame to the DesktopPane.But i
Already Filled my ContentPane with JPanel to show its content.How can i achieve Jinternal
Frame to be added in this JFrame.Kindly give your valuable suggestions.
Not really the right direction. You original panel is under the control of layout manager, this means that when you add the JInternalFrame to it, the layout manager wants to try and layout it out.
Generally speaking, a JInternalFrame wants to reside in a container which is unmanaged, allowing it to be positioned and sized independently of the content.
A possible solution might be to take advantage of the glass pane for the JInternalFrame instead, for more details see How to Use Root Panes
Another solution might be to use a JLayeredPane. Basically, you would start by setting the layout manager of the JLayeredPane to something link BorderLayout add the first panel to it and then add a second, transparent pane, with no layout, above it. You would add the JInternalFrames to this second panel.
See How to Use Layered Panes for more details
The question that jumps out at me though is...why? Why wouldn't you just use some kind of dialog instead? See How to Make Dialogs for more details
What is it what you really want?
You wrote you already have your content pane added to your frame. JDesktopPane has to have its own space reserved. If you don't have or you don't want to reserve space for the internal frames in your main frame, then maybe you don't even want it to be part of the main frame. In this case you might want to use child JDialogs instead of JInternalFrames.
So either add your JDesktopPane to your main frame (having its space reserved) or use child JDialogs which can be modal or not and can overlap any part of the main frame. JInternalFrames are only visible in the area of the JDesktopPane while child JDialogs can "float" over your main frame.
Check out this Oracle documentation: How to Use Internal Frames
And How to Make Dialogs.
I am using a JDialog with GridBagLayout. Since this layout determines the container and component size automatically, I have not used setSize on anything. However, when the GUI is drawn, it seems to be stretching the container unnecessarily.
Why does GridBagLayout not size the container to "just as much as needed"? Basically I want the dialog size to be just as big as the table inside. Here is the code snippet:
public class GridBagLayoutTester
{
public static void main(String[] args)
{
JDialog mDialog = new JDialog();
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
// Create a table to be added to the panel
JTable table = new JTable(4,4);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setBorder(BorderFactory.createLineBorder(Color.ORANGE, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
// Add table to the panel
panel1.add(scrollpane, gbc);
mDialog.add(panel1, BorderLayout.CENTER);
// Display the window.
mDialog.pack();
mDialog.setVisible(true);
}
}
You have to set the preferred size of components when the default sizing isn't what you want.
Change your program to add these lines:
// Display the window.
mDialog.pack();
Dimension d = table.getPreferredSize();
d.width += 16;
d.height += 10;
scrollpane.setPreferredSize(d);
mDialog.pack();
mDialog.setVisible(true);
The additional width of 16 accommodates the border and vertical scroll bar.
The additional height of 10 accommodates the border.