Search code examples
javahtmlswingjbuttoncentering

Why is my html code in Swing's JButton off center?


My Swing JButton code looks like this :

  Insets An_Inset=new Insets(0,0,0,0);
  String Content="<Html>"+
                         "  <Table Border=1 Cellspacing=0 Cellpadding=2 Width=48>"+
                         "    <Tr><Td Align=Center BgColor=Blue><Font Size=3> + </Font></Td><Td Align=Center><Font Size=3> + </Font></Td></Tr>"+
                         "    <Tr><Td Align=Center><Font Size=3> + </Font></Td><Td Align=Center><Font Size=3> + </Font></Td></Tr>"+
                         "  </Table>"+
                         "</Html>";
  JButton aButton=new JButton(Content);
  aButton.setFont(new Font(Monospaced,0,16));
  aButton.setPreferredSize(new Dimension(56,56));
  aButton.setEnabled(false);
  aButton.setMargin(An_Inset);
  aButton.setHorizontalAlignment(SwingConstants.CENTER);

But the "+" mark is off center, how to fix it ?

enter image description here


Solution

  • So two main things (maybe 3)

    • Get rid of the setPreferredSize, let the button decide how big it should be based on how the text would be rendered.
    • Get rid of the spaces around the "+", the spaces aren't allowing the centering of the text to become aligned properly (through what ever calculation is been used to determine it)
    • You might consider getting rid of aButton.setFont(new Font("Monospaced", 0, 16)); as well, but that comes down to your needs...

    WithoutFontWithFont

    So, the one on the left is without setFont, the one on the right is with setFont

    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Buttons {
    
        public static void main(String[] args) {
            new Buttons();
        }
    
        public Buttons() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                add(makeButton());
            }
    
            public JButton makeButton() {
                Insets An_Inset = new Insets(0, 0, 0, 0);
                String Content = "<Html>"
                                + "  <Table Border=1 Cellspacing=0 Cellpadding=2 Width=48>"
                                + "    <Tr><Td Align=Center BgColor=Blue><Font Size=3>+</Font></Td><Td Align=Center><Font Size=3>+</Font></Td></Tr>"
                                + "    <Tr><Td Align=Center><Font Size=3>+</Font></Td><Td Align=Center>+</Font></Td></Tr>"
                                + "  </Table>"
                                + "</Html>";
                JButton aButton = new JButton(Content);
                aButton.setFont(new Font("Monospaced", 0, 16));
    //          aButton.setPreferredSize(new Dimension(56, 56));
                aButton.setEnabled(false);
                aButton.setMargin(An_Inset);
                aButton.setHorizontalAlignment(SwingConstants.CENTER);
                return aButton;
            }
    
        }
    
    }
    

    I'm kind of left to wonder if using a GridLayout might be simpler, but I don't really know what it is you are trying to achieve...