As @mKorbel comments, the five pixel vertical space is added by the default layout of JPanel
, FlowLayout
. The image below shows vgap
set to zero.
JPanel eastPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
Domanda
How can I align components in a line with a table without affecting a gap between components of a 5px? I had an idea to set value for hgap of FlowLayout to zero, but if I do it will change a gap also between components... Here is the screenshot and code, if I wasn't clear:
How I want:
How it is:
Code:
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 java.awt.event.KeyEvent;
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;
public class Test3 {
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 = {
"First 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 Test3() {
JFrame f = new JFrame("Test2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(getPanelComponents());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private JPanel getPanelComponents() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel center = new JPanel(new GridLayout());
table = new JTable(tableRows, tableCols);
table.setPreferredScrollableViewportSize(new Dimension(450, 128));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
center.add(new JScrollPane(table));
JPanel eastPanel = new JPanel();
Box eastPanelBox = Box.createVerticalBox();
addNewColumnButton = new JButton("Add New Column");
addNewColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(addNewColumnButton);
eastPanelBox.add(Box.createVerticalStrut(35));
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));
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 Test3();
}
});
}
}
Soluzione
As @mKorbel comments, the five pixel vertical space is added by the default layout of JPanel
, FlowLayout
. The image below shows vgap
set to zero.
JPanel eastPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));