Solved

Justify JLabels

Posted on 2004-03-30
4
1,126 Views
Last Modified: 2007-12-19
hi!

I want to right justify all these labels.
Somehow, this doesn't work.
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   JLabel minXLabel = new JLabel("Min X Value: ", SwingConstants.RIGHT);
    JLabel maxXLabel = new JLabel("Max X Value: ", SwingConstants.RIGHT);
    JLabel xTickLabel = new JLabel("X tick spacing: ", SwingConstants.RIGHT);
    JLabel minYLabel = new JLabel("Min Y Value: ", SwingConstants.RIGHT);
    JLabel maxYLabel = new JLabel("Max Y Value: ", SwingConstants.RIGHT);
    JLabel yTickLabel = new JLabel("Y tick spacing: ", SwingConstants.RIGHT);

    minXTField = new JTextField(4);
    maxXTField = new JTextField(4);
    xTickTField = new JTextField(4);
    minYTField = new JTextField(4);
    maxYTField = new JTextField(4);
    yTickTField = new JTextField(4);

    arrageComponent(minXLabel, 0, 0, 1, 1);
    axisSpacingPanel.add(minXLabel, constraints);

    arrageComponent(minXTField, 1, 0, 1, 1);
    axisSpacingPanel.add(minXTField, constraints);

    arrageComponent(maxXLabel, 0, 1, 1, 1);
    axisSpacingPanel.add(maxXLabel, constraints);

    arrageComponent(maxXTField, 1, 1, 1, 1);
    axisSpacingPanel.add(maxXTField, constraints);

    arrageComponent(xTickLabel, 0, 2, 1, 1);
    axisSpacingPanel.add(xTickLabel, constraints);

    arrageComponent(xTickTField, 1, 2, 1, 1);
    axisSpacingPanel.add(xTickTField, constraints);

    arrageComponent(minYLabel, 0, 3, 1, 1);
    axisSpacingPanel.add(minYLabel, constraints);

    arrageComponent(minYTField, 1, 3, 1, 1);
    axisSpacingPanel.add(minYTField, constraints);

    arrageComponent(maxYLabel, 0, 4, 1, 1);
    axisSpacingPanel.add(maxYLabel, constraints);

    arrageComponent(maxYTField, 1, 4, 1, 1);
    axisSpacingPanel.add(maxYTField, constraints);

    arrageComponent(yTickLabel, 0, 5, 1, 1);
    axisSpacingPanel.add(yTickLabel, constraints);

    arrageComponent(yTickTField, 1, 5, 1, 1);
    axisSpacingPanel.add(yTickTField, constraints);

+++++++++++++++++++++++++++++++++++++++
what did I do wrong?
0
Comment
Question by:dkim18
  • 2
4 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10720923
can you post the entire code.

Seems like there is no problem with the constants used for RIGHT Justification.


Regards,
Muruga
0
 

Author Comment

by:dkim18
ID: 10720991
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TabbedPane
    extends JPanel {
  //implements ActionListener {
  private JTabbedPane tabbedPane;
  //private JPanel btmPanel;
  private JPanel axisSpacingPanel;
  private JPanel axisFormatPanel;
  private JPanel axisTitlesPanel;
  private JPanel sizePanel;
  private JPanel sinDataPanel;
  private JPanel cosDataPanel;
  private boolean wrappedTabs = true;
  Dimension component_dimen = new Dimension(300, 300);

  private GridBagConstraints constraints;
  private JTextField wtTField;
  private JTextField htTField;

  private JTextField minXTField;
  private JTextField maxXTField;
  private JTextField xTickTField;
  private JTextField minYTField;
  private JTextField maxYTField;
  private JTextField yTickTField;

  //private JPanel mainPanel;
  private JButton verifyBtn;
  //private Container container;
  //private JTabbedpane tabbedPane;

  public TabbedPane() {

    // super(new GridLayout(1, 1));
    setPreferredSize(component_dimen);
    //setSize(300,300);
    setBackground(Color.gray);
    setLayout(new BorderLayout());

    createSizePanel();
    createAxisSpacingPanel();

    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Axis Spacing", axisSpacingPanel);
    tabbedPane.addTab("Axis Format", axisFormatPanel);
    tabbedPane.addTab("Axis Titles", axisTitlesPanel);
    tabbedPane.addTab("Size", sizePanel);
    tabbedPane.addTab("Sin Data", sinDataPanel);
    tabbedPane.addTab("Cos Data", cosDataPanel);

    //mainPanel = new JPanel();

    add(tabbedPane, BorderLayout.CENTER);
    //add(mainPanel);
    JPanel southPanel = new JPanel();
    verifyBtn = new JButton("Verify");
    southPanel.add(verifyBtn);
    add(southPanel, BorderLayout.SOUTH);
    //getSelectedIndex()
    verifyBtn.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("fsdf");
        int selectedTab = tabbedPane.getSelectedIndex();
        if (selectedTab == 0) {
          boolean isWtNum = true;
          boolean isHtNum = true;

          isWtNum = isValidInput(wtTField);
        }
        if (selectedTab == 3) {
          int width = -1;
          int height = -1;
          boolean isWtInt = true;
          boolean isHtInt = true;
          //wtTField.isin
          try {
            width = Integer.parseInt(wtTField.getText());
            //isWtInt = isValidInput(wtTField);
          }
          catch (NumberFormatException e) {
            isWtInt = false;
            //System.out.println(isWtInt);
          }
          try {
            height = Integer.parseInt(htTField.getText());
            //isHtInt = isValidInput(htTField);
          }
          catch (NumberFormatException e) {
            isHtInt = false;
            //System.out.println(isHtInt);
          }

          if ( ( (width <= 0) || ! (isWtInt)) && (height > 0)) {
            JOptionPane.showMessageDialog(null,
                                          "Width must be a positive integer",
                                          "Invalid Input Values",
                                          JOptionPane.ERROR_MESSAGE);
          }
          else if ( ( (height <= 0) || ! (isHtInt)) && (width > 0)) {
            JOptionPane.showMessageDialog(null,
                                          "Height must be a positive integer",
                                          "Invalid Input Values",
                                          JOptionPane.ERROR_MESSAGE);

          }
          else if ( (width <= 0 && height <= 0) ||

                   (isWtInt == false && isHtInt == false)) {
            JOptionPane.showMessageDialog(null,
                "Width must be a positive integer\nHeight must be a positive integer",
                "Invalid Input Values",
                JOptionPane.ERROR_MESSAGE);
          }
        }
        else {}
      }
    }
    );
  }

  public boolean isValidInput(JTextField tf) {
    boolean result = true;
    try {
      Integer.parseInt(tf.getText());
      result = true;
      System.out.println("in try section");
      System.out.println(result);
    }
    catch (Exception ex) {
      result = false;
      System.out.println("in  catch section");
      System.out.println(result);
    }
    System.out.println("before return;");
    System.out.println(result);
    return result;
  }

//---------------
  public void createSizePanel() {
    sizePanel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    sizePanel.setLayout(layout);

    constraints = new GridBagConstraints();
    JLabel wtLabel = new JLabel("Graph Width in pixels:");
    JLabel htLabel = new JLabel("Graph Height in pixels:");
    wtTField = new JTextField("400", 5);
    htTField = new JTextField("400", 5);

    arrageComponent(wtLabel, 0, 0, 1, 1);
    //constraints.anchor = GridBagConstraints.EAST;
    sizePanel.add(wtLabel, constraints);

    arrageComponent(wtTField, 1, 0, 1, 1);
    //constraints.anchor = GridBagConstraints.WEST;
    sizePanel.add(wtTField, constraints);

    arrageComponent(htLabel, 0, 1, 1, 1);
    //constraints.anchor = GridBagConstraints.EAST;
    sizePanel.add(htLabel, constraints);

    arrageComponent(htTField, 1, 1, 1, 1);
    //constraints.anchor = GridBagConstraints.WEST;
    sizePanel.add(htTField, constraints);

    /*
     */
  }

  public void createAxisSpacingPanel() {
    axisSpacingPanel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    axisSpacingPanel.setLayout(layout);

    constraints = new GridBagConstraints();
    JLabel minXLabel = new JLabel("Min X Value: ", SwingConstants.RIGHT);
    JLabel maxXLabel = new JLabel("Max X Value: ", SwingConstants.RIGHT);
    JLabel xTickLabel = new JLabel("X tick spacing: ", SwingConstants.RIGHT);
    JLabel minYLabel = new JLabel("Min Y Value: ", SwingConstants.RIGHT);
    JLabel maxYLabel = new JLabel("Max Y Value: ", SwingConstants.RIGHT);
    JLabel yTickLabel = new JLabel("Y tick spacing: ", SwingConstants.RIGHT);

    minXTField = new JTextField(4);
    maxXTField = new JTextField(4);
    xTickTField = new JTextField(4);
    minYTField = new JTextField(4);
    maxYTField = new JTextField(4);
    yTickTField = new JTextField(4);

    arrageComponent(minXLabel, 0, 0, 1, 1);
    axisSpacingPanel.add(minXLabel, constraints);

    arrageComponent(minXTField, 1, 0, 1, 1);
    axisSpacingPanel.add(minXTField, constraints);

    arrageComponent(maxXLabel, 0, 1, 1, 1);
    axisSpacingPanel.add(maxXLabel, constraints);

    arrageComponent(maxXTField, 1, 1, 1, 1);
    axisSpacingPanel.add(maxXTField, constraints);

    arrageComponent(xTickLabel, 0, 2, 1, 1);
    axisSpacingPanel.add(xTickLabel, constraints);

    arrageComponent(xTickTField, 1, 2, 1, 1);
    axisSpacingPanel.add(xTickTField, constraints);

    arrageComponent(minYLabel, 0, 3, 1, 1);
    axisSpacingPanel.add(minYLabel, constraints);

    arrageComponent(minYTField, 1, 3, 1, 1);
    axisSpacingPanel.add(minYTField, constraints);

    arrageComponent(maxYLabel, 0, 4, 1, 1);
    axisSpacingPanel.add(maxYLabel, constraints);

    arrageComponent(maxYTField, 1, 4, 1, 1);
    axisSpacingPanel.add(maxYTField, constraints);

    arrageComponent(yTickLabel, 0, 5, 1, 1);
    axisSpacingPanel.add(yTickLabel, constraints);

    arrageComponent(yTickTField, 1, 5, 1, 1);
    axisSpacingPanel.add(yTickTField, constraints);

  }

  private void arrageComponent(Component component, int column, int row,
                               int width, int height) {
    constraints.gridx = column;
    constraints.gridy = row;

    constraints.gridwidth = width;
    constraints.gridheight = height;
  }

  //----------------------
  public static void main(String[] args) {

    final JFrame jf = new JFrame("Homework 4");
    TabbedPane hw = new TabbedPane();
    //Graph pp = new Graph( );
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setContentPane(hw);
    jf.pack();
    jf.setVisible(true);

  }

}
//-----------------
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10721185
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 100 total points
ID: 10721266

      private void arrageComponent(Component component, int column, int row, int width, int height)
      {
            constraints.gridx = column;
            constraints.gridy = row;

            constraints.gridwidth = width;
            constraints.gridheight = height;
            
            constraints.anchor = GridBagConstraints.LINE_END;
      }


This will work...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question