Java Swing GridBagLayout - java

I've been learning Java Swing using GridBagLayout.
I have a main JFrame and add main JPanel, then I add subsequent JPanels/components to that main JPanel.
But anything I add it always positions to the center of the main panel (including while resize), which is not desired result.
Just a small snippet on the core code:
getContentPane().setLayout(new GridBagLayout());
JPanel panel = new JPanel(new GridBagLayout());
getContentPane().add(panel);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
JTextField nameField = new JTextField(10);
panel.add(nameField, gbc)
Any feedback would be appreciated, thanks.

In the future, post a SSCCE when you have a problem. A small "snippet" of code generally doesn't help.
Read the section from the Swing tutorial on How to Use GridbagLayout. The part on "weightx/weighty" will explain why this happens.

Related

how to vertically align panels withing Java AWT/Swing

I'm wondering how exactly to use swing. I'd like to align 3 panels so that panel 1 is on top of panel 2, which is then on top of panel 3. Each of these panels will then have their own labels/buttons within it.
Each of these then needs to contain their own labels/buttons inside the panels.
Use a GridBagLayout or GridLayout. Start by having a look at Laying Out Components Within a Container
GridBagLayout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(new ExamplePane(1), gbc);
add(new ExamplePane(2), gbc);
add(new ExamplePane(3), gbc);
GridLayout
setLayout(new GridLayout(0, 1));
add(new ExamplePane(1));
add(new ExamplePane(2));
add(new ExamplePane(3));
Important
There are significant differences between the two and you will need to read the linked tutorial and supporting documentation on both these layouts to understand how they work and which might be best suited to you're immediate needs

JScrollPane doesn't seem to scroll

I'm pretty new to Java Swing. Can someone help me figure out what I am doing wrong? Please correct me anywhere necessary. A good portion of this code was trial and error.
I have a frame, which contains a JPanel. The JPanel is using the "GridBag" layout. The code included revolves around the child JPanel on the right side as seen in the picture. For some reason, I can't seem get my vertical scrollbar working properly.
Here's the code of interest:
/// GridBagConstraints
GridBagConstraints gbc = new GridBagConstraints();
// parent jpanel for scrollpane
scrollPanel = new JPanel();
scrollPanel.setLayout(new BorderLayout());
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(scrollPanel, gbc);
// content jpanel for scrollpane
scrollPaneContent = new JPanel();
scrollPaneContent.setLayout(new GridLayout(0, 1, 0, 1));
// scrollPane
scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createEmptyBorder(0,30,0,0));
scrollPane.setViewportView(scrollPaneContent);
scrollPanel.add(scrollPane, BorderLayout.PAGE_START);
And here is what the program looks like at the moment.
You can see the numbers just go off the screen:
Any help is greatly appreciated! Thank you.
scrollPanel.add(scrollPane, BorderLayout.PAGE_START);
You are attempting to add the scrollPane to the scrollPanel. That is not the way it works.
A JScrollPane is a container, so you need to add the panel containing the components to the scroll pane
JPanel panel = new JPanel(...);
panel.add(....);
panel.add(....);
JScrollPane scrollPane = new JScrollPane( panel );
frame.add( scrollPane );
The above code will add the panel to the "viewport" of the scroll pane.

Jbuttons inside JPanel with background image in right | bottom corner

Can somebody explain me how can I put few jButtons inside jLabel which have background image like on this image? The main jFrame is undecorated and is set to full screen.
I saw a lot of different examples like
this or like this, but these examples are showing only single button in jPanel.
Personally, I'd avoid using a JLabel for this purpose, it does not calculate it's required size based on it's content, but rather off it's icon and text properties.
This might be a good or bad thing, but it can catch your unawares if you're not aware of it.
Instead, I'd use a customised JPanel, which would allow you to define things like the resize and fill rules, for example and for example
Now, once you have that covered, you need to create a panel of your buttons. I prefer to create a dedicated class, as it makes it easier to isolate functionality and management, but that's me...
public class ButtonPane extends JPanel {
public ButtonPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(8, 8, 8, 8));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JButton("Button 1"), gbc);
add(new JButton("Button 2"), gbc);
add(new JButton("Button 3"), gbc);
}
}
Next, you need to add this panel to your background
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(backgroundPane);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.insets = new Insets(30, 30, 30, 30);
ButtonPane buttonPane = new ButtonPane();
frame.add(buttonPane, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Which can generate something like...
Have a look at Laying Out Components Within a Container and How to Use GridBagLayout for some more details
These examples are truly good enough, I think you should just learn more about swing.
For now, You could simply do:
JFrame frame = new JFrame("Hi there");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
frame.add(b1);
frame.add(b2);
b1.setBounds(60, 60, 40, 40);
b2.setBounds(10, 10, 40, 40);
frame.setVisible(true); //in case, add frame.setLayout(null);
You can of course add buttons to JPanel instead of JFrame

making a full size panel, with grid bag layout

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.

Confusion about Layout Managers to use

I am creating a simple game using Java. I have created the game's menu using JFrame. I am having confusion about what layouts to be used to place the Menu Buttons(Start,High Scores,Instructions,Exit) at the center. I have an approach in mind that is :
Creating a grid layout of three columns and in the middle column adding a box layout(having the menu buttons) positioned at the center of this column.
Should I use this approach? if not then please tell me the solution.
Use a GridBagLayout
JButton startButton = new JButton("Start");
JButton scoreButton = new JButton("High Score");
JButton instructButton = new JButton("Instructions");
JButton exitButton = new JButton("Exit");
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = java.awt.gbc.HORIZONTAL;
gbc.insets = new java.awt.Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
getContentPane().add(startButton, gbc);
gbc.gridy = 1;
getContentPane().add(scoreButton, gbc);
gbc.gridy = 2;
getContentPane().add(instructButton, gbc);
gbc.gridy = 3;
getContentPane().add(exitButton, gbc);
Madprogrammer is right, use a gridbaglayout. I myself am actually too lazy to fool around with layout managers so I use WindowBuilder for Eclipse SDK. Search for it on google. WindowBuilder has bi directional code generation (ie, it can parse swing code and then generate it from what you do in the gui), and its all drag and drop.

Categories

Resources