Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7149090

Nimbus:BorderFactory.createTitledBorder() the DEFAULT position of a title is not the same as the TOP

XMLWordPrintable

    • b36
    • generic
    • generic
    • Verified

      JDK specification for the
       public static TitledBorder createTitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont)
      says:

      "
      Adds a title to an existing border, with the specified positioning and font, and using the default text color (determined by the current look and feel).

      Parameters:
      ...
          titlePosition - an integer specifying the vertical position of the text in relation to the border -- one of the following:

              TitledBorder.ABOVE_TOP
              TitledBorder.TOP (sitting on the top line)
              TitledBorder.BELOW_TOP
              TitledBorder.ABOVE_BOTTOM
              TitledBorder.BOTTOM (sitting on the bottom line)
              TitledBorder.BELOW_BOTTOM
              TitledBorder.DEFAULT_POSITION (top)

          titleFont - a Font object specifying the title font
      ...
      "

      So, if titlePosition is TitledBorder.DEFAULT_POSITION it must looks like TitledBorder.TOP. I.e. "sitting on the top line".
      In that time it looks like TitledBorder.ABOVE_TOP. I.e. the title actually will be above line.

      See the simple code below:

      import javax.swing.*;
      import java.awt.*;
      import javax.swing.border.TitledBorder;

      public class BFTest{

           public static void main (String[] args) throws Exception {
               SwingUtilities.invokeAndWait(new Runnable() {
                   public void run() {
                       createAndShowFrame();
                   }
               });
           }

           private static void createAndShowFrame() {
               JFrame frame = new JFrame();
               frame.setLayout(new BorderLayout());
               JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
               
               JLabel jl = new JLabel("BorderTester");
           jl.setOpaque(true);
           jl.setBackground(Color.green);
               jl.setBorder(BorderFactory.createTitledBorder(
                     BorderFactory.createLineBorder(Color.black),
                     "Title",
                     TitledBorder.LEFT,
                     TitledBorder.DEFAULT_POSITION,
                     new Font("Dialog", Font.BOLD|Font.ITALIC, 16)
                  )
               );
           panel.add(jl);
               frame.getContentPane().add(panel, BorderLayout.CENTER);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setSize(200, 200);
               frame.setLocationRelativeTo(null);
               frame.setVisible(true);
         }
      }

      Compile and execute it with the comand like this:
      # /export/jdk/jdk1.8.0/bin/java -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel BFTest

      You will see the frame with a border where the title position is above line - it is not expected.

      Run the code above without Nimbus L&F:
      # /export/jdk/jdk1.8.0/bin/java BFTest

      You will see that the fram will be opened with the title sitting on the top line as expected.

      So, the method doesn't conform to the specification if Numbus L&F is used.

      The same issue with the BorderFactory.createTitledBorder(border,Title,Justification,Position,Font,Color) method.

            rupashka Pavel Porvatov (Inactive)
            afomin Alexander Fomin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: