1

I got an example for JToggleButton from java2s. In the code, I see that a toggle button does not display text properly if the text is "West". West is shown as We.. . Everything else is okay. But there is no problem when text = "west", ie w small. Is this a bug?

enter image description here

Code:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class MainClass {
 public static void main(String args[]) {
 JFrame f = new JFrame("JToggleButton Sample");
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 f.add(new JToggleButton("North"), BorderLayout.NORTH);
 f.add(new JToggleButton("East"), BorderLayout.EAST);
 f.add(new JToggleButton("West"), BorderLayout.WEST);
 f.add(new JToggleButton("Center"), BorderLayout.CENTER);
 f.add(new JToggleButton("South"), BorderLayout.SOUTH);
 f.setSize(300, 200);
 f.setVisible(true);
 }
}
SuperStar
  • 495
  • 2
  • 9
  • 22

2 Answers2

2

Seems to work just fine for me...

enter image description hereenter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout23 {

    public static void main(String[] args) {
        new TestLayout23();
    }

    public TestLayout23() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame f = new JFrame("JToggleButton Sample");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new JToggleButton("North"), BorderLayout.NORTH);
                f.add(new JToggleButton("East"), BorderLayout.EAST);
                f.add(new JToggleButton("West"), BorderLayout.WEST);
                f.add(new JToggleButton("Center"), BorderLayout.CENTER);
                f.add(new JToggleButton("South"), BorderLayout.SOUTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }

        });
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • That looks like Mac OS UI. I am on win-DODO-ws. Try removing that set look and feel code. – SuperStar Apr 03 '13 at 21:26
  • @mKorbel - Not sure if I understand you. You mean - Not an issue. It happens only with MetalLookAndFeel on Win7 Java6/7. Right ? – SuperStar Apr 03 '13 at 21:30
  • see my quick test, its regular bug, – mKorbel Apr 03 '13 at 21:38
  • @SuperStar Don't be fictitious. You did not mention any where in you answer OS or Look and Feel – MadProgrammer Apr 03 '13 at 22:37
  • @MadProgrammer - sorry, I don't understand. – SuperStar Apr 03 '13 at 22:39
  • @SuperStar Firstly, I've been up since 330am taking care of a baby who won't sleep unless someone is holding her, so I'm a little tetchy, but I find this comment - *I am on win-DODO-ws* slight offensive (the comment it self wasn't really, I just come across as such) (and I'm sure you didn't mean to be), but you didn't mention anything about what platform or look and feel you were testing. As it turns out, you did find a bug - Well done ;) – MadProgrammer Apr 04 '13 at 01:28
2

MetalLookAndFeel - getPreferredSize isn't calculated correctly

enter image description here

SystemLookAndFeel

enter image description here

Nimbus

enter image description here

Substance

enter image description here

from code (same issue with JFrame, JPanel as container by using BorderLayout)

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class MainClass {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            "org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel");
                    //UIManager.getDefaults();
                } catch (Exception e) {
                }
            }
        });

       /*try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("JToggleButton Sample");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                panel.add(new JToggleButton("North"), BorderLayout.NORTH);
                panel.add(new JToggleButton("East"), BorderLayout.EAST);
                panel.add(new JToggleButton("West"), BorderLayout.WEST);
                panel.add(new JToggleButton("Center"), BorderLayout.CENTER);
                panel.add(new JToggleButton("South"), BorderLayout.SOUTH);
                f.add(panel);
                f.pack();
                //f.setSize(300, 200);
                f.setVisible(true);
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319