Solved

Justify JLabels

Posted on 2004-03-30
4
1,118 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:mayankeagle
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now