0
votes

I currently have a jpanel with layout set to GridBagLayout, with gbc = new GridBagConstraints();, However for whatever value of x, y or gridwidth,gridheight the items don't move at all.

I would greatly appreciate an expert eye to look over my code to see what I am missing, Thanks in advance.

Edit: Added the imports and the main method

Edit 2: turns out I was thinking the x and y values were pixels and that was the reason it wasn't working

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class HomeScreenUI {
    
      public void addobjects(Component componente, Container yourcontainer, GridBagLayout layout, GridBagConstraints gbc, int gridx, int gridy, int gridwidth, int gridheight){

            gbc.gridx = gridx;
            gbc.gridy = gridy;

            gbc.gridwidth = gridwidth;
            gbc.gridheight = gridheight;

            layout.setConstraints(componente, gbc);
            yourcontainer.add(componente);
        }
    HomeScreenUI(){
        //frame
        
        JFrame frame = new JFrame("Opisa");

        //panels, one before button click and one after
        JPanel panel = new JPanel();
        JPanel panelAfterButtonClick = new JPanel();
        GridBagLayout ourlayout;
        
        ourlayout = new GridBagLayout();
        panel.setLayout(ourlayout);
        panelAfterButtonClick.setLayout(ourlayout);
        
        GridBagConstraints gbc = new GridBagConstraints();

        //jlabel that isnt displaying + dimensions
        JLabel label = new JLabel("Opisa");
        label.setFont(new Font("Helvetica", Font.PLAIN, 70));
        //second jlabel that isn't displaying
        JLabel label2 = new JLabel("Home");
        label2.setFont(new Font("Helvetica", Font.PLAIN, 70));
        
        //adding the labels to the panels
        panel.add(label);
        panelAfterButtonClick.add(label2);
        
        //button that is displaying both before and after
        JButton button = new JButton("Click Me..");
        JButton buttonAfterClick = new JButton("Clicked Me..");
        
        //adding the buttons to the jpanel
        this.addobjects(label, panel, ourlayout, gbc, 0,0, 3, 1);
        this.addobjects(button, panel, ourlayout, gbc, 700, 100, 2, 0);

        this.addobjects(label2, panelAfterButtonClick, ourlayout, gbc, 200, 200, 1, 1);
        this.addobjects(buttonAfterClick, panelAfterButtonClick, ourlayout, gbc, 700, 10, 2, 0);
        
        //function that changes the panel after the button is clicked
        button.addActionListener(new ActionListener() {

              public void actionPerformed(ActionEvent event) {
                  frame.setContentPane(panelAfterButtonClick);
                  frame.invalidate();
                  frame.validate();
              }

            });
        //adding the panel to the frame and setting the size
        frame.add(panel);
        frame.setSize(720,1280);
        frame.setVisible(true);
    }
}
public static void main (String args[]) {
        HomeScreenUI hs = new HomeScreenUI();
}
2
That code is missing import statements and a main method. For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example.Andrew Thompson
Use a CardLayout to switch between JPanels. Gbc parameters gridx and gridy start at 0, 0 and increment by one, not several hundred.Gilbert Le Blanc

2 Answers

0
votes

It seems you are using GridBagLayout completely different than it was designed for. Note you have one instance of GridBagLayout but want to use it for two panels. Each panel needs to have it's own LayoutManager instance.

Then look how many GridBagConstraints you have. Each Component that shall be added needs it's own instance to be properly managed.

Then there are strange values you are passing into GridBagConstraints. I suggest you take the time and go through How to Use GridBagLayout.

0
votes

I modified your code to create the following GUI.

Opisa GUI

Here's what it looks like after you left-click the button.

Home GUI

You can swap back and forth between the two panels.

Here are the major changes I made to your code.

  1. Code should be organized like an essay. The most important code should come first, followed by the less important code.

  2. Break your code up into methods and classes. Each method should do one thing and do it well. This is called separation of concerns and it helps you to focus on one part of your code at a time.

  3. To start the Swing application, I made a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.

  4. The JFrame code is in the class constructor. The JFrame code has to be called in a certain order. This is the order I use for most of my Swing applications. Don't forget to call the setDefaultCloseOperation method. The JFrame.EXIT_ON_CLOSE parameter exits the application when you close the JFrame.

  5. I used a CardLayout to hold the two subordinate JPanels. I created the CardLayout in its own method.

  6. I created each subordinate JPanel in its own method. I used a GridBagLayout for both subordinate JPanels. As you can see in the code, you have to set quite a few GridBagConstraints parameters.

  7. I created an anonymous ActionListener for each of the JButtons. The ActionListeners swap the two subordinate JPanels in the CardLayout.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class HomeScreenUI {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new HomeScreenUI();
            }
        });
    }
    
    private CardLayout cardLayout;
    
    private JPanel cardPanel;
    
    public HomeScreenUI() {
        JFrame frame = new JFrame("Opisa");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.cardPanel = createCardPanel();
        frame.add(cardPanel, BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createCardPanel() {
        cardLayout = new CardLayout();
        JPanel panel = new JPanel(cardLayout);
        
        panel.add(createOpisaPanel(), "Opisa");
        panel.add(createHomePanel(), "Home");
        
        return panel;
    }
    
    private JPanel createOpisaPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.gridx = 0;
        gbc.gridy = 0;

        // jlabel that isnt displaying + dimensions
        JLabel label = new JLabel("Opisa");
        label.setFont(new Font("Helvetica", Font.PLAIN, 72));
        panel.add(label, gbc);
        
        gbc.gridy++;
        JButton button = new JButton("Click Me..");
        // function that changes the panel after the button is clicked
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                cardLayout.show(cardPanel, "Home");
            }
        });
        panel.add(button, gbc);

        return panel;
    }

    private JPanel createHomePanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.gridx = 0;
        gbc.gridy = 0;

        // second jlabel that isn't displaying
        JLabel label2 = new JLabel("Home");
        label2.setFont(new Font("Helvetica", Font.PLAIN, 72));
        panel.add(label2, gbc);
        
        gbc.gridy++;
        JButton buttonAfterClick = new JButton("Clicked Me..");
        buttonAfterClick.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                cardLayout.show(cardPanel, "Opisa");
            }
        });
        panel.add(buttonAfterClick, gbc);

        return panel;
    }
    
}