I searched the site and couldn't find what I'm looking for. I tried the setAlignmentX(JSpinner.CENTER_ALIGNMENT)
method of JSpinner, and also tried to set the preferred width to 10, but it did not work. I want JSpinners horizontally centered at Jpanel.
Here's my code:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JSpinner[] spinners = new JSpinner[10];
for (int i = 0; i < spinners.length; i++) {
spinners[i] = WidgetFactory.createJSpinnerAndAddToPanel(panel);
}
frame.add(panel, BorderLayout.CENTER);
and its WidgetFactory:
public static JSpinner createJSpinnerAndAddToPanel(JPanel panel) {
JSpinner spinner = new JSpinner();
Dimension preferredSize = spinner.getPreferredSize();
preferredSize.width = 10;
spinner.setPreferredSize(preferredSize);
spinner.setAlignmentX(JSpinner.CENTER_ALIGNMENT);
panel.add(spinner);
return spinner;
}
This is what I get: