-1

I have tried a lot to create one the following layout, but I am not able to. I tried using all sort of constraints but still I am not able to.

I want the following layout.

__________________________
| JLABEL          |  J   |
|_________________|  B   |
| JBUTTON         |  T   |
|_________________|__N___|

JBTN is JBUTTON.

What result I got is they all are coming in one line only. Or sometimes JBTN will come at the left bottom and JLABEL & JBUTTON up in one line.

So overall I am not able to get the desire layout. Can anyone help here or give some advice how to fix it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ashish
  • 358
  • 2
  • 4
  • 15
  • 1
    `Can anyone help here or give some advice how to fix it?` - start by reading the Swing tutorial on [How to Use GridBagLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) for explanation of the constraints and a working example to get you started. The example shows how to have a component "span the width" of multiple cells. In you case you want to "span the height" of multiple cells. 2) if you still have a problem then post your [mcve] demonstrating the problem. We can't guess what you are doing. – camickr Apr 05 '18 at 17:09

1 Answers1

0

This seems like the layout you want:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class Answer extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Answer frame = new Answer();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Answer() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{302, 89, 0};
        gbl_contentPane.rowHeights = new int[]{115, 128, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JLabel lblNewLabel = new JLabel("New label");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        contentPane.add(lblNewLabel, gbc_lblNewLabel);

        JButton btnNewButton = new JButton("New button");
        GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
        gbc_btnNewButton.anchor = GridBagConstraints.WEST;
        gbc_btnNewButton.fill = GridBagConstraints.VERTICAL;
        gbc_btnNewButton.gridheight = 2;
        gbc_btnNewButton.gridx = 1;
        gbc_btnNewButton.gridy = 0;
        contentPane.add(btnNewButton, gbc_btnNewButton);

        JButton btnNewButton_1 = new JButton("New button");
        GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
        gbc_btnNewButton_1.fill = GridBagConstraints.BOTH;
        gbc_btnNewButton_1.insets = new Insets(0, 0, 0, 5);
        gbc_btnNewButton_1.gridx = 0;
        gbc_btnNewButton_1.gridy = 1;
        contentPane.add(btnNewButton_1, gbc_btnNewButton_1);
    }

}