http://java.net/jira/browse/MACOSX_PORT-266 submitted 2011/08/11 by Igor Stolyarov
Swing GUI elements placed into container with FlowLayout manager have different baselines.
Test for reproducing issue:
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class FlowLayoutTest {
private JComboBox jcb;
private JRadioButton jrb;
private JCheckBox jckb;
private JTextField jtf;
private JSpinner jsp;
private boolean status = false;
public FlowLayoutTest() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() { createAndShowGUI(); }
});
}
private void createAndShowGUI() {
JFrame frame = new JFrame("FlowLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
FlowLayout layout = new FlowLayout();
layout.setAlignOnBaseline(true);
panel.setLayout(layout);
jcb = new JComboBox(new String[]{"red", "yellow", "green"});
jcb.setFont(new Font("Monospaced", Font.PLAIN, 12));
jrb = new JRadioButton("Radio Button");
jrb.setFont(new Font("Times New Roman", Font.BOLD, 10));
jckb = new JCheckBox("Check Box");
jckb.setFont(new Font("Sans Serif", Font.BOLD, 25));
jtf = new JTextField("Hello World!", 40);
jtf.setFont(new Font("Times New Roman", Font.BOLD, 10));
jsp = new JSpinner();
jsp.setFont(new Font("Times New Roman", Font.BOLD, 40));
panel.add(jrb);
panel.add(jckb);
panel.add(jcb);
panel.add(jtf);
panel.add(jsp);
frame.getContentPane().add(panel);
frame.doLayout();
frame.pack();
frame.setVisible(true);
}
public void test() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() { int x = jcb.getBaseline(jcb.getWidth(), jcb.getHeight()) + jcb.getY(); int y = jtf.getBaseline(jtf.getWidth(), jtf.getHeight()) + jtf.getY(); int z = jckb.getBaseline(jckb.getWidth(), jckb.getHeight()) + jckb.getY(); int a = jrb.getBaseline(jrb.getWidth(), jrb.getHeight()) + jrb.getY(); int b = jsp.getBaseline(jsp.getWidth(), jsp.getHeight()) + jsp.getY(); status = x == y && x == z && x == a && x == b; }
});
}
public int checkStatus() {
if(status) { System.out.println("TEST PASS!"); return 0; }
System.out.println("TEST FAIL!");
return 1;
}
public static void main(String[] argv) throws Exception { FlowLayoutTest test = new FlowLayoutTest(); test.test(); System.exit(test.checkStatus()); }
}
Swing GUI elements placed into container with FlowLayout manager have different baselines.
Test for reproducing issue:
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class FlowLayoutTest {
private JComboBox jcb;
private JRadioButton jrb;
private JCheckBox jckb;
private JTextField jtf;
private JSpinner jsp;
private boolean status = false;
public FlowLayoutTest() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() { createAndShowGUI(); }
});
}
private void createAndShowGUI() {
JFrame frame = new JFrame("FlowLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
FlowLayout layout = new FlowLayout();
layout.setAlignOnBaseline(true);
panel.setLayout(layout);
jcb = new JComboBox(new String[]{"red", "yellow", "green"});
jcb.setFont(new Font("Monospaced", Font.PLAIN, 12));
jrb = new JRadioButton("Radio Button");
jrb.setFont(new Font("Times New Roman", Font.BOLD, 10));
jckb = new JCheckBox("Check Box");
jckb.setFont(new Font("Sans Serif", Font.BOLD, 25));
jtf = new JTextField("Hello World!", 40);
jtf.setFont(new Font("Times New Roman", Font.BOLD, 10));
jsp = new JSpinner();
jsp.setFont(new Font("Times New Roman", Font.BOLD, 40));
panel.add(jrb);
panel.add(jckb);
panel.add(jcb);
panel.add(jtf);
panel.add(jsp);
frame.getContentPane().add(panel);
frame.doLayout();
frame.pack();
frame.setVisible(true);
}
public void test() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() { int x = jcb.getBaseline(jcb.getWidth(), jcb.getHeight()) + jcb.getY(); int y = jtf.getBaseline(jtf.getWidth(), jtf.getHeight()) + jtf.getY(); int z = jckb.getBaseline(jckb.getWidth(), jckb.getHeight()) + jckb.getY(); int a = jrb.getBaseline(jrb.getWidth(), jrb.getHeight()) + jrb.getY(); int b = jsp.getBaseline(jsp.getWidth(), jsp.getHeight()) + jsp.getY(); status = x == y && x == z && x == a && x == b; }
});
}
public int checkStatus() {
if(status) { System.out.println("TEST PASS!"); return 0; }
System.out.println("TEST FAIL!");
return 1;
}
public static void main(String[] argv) throws Exception { FlowLayoutTest test = new FlowLayoutTest(); test.test(); System.exit(test.checkStatus()); }
}