Preventing Events From Firing

J

Jason Cavett

Is it possible to update a Swing component (such as calling
JTextField.setText(String)) without an event being fired off?
Currently, any call I make to setText fires the DocumentListener on
the JTextField's document.

Basically, I want a way to temporarily disable actions on a
JComponent. Or, if that's not possible, I would like a way to know
what fired the event (AKA, was it me calling "setText" or was it a
user typing on the keyboard) so I can handle it in a different manner
depending on the situation.
 
J

John B. Matthews

Jason Cavett said:
Is it possible to update a Swing component (such as calling
JTextField.setText(String)) without an event being fired off?
Currently, any call I make to setText fires the DocumentListener on
the JTextField's document.

Basically, I want a way to temporarily disable actions on a
JComponent. Or, if that's not possible, I would like a way to know
what fired the event (AKA, was it me calling "setText" or was it a
user typing on the keyboard) so I can handle it in a different manner
depending on the situation.

I think the best way is to make your DocumentListener implementation do
the right thing either way, as discussed in the context of your earlier
question:

<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thre
ad/bbab1865d121ae47>

I recall being tempted to temporarily remove a problem listener; but it
seemed wrong, so I never actually tried it:

<http://java.sun.com/javase/6/docs/api/javax/swing/text/Document.html#rem
oveDocumentListener(javax.swing.event.DocumentListener)>

As before, an short example <http://pscode.org/sscce.html> might help
you clarify your approach or garner a better reply.
 
K

Knute Johnson

Jason said:
Is it possible to update a Swing component (such as calling
JTextField.setText(String)) without an event being fired off?
Currently, any call I make to setText fires the DocumentListener on
the JTextField's document.

Basically, I want a way to temporarily disable actions on a
JComponent. Or, if that's not possible, I would like a way to know
what fired the event (AKA, was it me calling "setText" or was it a
user typing on the keyboard) so I can handle it in a different manner
depending on the situation.

There was just a discussion here the other day very similar to this.
That fellow didn't want to have the mouse select text in a JTextField
when it was clicked. The solution I posted is below. You could do
something very similar to this or maybe extend setText() to disable the
DocumentListener before calling super.setText().

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FunnyJTextField extends JTextField {
private boolean mouseFlag;

public FunnyJTextField(String text) {
super(text);

addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
mouseFlag = true;
}
});

addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent fe) {
if (mouseFlag)
mouseFlag = false;
else
selectAll();
}
});
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextField tf = new JTextField("normal field");
f.add(tf,BorderLayout.CENTER);

FunnyJTextField ftf = new FunnyJTextField("funny text
field");
f.add(ftf,BorderLayout.SOUTH);

f.pack();
f.setVisible(true);
}
});
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top