Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can same DocumentListener be added to more JTextFields?

23 views
Skip to first unread message

RB Smissaert

unread,
Dec 29, 2001, 8:08:50 AM12/29/01
to
Using JDK1.3.1_02. Is it possible to add the same DocumentListener to more
than one JTextField? I have 10 JTextFields that I want to monitor for any
change. Somehow I don't think I would need 10 classes to deal with the same
thing.

This is the simplified code snippet. It works fine, but it deals only with
changes in the first JTextField.
Thanks for any advice. Please let me know if you have to see the whole code.

RBS


import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.event.*;

public class CHD_Calc extends JFrame {

//define the JTextField array, holding 11 JTextFields
static JTextField JTF[] = new JTextField[11];

public static void main(String args[]) {
CHD_Calc app = new CHD_Calc();
Container c = app.getContentPane();

//setup the frame
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(new GridLayout(2, 11, 8, 4));

// add the listener for the text fields
MyTextListener tList = app.new MyTextListener();

//setup the JTextFields
for (int i = 0; i < 11; i++) {
JTF[i] = new JTextField(7);
c.add(JTF[i]);
JTF[i].addFocusListener(tList);
}

//fill the JTextFields
JTF[0].setText("Male");
JTF[1].setText("50");
//and so on, 11 JTextFields, but only first 10 need monitoring

//this has to be done for the first 10 JTextFields
JTF[0].getDocument().addDocumentListener(new MyDocumentListener());

// set window size and display
app.setSize(720, 100);
app.show();
}

static class MyDocumentListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
JTF[10].setText("");
}
public void removeUpdate(DocumentEvent e) {
JTF[10].setText("");
}
public void changedUpdate(DocumentEvent e) {
JTF[10].setText("");
}
}

Tor Iver Wilhelmsen

unread,
Dec 29, 2001, 8:56:01 AM12/29/01
to
"RB Smissaert" <bartsm...@blueyonder.co.uk> writes:

> Using JDK1.3.1_02. Is it possible to add the same DocumentListener to more
> than one JTextField?

Yes.

--
Tor Iver Wilhelmsen <to...@chello.no>
If you get too cold, I'll tax the heat
If you take a walk, I'll tax your feet.
- The Beatles: Taxman

Nils O. Selåsdal

unread,
Dec 29, 2001, 8:56:22 AM12/29/01
to
In article <CfjX7.946$q35.6...@news-text.cableinet.net>, RB Smissaert wrote:
> Using JDK1.3.1_02. Is it possible to add the same DocumentListener to more
> than one JTextField? I have 10 JTextFields that I want to monitor for any
> change. Somehow I don't think I would need 10 classes to deal with the same
> thing.
No you dont ;)
Ok, do that. But do it like:
MyDocumentListener l = new MyDocumentListener();
JTF[0].getDocument().addDocumentListener(l);
JTF[1].getDocument().addDocumentListener(l);
...

> // set window size and display
> app.setSize(720, 100);
> app.show();
> }
>
> static class MyDocumentListener implements DocumentListener {
> public void insertUpdate(DocumentEvent e) {

((JTextField)e.getSource()).setText("");
> }
> public void removeUpdate(DocumentEvent e) {
((JTextField)e.getSource()).setText("");
> }
> public void changedUpdate(DocumentEvent e) {
((JTextField)e.getSource()).setText("");
> }
> }
>

Christian Kaufhold

unread,
Dec 29, 2001, 9:28:26 AM12/29/01
to
Hello!

RB Smissaert <bartsm...@blueyonder.co.uk> wrote:

> Using JDK1.3.1_02. Is it possible to add the same DocumentListener to more
> than one JTextField? I have 10 JTextFields that I want to monitor for any
> change. Somehow I don't think I would need 10 classes to deal with the same
> thing.

1. Classes != instances. You may have to create 10 instances of the same class,
if your listeners share the same functionality, but have to have some knowledge
about the context (DocumentEvent will only carry the Document, but that is in-
dependent from any JTextComponent(s) that may display it, so it usually isn't
useful).

You don't really describe what your document listener is supposed to do. If
it always should clear the tenth text field, it doesn't need any context, you
can even use the same DocumentListener instance for all text fields.


DocumentListener clearTenthField = new MyDocumentListener();

for (int i = 0; i < 10; i++)
textField[i].getDocument().addDocumentListener(clearTenthField);


Actually, you appear to be doing this for the FocusListener in your code.
Where is the problem in doing it the same way with a DocumentListener?

Christian

RB Smissaert

unread,
Dec 29, 2001, 10:37:59 AM12/29/01
to
Thanks for the reply. Figured it out now.
The problem was that I added a DocumentListener to the last JTextField, just
because it was easy to do in a loop that was already there. This last
JTextField is the one that alters (setText("")) if any of the first 10
JTextFields alters. So I guess that by both listening and changing in the
same JTextField, there was a sort of never ending loop.

This is the error message on running (it compiled fine):

java.lang.IllegalStateException: Attempt to mutate in notification
at
javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1090)
at
javax.swing.text.AbstractDocument.remove(AbstractDocument.java:456)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1159)
at CHD_Calc$dList.insertUpdate(CHD_Calc.java:103)
at
javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:180
)
at
javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:542)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1160)
at CHD_Calc.main(CHD_Calc.java:74)

This is the whole code now:
Please let me know if anything is wrong or could be improved.


//Java core packages


import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.DecimalFormat;

//Java extension packages
import javax.swing.*;
import javax.swing.event.*;

public class CHD_Calc extends JFrame {

//define the JTextField array, holding 11 JTextFields
static JTextField JTF[] = new JTextField[11];

//define the JLabel array, holding 11 JLabels
static JLabel JL[] = new JLabel[11];

//declare custom output format
static DecimalFormat df1 = new DecimalFormat("##.0");

//declare the array holding all the data
static double[] Holder = new double[16];

public static void main(String args[]) {
CHD_Calc app = new CHD_Calc();
Container c = app.getContentPane();

//setup the frame
app.setResizable(false);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setTitle(" CHD Risk Calculator Tab to move to next
field and calculate");


c.setLayout(new GridLayout(2, 11, 8, 4));

// setup the listeners for the JTextFields
FocusListener tList = app.new tList();
DocumentListener dList = app.new dList();

//set the font of the JTextFields
Font currentFont = new Font("TimesRoman", Font.PLAIN, 18);

//setup the JTextFields
for (int i = 0; i < 11; i++) {
JTF[i] = new JTextField(7);
c.add(JTF[i]);

JTF[i].setFont(currentFont);
JTF[i].setHorizontalAlignment(JTextField.CENTER);
JTF[i].addFocusListener(tList);
}

//add the DocumentListener to the first 10 JTextFields
for (int i = 0; i < 10; i++) {
JTF[i].getDocument().addDocumentListener(dList);
}

//setup the JLabels
for (int i = 0; i < 11; i++) {
JL[i] = new JLabel();
c.add(JL[i]);
JL[i].setHorizontalAlignment(JLabel.CENTER);
}

//fill the JTextFields
JTF[0].setText("Male");
JTF[1].setText("50");

JTF[2].setText("150");
JTF[3].setText("90");
JTF[4].setText("No");
JTF[5].setText("6");
JTF[6].setText("1.3");
JTF[7].setText("No");
JTF[8].setText("No");
JTF[9].setText("10");
JTF[10].setText("10.4");

//fill the JLabels
JL[0].setText("Sex");
JL[1].setText("Age");
JL[2].setText("Sys BP");
JL[3].setText("Dias BP");
JL[4].setText("Smoking");
JL[5].setText("Tot Chol");
JL[6].setText("HDL Chol");
JL[7].setText("Diabetes");
JL[8].setText("LVH");
JL[9].setText("Years");
JL[10].setText("CHD Risk");

//format the last JTextField and JLabel
JTF[10].setBorder(BorderFactory.createEtchedBorder(Color.blue,
Color.lightGray));
JTF[10].setEnabled(false);
JTF[10].setDisabledTextColor(Color.black);
JL[10].setForeground(Color.blue);

// set window size and display
app.setSize(720, 100);
app.show();
}

//class to deal with changes in JTF's
static class dList implements DocumentListener {


public void insertUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void removeUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void changedUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);
}
}

//class to deal with focus changes
static class tList implements FocusListener {
//action when JTextField loses focus
public void focusLost(FocusEvent efL) {
JTextField FL = (JTextField) efL.getSource();

try {

////////////////////////////////////////////////////////////
//start of calculations

JTF[10].setBackground(Color.white);
JTF[10].setDisabledTextColor(Color.black);
FL.setBackground(Color.white);

//Sex
if (JTF[0].getText().equalsIgnoreCase("Female") ||
JTF[0].getText().equalsIgnoreCase("F")) {
Holder[0] = 1;
JTF[0].setText("Female");
} else {
Holder[0] = 0;


JTF[0].setText("Male");
}

//Smoking
if (JTF[4].getText().equalsIgnoreCase("Yes") ||
JTF[4].getText().equalsIgnoreCase("Y")) {
Holder[1] = 1;
JTF[4].setText("Yes");
} else {
Holder[1] = 0;
JTF[4].setText("No");
}

//Diabetes
if (JTF[7].getText().equalsIgnoreCase("Yes") ||
JTF[7].getText().equalsIgnoreCase("Y")) {
Holder[2] = 1;
JTF[7].setText("Yes");
} else {
Holder[2] = 0;
JTF[7].setText("No");
}

//LVH
if (JTF[8].getText().equalsIgnoreCase("Yes") ||
JTF[8].getText().equalsIgnoreCase("Y")) {
Holder[3] = 1;
JTF[8].setText("Yes");
} else {
Holder[3] = 0;
JTF[8].setText("No");
}

//systolic a
Holder[4] = 11.1122 - 0.9119 *
Math.log(Double.parseDouble(JTF[2].getText())) -
0.2767 * Holder[1] - 0.7181 *
Math.log(Double.parseDouble(JTF[5].getText()) /
Double.parseDouble(JTF[6].getText())) - 0.5865 * Holder[3];

//diastolic a
Holder[10] = 11.0938 - 0.867 *
Math.log(Double.parseDouble(JTF[3].getText())) -
0.2789 * Holder[1] - 0.7142 *
Math.log(Double.parseDouble(JTF[5].getText()) /
Double.parseDouble(JTF[6].getText())) - 0.7195 * Holder[3];

//systolic m
if (Holder[0] < 1) {
Holder[5] = (Holder[4] - 1.4792 *
Math.log(Double.parseDouble(JTF[1].getText()))) -
0.1759 * Holder[2];
} else {
Holder[5] = (Holder[4] - 5.8549) + 1.8515 *
Math.log(Double.parseDouble(JTF[1].getText()) / 74) *
Math.log(Double.parseDouble(JTF[1].getText()) / 74) -
0.3758 * Holder[2];
}

//diastolic m
if (Holder[0] < 1) {
Holder[11] = (Holder[10] - 1.6343 *
Math.log(Double.parseDouble(JTF[1].getText()))) -
0.2082 * Holder[2];
} else {
Holder[11] = (Holder[10] - 6.5306) + 2.1059 *
Math.log(Double.parseDouble(JTF[1].getText()) / 74) *
Math.log(Double.parseDouble(JTF[1].getText()) / 74) -
0.4055 * Holder[2];
}

//systolic mu
Holder[6] = 4.4181 + Holder[5];

//diastolic mu
Holder[12] = 4.4284 + Holder[11];

//systolic sigma
Holder[7] = Math.exp(-0.3155 - 0.2784 * Holder[5]);

//diastolic sigma
Holder[13] = Math.exp(-0.3171 - 0.2825 * Holder[11]);

//systolic u
Holder[8] =
(Math.log(Double.parseDouble(JTF[9].getText())) - Holder[6]) / Holder[7];

//diastolic u
Holder[14] =
(Math.log(Double.parseDouble(JTF[9].getText())) - Holder[12]) / Holder[13];

//systolic p
Holder[9] = 1 - Math.exp(-Math.exp(Holder[8]));

//diastolic p
Holder[15] = 1 - Math.exp(-Math.exp(Holder[14]));

//final risk score, formatted to 1 decimal
if (Holder[15] > Holder[9]) {
JTF[10].setText(df1.format(Holder[15] * 100));
} else {
JTF[10].setText(df1.format(Holder[9] * 100));
}

//JTF[10] font red if > 29.9
if ((Holder[9] * 100) > 29.9 || (Holder[15] * 100) > 29.9) {
JTF[10].setDisabledTextColor(Color.red);
} else {
JTF[10].setDisabledTextColor(Color.black);
}

//end of calculations
////////////////////////////////////////////////////////////

} catch (Exception e) {
JTF[10].setText("Error");
JTF[10].setBackground(Color.red);
JTF[10].setDisabledTextColor(Color.white);
FL.requestFocus();
FL.selectAll();
FL.setBackground(Color.pink);
}
}

//action when JTextField gains focus
public void focusGained(FocusEvent efG) {
JTextField FG = (JTextField) efG.getSource();
FG.selectAll();
}
}
}


RBS

RB Smissaert

unread,
Dec 29, 2001, 1:46:10 PM12/29/01
to
Just one more question. When minimizing the application and then clicking
the icon to maximize, there is no highlighted text anymore.
Tried to solve this with a WindowListener, but it didn't work. Would there
be any easy solution for this?
Thanks for any advice.

RBS


//Java core packages


import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.DecimalFormat;

//Java extension packages
import javax.swing.*;
import javax.swing.event.*;

public class CHD_Calc extends JFrame {

//define the JTextField array, holding 11 JTextFields
static JTextField JTF[] = new JTextField[11];

//define the JLabel array, holding 11 JLabels


static JLabel JL[] = new JLabel[11];

//declare custom output format
static DecimalFormat df1 = new DecimalFormat("##.0");

//declare the array holding all the data
static double[] Holder = new double[16];

public static void main(String args[]) {


CHD_Calc app = new CHD_Calc();
Container c = app.getContentPane();

//setup the frame


app.setResizable(false);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setTitle(" CHD Risk Calculator Tab to move to next
field and calculate");

c.setLayout(new GridLayout(2, 11, 8, 4));

// setup the listeners for the JTextFields


FocusListener tList = app.new tList();
DocumentListener dList = app.new dList();

//set the font of the JTextFields
Font currentFont = new Font("TimesRoman", Font.PLAIN, 18);

//setup the JTextFields


for (int i = 0; i < 11; i++) {
JTF[i] = new JTextField(7);
c.add(JTF[i]);

JTF[i].setFont(currentFont);
JTF[i].setHorizontalAlignment(JTextField.CENTER);
}

//add the DocumentListener and FocusListeners to the first 10
JTextFields


for (int i = 0; i < 10; i++) {
JTF[i].getDocument().addDocumentListener(dList);

JTF[i].addFocusListener(tList);
}

//setup the JLabels


for (int i = 0; i < 11; i++) {

JL[i] = new JLabel();
c.add(JL[i]);
JL[i].setHorizontalAlignment(JLabel.CENTER);
}

//fill the JTextFields


JTF[0].setText("Male");
JTF[1].setText("50");

// set window size and display
app.setSize(720, 100);
app.show();
}

//class to deal with changes in JTF's
static class dList implements DocumentListener {


public void insertUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void removeUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void changedUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);
}
}

//class to deal with focus changes
static class tList implements FocusListener {
//action when JTextField loses focus
public void focusLost(FocusEvent efL) {
JTextField FL = (JTextField) efL.getSource();

try {

////////////////////////////////////////////////////////////
//start of calculations

JTF[10].setBackground(Color.white);
JTF[10].setDisabledTextColor(Color.black);
FL.setBackground(Color.white);

//Sex
if (JTF[0].getText().equalsIgnoreCase("Female") ||
JTF[0].getText().equalsIgnoreCase("F")) {
Holder[0] = 1;
JTF[0].setText("Female");
} else {
Holder[0] = 0;

JTF[0].setText("Male");
}

//Smoking

Christian Kaufhold

unread,
Dec 29, 2001, 9:28:26 AM12/29/01
to
Hello!

RB Smissaert <bartsm...@blueyonder.co.uk> wrote:

> Using JDK1.3.1_02. Is it possible to add the same DocumentListener to more
> than one JTextField? I have 10 JTextFields that I want to monitor for any
> change. Somehow I don't think I would need 10 classes to deal with the same
> thing.

1. Classes != instances. You may have to create 10 instances of the same class,


if your listeners share the same functionality, but have to have some knowledge
about the context (DocumentEvent will only carry the Document, but that is in-
dependent from any JTextComponent(s) that may display it, so it usually isn't
useful).

You don't really describe what your document listener is supposed to do. If
it always should clear the tenth text field, it doesn't need any context, you
can even use the same DocumentListener instance for all text fields.


DocumentListener clearTenthField = new MyDocumentListener();

for (int i = 0; i < 10; i++)
textField[i].getDocument().addDocumentListener(clearTenthField);


Actually, you appear to be doing this for the FocusListener in your code.
Where is the problem in doing it the same way with a DocumentListener?

Christian

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-nwblwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!newsfeed1.earthlink.net!newsfeed.earthlink.net!uunet!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: use...@chka.de (Christian Kaufhold)
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <2t3c2dd1c...@aves.chka.de>
Control: cancel <2t3c2dd1c...@aves.chka.de>
Date: Wed, 2 Jan 2002 05:17:17 GMT
Organization: Navix Internet Subscribers
Lines: 2
Message-ID: <cancel.2t3c2...@aves.chka.de>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949453 28325 166.102.15.34 (2 Jan 2002 05:30:53 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:30:53 GMT
X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

autocancel

Nils O. Selåsdal

unread,
Dec 29, 2001, 8:56:22 AM12/29/01
to
In article <CfjX7.946$q35.6...@news-text.cableinet.net>, RB Smissaert wrote:
> Using JDK1.3.1_02. Is it possible to add the same DocumentListener to more
> than one JTextField? I have 10 JTextFields that I want to monitor for any
> change. Somehow I don't think I would need 10 classes to deal with the same
> thing.
No you dont ;)
Ok, do that. But do it like:
MyDocumentListener l = new MyDocumentListener();
JTF[0].getDocument().addDocumentListener(l);
JTF[1].getDocument().addDocumentListener(l);
...

> // set window size and display


> app.setSize(720, 100);
> app.show();
> }
>
> static class MyDocumentListener implements DocumentListener {
> public void insertUpdate(DocumentEvent e) {

((JTextField)e.getSource()).setText("");
> }
> public void removeUpdate(DocumentEvent e) {
((JTextField)e.getSource()).setText("");
> }
> public void changedUpdate(DocumentEvent e) {
((JTextField)e.getSource()).setText("");
> }
> }
>

========= WAS CANCELLED BY =======:

Path: news.uni-stuttgart.de!blackbush.xlink.net!blackbush.de.kpnqwest.net!howland.erols.net!news-out.worldnet.att.net.MISMATCH!wn3feed!worldnet.att.net!198.6.0.123!uunet!sac.uu.net!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: =?iso-8859-1?Q?Nils_O=2E_Sel=E5sdal?= <nose...@frisurf.no>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <aYjX7.12506$KQ3.1...@news1.oke.nextra.no>
Control: cancel <aYjX7.12506$KQ3.1...@news1.oke.nextra.no>
Date: Wed, 2 Jan 2002 03:53:47 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.aYjX7.12506$KQ3.1...@news1.oke.nextra.no>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949491 28325 166.102.15.34 (2 Jan 2002 05:31:31 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:31:31 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

Xref: news.uni-stuttgart.de control:40290059

autocancel

RB Smissaert

unread,
Dec 29, 2001, 10:37:59 AM12/29/01
to
Thanks for the reply. Figured it out now.
The problem was that I added a DocumentListener to the last JTextField, just
because it was easy to do in a loop that was already there. This last
JTextField is the one that alters (setText("")) if any of the first 10
JTextFields alters. So I guess that by both listening and changing in the
same JTextField, there was a sort of never ending loop.

This is the error message on running (it compiled fine):

java.lang.IllegalStateException: Attempt to mutate in notification
at
javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1090)
at
javax.swing.text.AbstractDocument.remove(AbstractDocument.java:456)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1159)
at CHD_Calc$dList.insertUpdate(CHD_Calc.java:103)
at
javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:180
)
at
javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:542)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1160)
at CHD_Calc.main(CHD_Calc.java:74)

This is the whole code now:
Please let me know if anything is wrong or could be improved.


//Java core packages


import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.DecimalFormat;

//Java extension packages
import javax.swing.*;
import javax.swing.event.*;

public class CHD_Calc extends JFrame {

//define the JTextField array, holding 11 JTextFields
static JTextField JTF[] = new JTextField[11];

//define the JLabel array, holding 11 JLabels


static JLabel JL[] = new JLabel[11];

//declare custom output format
static DecimalFormat df1 = new DecimalFormat("##.0");

//declare the array holding all the data
static double[] Holder = new double[16];

public static void main(String args[]) {


CHD_Calc app = new CHD_Calc();
Container c = app.getContentPane();

//setup the frame


app.setResizable(false);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setTitle(" CHD Risk Calculator Tab to move to next
field and calculate");

c.setLayout(new GridLayout(2, 11, 8, 4));

// setup the listeners for the JTextFields


FocusListener tList = app.new tList();
DocumentListener dList = app.new dList();

//set the font of the JTextFields
Font currentFont = new Font("TimesRoman", Font.PLAIN, 18);

//setup the JTextFields


for (int i = 0; i < 11; i++) {
JTF[i] = new JTextField(7);
c.add(JTF[i]);

JTF[i].setFont(currentFont);
JTF[i].setHorizontalAlignment(JTextField.CENTER);
JTF[i].addFocusListener(tList);
}

//add the DocumentListener to the first 10 JTextFields
for (int i = 0; i < 10; i++) {
JTF[i].getDocument().addDocumentListener(dList);
}

//setup the JLabels


for (int i = 0; i < 11; i++) {
JL[i] = new JLabel();
c.add(JL[i]);
JL[i].setHorizontalAlignment(JLabel.CENTER);
}

//fill the JTextFields


JTF[0].setText("Male");
JTF[1].setText("50");

// set window size and display
app.setSize(720, 100);
app.show();
}

//class to deal with changes in JTF's
static class dList implements DocumentListener {


public void insertUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void removeUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void changedUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);
}
}

//class to deal with focus changes
static class tList implements FocusListener {
//action when JTextField loses focus
public void focusLost(FocusEvent efL) {
JTextField FL = (JTextField) efL.getSource();

try {

////////////////////////////////////////////////////////////
//start of calculations

JTF[10].setBackground(Color.white);
JTF[10].setDisabledTextColor(Color.black);
FL.setBackground(Color.white);

//Sex
if (JTF[0].getText().equalsIgnoreCase("Female") ||
JTF[0].getText().equalsIgnoreCase("F")) {
Holder[0] = 1;
JTF[0].setText("Female");
} else {
Holder[0] = 0;

JTF[0].setText("Male");
}

//Smoking

//end of calculations
////////////////////////////////////////////////////////////


RBS

========= WAS CANCELLED BY =======:

Path: news.uni-stuttgart.de!dns.phoenix-ag.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.media.kyoto-u.ac.jp!newsfeed.mesh.ad.jp!uunet!osa.uu.net!sac.uu.net!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: "RB Smissaert" <bartsm...@blueyonder.co.uk>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <rrlX7.1150$3M5.8...@news-text.cableinet.net>
Control: cancel <rrlX7.1150$3M5.8...@news-text.cableinet.net>
Date: Wed, 2 Jan 2002 02:25:11 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.rrlX7.1150$3M5.8...@news-text.cableinet.net>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949438 28325 166.102.15.34 (2 Jan 2002 05:30:38 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:30:38 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

Xref: news.uni-stuttgart.de control:40289864

autocancel

Tor Iver Wilhelmsen

unread,
Dec 29, 2001, 8:56:01 AM12/29/01
to
"RB Smissaert" <bartsm...@blueyonder.co.uk> writes:

> Using JDK1.3.1_02. Is it possible to add the same DocumentListener to more
> than one JTextField?

Yes.

--
Tor Iver Wilhelmsen <to...@chello.no>
If you get too cold, I'll tax the heat
If you take a walk, I'll tax your feet.
- The Beatles: Taxman

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-nwblwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!newsfeed1.earthlink.net!newsfeed.earthlink.net!uunet!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: Tor Iver Wilhelmsen <to...@chello.no>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <wku1uan...@mail.multinett.no>
Control: cancel <wku1uan...@mail.multinett.no>
Date: Wed, 2 Jan 2002 03:13:46 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.wku...@mail.multinett.no>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949487 28325 166.102.15.34 (2 Jan 2002 05:31:27 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:31:27 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

autocancel

RB Smissaert

unread,
Dec 29, 2001, 1:46:10 PM12/29/01
to
Just one more question. When minimizing the application and then clicking
the icon to maximize, there is no highlighted text anymore.
Tried to solve this with a WindowListener, but it didn't work. Would there
be any easy solution for this?
Thanks for any advice.

RBS


//Java core packages


import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.DecimalFormat;

//Java extension packages
import javax.swing.*;
import javax.swing.event.*;

public class CHD_Calc extends JFrame {

//define the JTextField array, holding 11 JTextFields
static JTextField JTF[] = new JTextField[11];

//define the JLabel array, holding 11 JLabels


static JLabel JL[] = new JLabel[11];

//declare custom output format
static DecimalFormat df1 = new DecimalFormat("##.0");

//declare the array holding all the data
static double[] Holder = new double[16];

public static void main(String args[]) {


CHD_Calc app = new CHD_Calc();
Container c = app.getContentPane();

//setup the frame


app.setResizable(false);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setTitle(" CHD Risk Calculator Tab to move to next
field and calculate");

c.setLayout(new GridLayout(2, 11, 8, 4));

// setup the listeners for the JTextFields


FocusListener tList = app.new tList();
DocumentListener dList = app.new dList();

//set the font of the JTextFields
Font currentFont = new Font("TimesRoman", Font.PLAIN, 18);

//setup the JTextFields


for (int i = 0; i < 11; i++) {
JTF[i] = new JTextField(7);
c.add(JTF[i]);

JTF[i].setFont(currentFont);
JTF[i].setHorizontalAlignment(JTextField.CENTER);
}

//add the DocumentListener and FocusListeners to the first 10

JTextFields


for (int i = 0; i < 10; i++) {
JTF[i].getDocument().addDocumentListener(dList);

JTF[i].addFocusListener(tList);
}

//setup the JLabels


for (int i = 0; i < 11; i++) {

JL[i] = new JLabel();
c.add(JL[i]);
JL[i].setHorizontalAlignment(JLabel.CENTER);
}

//fill the JTextFields


JTF[0].setText("Male");
JTF[1].setText("50");

// set window size and display
app.setSize(720, 100);
app.show();
}

//class to deal with changes in JTF's
static class dList implements DocumentListener {


public void insertUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void removeUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);


}
public void changedUpdate(DocumentEvent e) {
JTF[10].setText("");

JTF[10].setBackground(Color.white);
}
}

//class to deal with focus changes
static class tList implements FocusListener {
//action when JTextField loses focus
public void focusLost(FocusEvent efL) {
JTextField FL = (JTextField) efL.getSource();

try {

////////////////////////////////////////////////////////////
//start of calculations

JTF[10].setBackground(Color.white);
JTF[10].setDisabledTextColor(Color.black);
FL.setBackground(Color.white);

//Sex
if (JTF[0].getText().equalsIgnoreCase("Female") ||
JTF[0].getText().equalsIgnoreCase("F")) {
Holder[0] = 1;
JTF[0].setText("Female");
} else {
Holder[0] = 0;

JTF[0].setText("Male");
}

//Smoking

//end of calculations
////////////////////////////////////////////////////////////

========= WAS CANCELLED BY =======:
Path: news.uni-stuttgart.de!dns.phoenix-ag.de!newsfeed01.sul.t-online.de!newsfeed00.sul.t-online.de!t-online.de!colt.net!newsfeed.esat.net!news-out.visi.com!hermes.visi.com!newsfeed1.earthlink.net!newsfeed.earthlink.net!uunet!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: "RB Smissaert" <bartsm...@blueyonder.co.uk>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <SboX7.1361$Vg6.9...@news-text.cableinet.net>
Control: cancel <SboX7.1361$Vg6.9...@news-text.cableinet.net>
Date: Wed, 2 Jan 2002 05:24:52 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.SboX7.1361$Vg6.9...@news-text.cableinet.net>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949310 28325 166.102.15.34 (2 Jan 2002 05:28:30 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:28:30 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

Xref: news.uni-stuttgart.de control:40289600

autocancel

0 new messages