Ruchir Baronia
Ruchir Baronia

Reputation: 7571

Border Layout not working?

I have a simple JLabel (response) that I want to put in the center of my JFrame, based on user interaction from a JCombobox. The JLabel response should be in the center of the window, and the JLabel selectone should be next to the combobox; currently all three are next to each other. First, I tried to use borderlayout directly with the JLabel while adding it to the JFrame, but that didn't work. Then I tried to add it while instantiating the JLabel itself, and then just adding the JLabel. Then I tried to add the JLabel to a JPanel, and adding the JPanel to the JFrame, but that didn't work either, and all three had the same result of all three next to each other. Here is my code:

package Buttons;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class sentencewindow extends JFrame {

public static void main(String[] args) {
    sentence senobj = new sentence();
    senobj.setVisible(true);
    senobj.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    senobj.setSize(500, 600);
}

}

class sentence extends JFrame {
String[] abc = { "First string", "Second String", "Third String" };
JComboBox combo = new JComboBox(abc);
JLabel selectone = new JLabel("This should be next to the combobox");
JLabel response = new JLabel("This should change based on combobox selection");

sentence() {
    super("TITLE");

    setLayout(new FlowLayout());

    combo.addItemListener(new ItemChangeListener());
    JPanel container = new JPanel();
    response.setSize(250, 250);
    container.add(response, BorderLayout.CENTER);
    add(container, BorderLayout.CENTER);
    add (selectone);
    add(combo);

}

class ItemChangeListener implements ItemListener {
    public void itemStateChanged(ItemEvent event) {

        if (combo.getSelectedItem().equals(abc[0])) {
            response.setText("You pressed the first option.");

        }
        if (combo.getSelectedItem().equals(abc[1])) {
            response.setText("You pressed the second option.");
        }
        if (combo.getSelectedItem().equals(abc[2])) {
            response.setText("You pressed the third option.");
        }
    }
}
}

Thank you so much for the time you are taking for reading this, I really appreciate the effort you are putting into helping a fellow programmer! I understand that this is a beginner question, but I have been stuck on it for a very long time, and have read pretty much all the questions related to this. Please help, thanks.

Upvotes: 1

Views: 407

Answers (1)

Dakshinamurthy Karra
Dakshinamurthy Karra

Reputation: 5463

What you are looking for is JLabel#setHorizontalAlignment for centering the text in the label itself.

Add Frame with border layout, add a panel with your combobox and label to the NORTH of the frame. Add label to CENTER of frame. The label occupies the full available space and the centre text option should centre the text within the label.

Upvotes: 2

Related Questions