3 Replies Latest reply: Oct 16, 2012 1:44 AM by 967983 RSS

    Baseline and Leading

    967983
      I don't see any difference in the output between Baseline(Code 1) and Leading(Code 2) except for difference between the textbox sizes. So what is the difference between Baseline and Landing?

      Baseline(Code 1):
      import java.awt.Component;
      import javax.swing.*;
      import static javax.swing.GroupLayout.Alignment.*;

      public class Find extends JFrame {
      public Find() {
      JLabel label = new JLabel("Find What:");;
      JTextField textField = new JTextField();
      JCheckBox caseCheckBox = new JCheckBox("Match Case");
      JCheckBox wrapCheckBox = new JCheckBox("Wrap Around");
      JCheckBox wholeCheckBox = new JCheckBox("Whole Words");
      JCheckBox backCheckBox = new JCheckBox("Search Backwards");
      JButton findButton = new JButton("Find");
      JButton cancelButton = new JButton("Cancel");

      // remove redundant default border of check boxes - they would hinder
      // correct spacing and aligning (maybe not needed on some look and feels)
      caseCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      wrapCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      wholeCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      backCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

      GroupLayout layout = new GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setAutoCreateGaps(true);
      layout.setAutoCreateContainerGaps(true);

      layout.setHorizontalGroup(layout.createSequentialGroup()
      .addComponent(label)
      .addGroup(layout.createParallelGroup(LEADING)
      .addComponent(textField))

      .addGroup(layout.createParallelGroup(LEADING)
      .addComponent(findButton))
      );

      layout.setVerticalGroup(layout.createSequentialGroup()
      .addComponent(label)
      .addGroup(layout.createParallelGroup(BASELINE)
      .addComponent(textField))

      .addGroup(layout.createParallelGroup(BASELINE)
      .addComponent(findButton))           
      );
      setTitle("Find");
      pack();
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

      public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      try {
      UIManager.setLookAndFeel(
      "javax.swing.plaf.metal.MetalLookAndFeel");
      // "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
      //UIManager.getCrossPlatformLookAndFeelClassName());
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      new Find().setVisible(true);
      }
      });
      }
      }

      Leading(Code 2):
      import java.awt.Component;
      import javax.swing.*;
      import static javax.swing.GroupLayout.Alignment.*;

      public class Find extends JFrame {
      public Find() {
      JLabel label = new JLabel("Find What:");;
      JTextField textField = new JTextField();
      JCheckBox caseCheckBox = new JCheckBox("Match Case");
      JCheckBox wrapCheckBox = new JCheckBox("Wrap Around");
      JCheckBox wholeCheckBox = new JCheckBox("Whole Words");
      JCheckBox backCheckBox = new JCheckBox("Search Backwards");
      JButton findButton = new JButton("Find");
      JButton cancelButton = new JButton("Cancel");

      // remove redundant default border of check boxes - they would hinder
      // correct spacing and aligning (maybe not needed on some look and feels)
      caseCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      wrapCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      wholeCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
      backCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

      GroupLayout layout = new GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setAutoCreateGaps(true);
      layout.setAutoCreateContainerGaps(true);

      layout.setHorizontalGroup(layout.createSequentialGroup()
      .addComponent(label)
      .addGroup(layout.createParallelGroup(LEADING)
      .addComponent(textField))

      .addGroup(layout.createParallelGroup(LEADING)
      .addComponent(findButton))
      );

      layout.setVerticalGroup(layout.createSequentialGroup()
      .addComponent(label)
      .addGroup(layout.createParallelGroup(LEADING)
      .addComponent(textField))

      .addGroup(layout.createParallelGroup(LEADING)
      .addComponent(findButton))           
      );
      setTitle("Find");
      pack();
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

      public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      try {
      UIManager.setLookAndFeel(
      "javax.swing.plaf.metal.MetalLookAndFeel");
      // "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
      //UIManager.getCrossPlatformLookAndFeelClassName());
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      new Find().setVisible(true);
      }
      });
      }
      }
        • 1. Re: Baseline and Leading
          800268
          Your layout is a bit of the mess, the following example shows the difference between base alignment or not.
          import java.awt.BorderLayout;
          import java.awt.Component;
          import java.awt.EventQueue;
          import java.awt.FlowLayout;
          
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JPanel;
          
          public class TestBaselIne {
              public static void main(String[] args) {
                  EventQueue.invokeLater(new Runnable() {
                      @Override
                      public void run() {
                          JFrame frame = new JFrame("Test");
                          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                          frame.getContentPane().add(createPanel(true), BorderLayout.PAGE_START);
                          frame.getContentPane().add(createPanel(false), BorderLayout.PAGE_END);
                          frame.pack();
                          frame.setLocationRelativeTo(null);
                          frame.setVisible(true);
                      }
          
                      private Component createPanel(boolean baseline) {
                          FlowLayout layout = new FlowLayout();
                          layout.setAlignOnBaseline(baseline);
                          JLabel label = new JLabel("Big font");
                          label.setFont(label.getFont().deriveFont(24f));
          
                          JPanel panel = new JPanel(layout);
                          panel.add(label);
                          panel.add(new JLabel("Small font"));
                          return panel;
                      }
                  });
              }
          }
          • 2. Re: Baseline and Leading
            Kleopatra
            Please format your code (the sticky post at the forum top explains how-to)

            Cheers
            Jeanette
            • 3. Re: Baseline and Leading
              967983
              Ok, your code shows the difference but what is wrong in my code. Also, If something is not baseline then does that mean it is leading? I am still not clear about the difference between baseline and leading.

              Baseline(Code 1):
              import java.awt.Component;
              import javax.swing.*;
              import static javax.swing.GroupLayout.Alignment.*;
              
              public class Find extends JFrame {
                  public Find() {
                      JLabel label = new JLabel("Find What:");;
                      JTextField textField = new JTextField();
                      JCheckBox caseCheckBox = new JCheckBox("Match Case");
                      JCheckBox wrapCheckBox = new JCheckBox("Wrap Around");
                      JCheckBox wholeCheckBox = new JCheckBox("Whole Words");
                      JCheckBox backCheckBox = new JCheckBox("Search Backwards");
                      JButton findButton = new JButton("Find");
                      JButton cancelButton = new JButton("Cancel");
              
                      // remove redundant default border of check boxes - they would hinder
                      // correct spacing and aligning (maybe not needed on some look and feels)
                      caseCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                      wrapCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                      wholeCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                      backCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
              
                      GroupLayout layout = new GroupLayout(getContentPane());
                      getContentPane().setLayout(layout);
                      layout.setAutoCreateGaps(true);
                      layout.setAutoCreateContainerGaps(true);        
                              
                      layout.setHorizontalGroup(layout.createSequentialGroup()
                          .addComponent(label)
                          .addGroup(layout.createParallelGroup(LEADING)
                              .addComponent(textField))
                             
                          .addGroup(layout.createParallelGroup(LEADING)
                              .addComponent(findButton))
                      );
                      
                      layout.setVerticalGroup(layout.createSequentialGroup()
                              .addComponent(label)
                              .addGroup(layout.createParallelGroup(BASELINE)
                                  .addComponent(textField))
                                 
                              .addGroup(layout.createParallelGroup(BASELINE)
                                  .addComponent(findButton))                  
                      );       
                      setTitle("Find");
                      pack();
                      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  }
                  
                  public static void main(String args[]) {
                      java.awt.EventQueue.invokeLater(new Runnable() {
                          public void run() {
                              try {
                                  UIManager.setLookAndFeel(
                                                "javax.swing.plaf.metal.MetalLookAndFeel");
                                              //  "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                                              //UIManager.getCrossPlatformLookAndFeelClassName());
                              } catch (Exception ex) {
                                  ex.printStackTrace();
                              }
                              new Find().setVisible(true);
                          }
                      });
                  }
              }
              Leading(Code 2):
              import java.awt.Component;
              import javax.swing.*;
              import static javax.swing.GroupLayout.Alignment.*;
              
              public class Find extends JFrame {
                  public Find() {
                      JLabel label = new JLabel("Find What:");;
                      JTextField textField = new JTextField();
                      JCheckBox caseCheckBox = new JCheckBox("Match Case");
                      JCheckBox wrapCheckBox = new JCheckBox("Wrap Around");
                      JCheckBox wholeCheckBox = new JCheckBox("Whole Words");
                      JCheckBox backCheckBox = new JCheckBox("Search Backwards");
                      JButton findButton = new JButton("Find");
                      JButton cancelButton = new JButton("Cancel");
              
                      // remove redundant default border of check boxes - they would hinder
                      // correct spacing and aligning (maybe not needed on some look and feels)
                      caseCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                      wrapCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                      wholeCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                      backCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
              
                      GroupLayout layout = new GroupLayout(getContentPane());
                      getContentPane().setLayout(layout);
                      layout.setAutoCreateGaps(true);
                      layout.setAutoCreateContainerGaps(true);        
                              
                      layout.setHorizontalGroup(layout.createSequentialGroup()
                          .addComponent(label)
                          .addGroup(layout.createParallelGroup(LEADING)
                              .addComponent(textField))
                             
                          .addGroup(layout.createParallelGroup(LEADING)
                              .addComponent(findButton))
                      );
                      
                      layout.setVerticalGroup(layout.createSequentialGroup()
                              .addComponent(label)
                              .addGroup(layout.createParallelGroup(LEADING)
                                  .addComponent(textField))
                                 
                              .addGroup(layout.createParallelGroup(LEADING)
                                  .addComponent(findButton))                  
                      );       
                      setTitle("Find");
                      pack();
                      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  }
                  
                  public static void main(String args[]) {
                      java.awt.EventQueue.invokeLater(new Runnable() {
                          public void run() {
                              try {
                                  UIManager.setLookAndFeel(
                                                "javax.swing.plaf.metal.MetalLookAndFeel");
                                              //  "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                                              //UIManager.getCrossPlatformLookAndFeelClassName());
                              } catch (Exception ex) {
                                  ex.printStackTrace();
                              }
                              new Find().setVisible(true);
                          }
                      });
                  }
              }
              Edited by: 964980 on Oct 15, 2012 11:39 PM