Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 6th, 2008, 12:16 AM   #1
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 262
Rep Power: 2 Fall Back Son is on a distinguished road
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)

Last edited by Fall Back Son; Feb 6th, 2008 at 12:31 AM.
Fall Back Son is offline   Reply With Quote
Old Feb 6th, 2008, 12:30 AM   #2
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 262
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Feb 6th, 2008, 1:46 AM   #3
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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?
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Feb 7th, 2008, 4:34 PM   #4
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 262
Rep Power: 2 Fall Back Son is on a distinguished road
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 is offline   Reply With Quote
Old Feb 12th, 2008, 1:16 AM   #5
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 262
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Feb 12th, 2008, 1:22 AM   #6
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: Putting spaces between formatted text fields

yah know you can put vertical spacing for GridLayout.... and use panels for group of two texfeilds
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Feb 12th, 2008, 2:01 PM   #7
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 262
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Feb 12th, 2008, 8:40 PM   #8
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: Putting spaces between formatted text fields

Quote:
Originally Posted by Fall Back Son View Post
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
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
text files auto imported into fields Micskill HTML / XHTML / CSS 1 Nov 26th, 2007 9:24 PM
using Text fields in SWT elford Java 4 Dec 27th, 2005 12:38 PM
Resizing fields to match text Arla C# 3 Aug 4th, 2005 1:56 PM
How to detect cursor location and insert text??? syntax-error C# 3 Jun 30th, 2005 1:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:59 PM.

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