![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
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() {
}
}(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. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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 feild
genTextfeild(new JTextField(""));why did you did you extend jpanel and not jframe?
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
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.
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
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.
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
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.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
Re: Putting spaces between formatted text fields
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |