1

I have a problem with text field and combo box components which are set on the east panel. For some reason, when I added Box layout to arrange them by Y, some of the components listed above, doesn't align and scale size properly with buttons, just as they should be.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 *
 * @author Isaac
 */
public class Test2 extends JFrame {

    private JButton addNewColumnButton;
    private JButton calculateColumnButton;
    private JButton resultButton;

    private JLabel textLabel;

    private JTextField columnField;
    private JTextField resultField;            

    private JComboBox columnListCB;
    private JTable table;

    private String[] tableCols = {"Fisrt Column", "Second Column", "Third Column", "", "", "", "", ""};
    private Object[][] tableRows = {
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null}
    };

    public Test2() {
        this.setSize(new Dimension(600, 280)); 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        this.init();
        this.add(getUIPanel());

        this.pack();
        this.setVisible(true);
    }

    private JPanel getUIPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setBackground(Color.blue);

        JPanel center = new JPanel();
        center.add(table);

        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(Color.MAGENTA);
            Box eastPanelBox = Box.createVerticalBox();
            eastPanelBox.add(addNewColumnButton);
            eastPanelBox.add(Box.createVerticalStrut(14));
            eastPanelBox.add(columnField);
            eastPanelBox.add(Box.createVerticalStrut(5));
            eastPanelBox.add(columnListCB);
            eastPanelBox.add(Box.createVerticalStrut(5));
            eastPanelBox.add(calculateColumnButton);
        eastPanel.add(eastPanelBox);

        JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        southPanel.setBackground(Color.green);
            southPanel.add(resultButton);
            southPanel.add(textLabel);
            southPanel.add(resultField);

        panel.add(center, BorderLayout.WEST);
        panel.add(southPanel, BorderLayout.SOUTH);
        panel.add(eastPanel, BorderLayout.EAST); 
        return panel;
    }

    private void init() {
        final int COMPONENT_WIDTH = 130;
        final int COMPONENT_HEIGHT = 25;

        table = new JTable(tableRows, tableCols);

        addNewColumnButton = new JButton("New Column");
        addNewColumnButton.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        columnField = new JTextField();
        columnField.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        columnListCB = new JComboBox(tableCols);
        columnListCB.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        calculateColumnButton = new JButton("Calculate Column");
        calculateColumnButton.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        resultButton = new JButton("Calculate");
        calculateColumnButton.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        textLabel = new JLabel("Result:");

        resultField = new JTextField(); 
        resultField.setPreferredSize(new Dimension(COMPONENT_WIDTH / 2, COMPONENT_HEIGHT));
    }

    public static void main(String[] args) {
        new Test2();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    What is your question? – Thomas S. Aug 03 '13 at 16:21
  • 1
    I'm confused. 1. Are you talking about alignment **inside** `eastPanel`? 2. Where do you add `BoxLayout` and to what container?? – PM 77-1 Aug 03 '13 at 16:42
  • 1
    Why not just use a GridLayout for your east JPanel? – Hovercraft Full Of Eels Aug 03 '13 at 16:43
  • I think I was very clear, I said pretty much in a first sentence, that I have a problem with a text field and combo box components, that means that something is wrong with this components, alright.. For a second sentence, you are right, at very begin it's not really clear, but later I said "for components listed above" which are text field and combo box, does not align and scale properly with button components. Have you even copy/paste this code and run, to see what is going on? You will pretty much understand everything, if you did.. I'm sorry, my english sux.. But still, it's understandable. – Isaac Tobin Aug 03 '13 at 20:34
  • @HovercraftFullOfEels Well, I need to have that space of a 10-15px between button and text field so, that's why I'm using Box, but i don't understand if i set below in init method setPreferredSize(), why this components just expand random width, if i set to all 4 of them same size? – Isaac Tobin Aug 03 '13 at 20:52
  • +1 for [sscce](http://sscce.org/). – trashgod Aug 04 '13 at 00:36

1 Answers1

1

Several issues arise in your example:

  • "In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment."

  • Specify the initial size of a JTextField using the appropriate constructor.

  • Don't use setPreferredSize() when you really mean to override getPreferredSize().

  • Invoke pack() and then set the location & visibility.

  • Use initial threads correctly.

  • Don't needlessly extend a top-level component, e.g. JFrame.

  • Avoid this pitfall on non-resizable container.

image

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 * @author Isaac
 * @see https://stackoverflow.com/a/18037704/230513
 */
public class Test2 {

    private JButton addNewColumnButton;
    private JButton calculateColumnButton;
    private JButton resultButton;
    private JLabel textLabel;
    private JTextField columnField;
    private JTextField resultField;
    private JComboBox columnListCB;
    private JTable table;
    private String[] tableCols = {
        "Fisrt Column", "Second Column", "Third Column"
    };
    private Object[][] tableRows = {
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null}
    };

    public Test2() {
        JFrame f = new JFrame("Test2");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(getUIPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JPanel getUIPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setBackground(Color.blue);

        JPanel center = new JPanel(new GridLayout());
        table = new JTable(tableRows, tableCols);
        table.setPreferredScrollableViewportSize(new Dimension(240, 120));
        center.add(new JScrollPane(table));

        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(Color.MAGENTA);
        Box eastPanelBox = Box.createVerticalBox();
        addNewColumnButton = new JButton("New Column");
        addNewColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(addNewColumnButton);
        eastPanelBox.add(Box.createVerticalStrut(14));
        columnField = new JTextField();
        columnField.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(columnField);
        eastPanelBox.add(Box.createVerticalStrut(5));
        columnListCB = new JComboBox(tableCols);
        columnListCB.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(columnListCB);
        eastPanelBox.add(Box.createVerticalStrut(5));
        calculateColumnButton = new JButton("Calculate Column");
        calculateColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(calculateColumnButton);
        eastPanel.add(eastPanelBox);

        JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        southPanel.setBackground(Color.green);
        resultButton = new JButton("Calculate");
        southPanel.add(resultButton);
        textLabel = new JLabel("Result:");
        southPanel.add(textLabel);
        resultField = new JTextField(10);
        southPanel.add(resultField);

        panel.add(center, BorderLayout.WEST);
        panel.add(southPanel, BorderLayout.SOUTH);
        panel.add(eastPanel, BorderLayout.EAST);
        return panel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test2();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hey again, sorry I forgot one more thing to ask, how can I move components in a line with a table with out disrupting a gap between components of 5px? Check screenshot here, if I wasn't clear http://imageshack.us/photo/my-images/89/yl8o.jpg/ – Isaac Tobin Aug 04 '13 at 10:40
  • Hard to say; this might be a new question; check out [nested layouts](http://stackoverflow.com/a/5630271/230513). – trashgod Aug 04 '13 at 11:15