Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Putting spaces between formatted text fields (http://www.programmingforums.org/showthread.php?t=15126)

Fall Back Son Feb 6th, 2008 1:16 AM

Putting spaces between formatted text fields
 
:

package components;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import java.text.*;

/**
 *
 *
 */
public class SetManualMatch extends JPanel
                                    implements PropertyChangeListener{

        // Strings to get the text input to the text field
        private String team1;
        private String team2;
        private String team3;
        private String team4;
        private String team5;
        private String team6;

    //Labels to identify the fields
    private JLabel teamLabel1;
    private JLabel teamLabel2;
    private JLabel teamLabel3;

    //Strings for the labels
    private static String versus = "vs.";

    //Fields for data entry
    private JFormattedTextField teamField1;
    private JFormattedTextField teamField2;
    private JFormattedTextField teamField3;
    private JFormattedTextField teamField4;
    private JFormattedTextField teamField5;
    private JFormattedTextField teamField6;

    //Formats to format and parse numbers
    private NumberFormat teamFormat1;
    private NumberFormat teamFormat2;
    private NumberFormat teamFormat3;
    private NumberFormat teamFormat4;
    private NumberFormat teamFormat5;
    private NumberFormat teamFormat6;

    public SetManualMatch() {
        super(new BorderLayout());
        setUpFormats();
     

        //Create the labels.
        teamLabel1 = new JLabel(versus);
        teamLabel2 = new JLabel(versus);
        teamLabel3 = new JLabel(versus);

        //Create the text fields and set them up.
        teamField1 = new JFormattedTextField(teamFormat1);
        teamField1.setText("");
        teamField1.setColumns(10);
        teamField1.addPropertyChangeListener("value", this);

        teamField2 = new JFormattedTextField(teamFormat2);
        teamField2.setText("");
        teamField2.setColumns(10);
        teamField2.addPropertyChangeListener("value", this);

        teamField3 = new JFormattedTextField(teamFormat3);
        teamField3.setText("");
        teamField3.setColumns(10);
        teamField3.addPropertyChangeListener("value", this);

        teamField4 = new JFormattedTextField(teamFormat4);
        teamField4.setText("");
        teamField4.setColumns(10);
        teamField4.addPropertyChangeListener("value", this);
       
        teamField5 = new JFormattedTextField(teamFormat5);
        teamField5.setText("");
        teamField5.setColumns(10);
        teamField5.addPropertyChangeListener("value", this);
       
        teamField6 = new JFormattedTextField(teamFormat6);
        teamField6.setText("");
        teamField6.setColumns(10);
        teamField6.addPropertyChangeListener("value", this);
       

        //Tell accessibility tools about label/textfield pairs.
        teamLabel1.setLabelFor(teamField1);
        teamLabel2.setLabelFor(teamField3);
        teamLabel3.setLabelFor(teamField5);

        //Lay out the labels in a panel.
        JPanel labelPane = new JPanel(new GridLayout(0,1));
        labelPane.add(teamLabel1);
        labelPane.add(teamLabel2);
        labelPane.add(teamLabel3);

        //Layout the text fields in a panel.
        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(teamField1);
        fieldPane.add(teamField2);
        fieldPane.add(teamField3);
        fieldPane.add(teamField4);
        fieldPane.add(teamField5);
        fieldPane.add(teamField6);

        //Put the panels in this panel, labels on left,
        //text fields on right.
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(labelPane, BorderLayout.CENTER);
        add(fieldPane, BorderLayout.LINE_END);
    }

    /** Called when a field's "value" property changes. */
    public void propertyChange(PropertyChangeEvent e) {
        Object source = e.getSource();
        if (source == teamField1) {
          team1 = teamField1.getText();
          System.out.println(team1);
        } else if (source == teamField2) {
          team2 = teamField2.getText();
          System.out.println(team2);
        } else if (source == teamField3) {
                team3 = teamField3.getText();
            System.out.println(team3);
        } else if (source == teamField4){
                team4 = teamField4.getText();
            System.out.println(team4);
        } else if (source == teamField5) {
                team5 = teamField5.getText();
            System.out.println(team5);
        } else if (source == teamField6) {
                team6 = teamField6.getText();
            System.out.println(team6);
        }

    }

   
    /**
    * Create the GUI and show it.  For thread safety,
    * this method should be invoked from the
    * event dispatch thread.
    */
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Change Or Add Team");
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        //Add contents to the window.
        frame.add(new SetManualMatch());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
   
 
   


    //Create and set up number formats. These objects also
    //parse numbers input by user.
    private void setUpFormats() {

    }
}

If you run this program, it will produce 6 blank text fields which are grouped into sets of two (the word "vs." is between each). I want to put whitespace inbetween each set of two. How can I accomplish that?

(To avoid reading all of this code, read my post below this one -- I think it will be sufficient to answer the question and it is much less code. But just in case it isn't, I've posted the whole program)

Fall Back Son Feb 6th, 2008 1:30 AM

Re: Putting spaces between formatted text fields
 
In particular, this code (from what I posted above) lays out my text fields into a panel.


:

       
        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(teamField1);
        fieldPane.add(teamField2);
        fieldPane.add(teamField3);
        fieldPane.add(teamField4);
        fieldPane.add(teamField5);
        fieldPane.add(teamField6);


I want to add whitespace between: teamField2 and teamField3; teamField4 and teamField5. But I also want the word "vs." to be clearly associated with each pair (i.e., sufficiently inbetween 1 & 2, 3 & 4, and 5 & 6). I hope that makes sense.

mrynit Feb 6th, 2008 2:46 AM

Re: Putting spaces between formatted text fields
 
you have lots of repeted code. consider using an array list to hold all the textfileds and making a method that creats all the text fields and adds the action listerns to the text fields. you still have to manual creat each text feildgenTextfeild(new JTextField(""));

why did you did you extend jpanel and not jframe?

Fall Back Son Feb 7th, 2008 5:34 PM

Re: Putting spaces between formatted text fields
 
There's no repeated code. There are two containers, one to hold labels, and one to hold text fields. This ensures that they are aligned properly. The only unnecessary code is setUpFormats() and the code that creates formats, but I will need it later so I left it there. And you didn't answer the question.

Fall Back Son Feb 12th, 2008 2:16 AM

Re: Putting spaces between formatted text fields
 
No one else? Just letting anyone who reads know I'm around and will read replies. There's probably an easy solution that I haven't thought of. Thanks for any replies.

mrynit Feb 12th, 2008 2:22 AM

Re: Putting spaces between formatted text fields
 
yah know you can put vertical spacing for GridLayout.... and use panels for group of two texfeilds

Fall Back Son Feb 12th, 2008 3:01 PM

Re: Putting spaces between formatted text fields
 
thats what I already did, as far as I remember. Haven't looked at it lately though. But that will probably do it. I think I'll just add a label thats just a blank string to space it.

mrynit Feb 12th, 2008 9:40 PM

Re: Putting spaces between formatted text fields
 
Quote:

Originally Posted by Fall Back Son (Post 140990)
thats what I already did, as far as I remember.

no you didn't
JPanel labelPane = new JPanel(new GridLayout(0,1));
http://java.sun.com/javase/6/docs/ap...,%20int,%20int


All times are GMT -5. The time now is 3:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC