Article View: borland.public.jbuilder.java.swing
Article #9102Re: Jtextfield problem
From: sven goetgeluck
Date: Wed, 24 May 2000 00:00
Date: Wed, 24 May 2000 00:00
581 lines
18226 bytes
18226 bytes
--------------C040021EAD675B67CC266F0E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi Paul this is my code , but i cannot figure out how to get an event
from the pluMinus panel to the frame so i can fill up the other
textfield with the value of the plusminus field.
When using firePropertyChange like you given me in earlier reply is
doen't work at all
can you figure this out??????
would be very helpfull , allready have putten "real" spinner look to the
panel but cannot use it withoutr the right events
greeting sven
listing 1
package spinner;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.text.Document;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.*;
import javax.swing.event.*;
public class PlusMinusTextField extends JPanel implements ChangeListener,
MouseListener, FocusListener,ActionListener{
JButton minusButton = new JButton();
JButton plusButton = new JButton();
JTextField tf = new JTextField();
GridBagLayout gridBagLayout1 = new GridBagLayout();
Border border1;
Border border2;
private String text;
protected BasicArrowButton north, south;
JTextField jTextField1 = new JTextField();
GridLayout gridLayout1 = new GridLayout();
public PlusMinusTextField() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
border1 =
BorderFactory.createBevelBorder(
BevelBorder.LOWERED,
Color.white,Color.white,
new Color(149, 142, 130),
new Color(104, 99, 91)
);
// Hide the border of the textfield itself.
border2 = BorderFactory.createEmptyBorder();
tf.setPreferredSize(new Dimension(40, 19));
tf.setBackground(new Color(242, 242, 242));
tf.setBorder(border2);
tf.setMinimumSize(new Dimension(40, 19));
tf.setHorizontalAlignment(JTextField.CENTER);
tf.setText("0");
setLayout(new BorderLayout());
add(BorderLayout.CENTER, jTextField1);
JSpinner spinner = new JSpinner(jTextField1);
addKeyListener(spinner);
addMouseListener(this);
addFocusListener(this);
add(BorderLayout.EAST, spinner);
}
Document getDocument()
{
return tf.getDocument();
}
String getString()
{
return jTextField1.getText();
}
void north_actionPerformed(ActionEvent e) {
}
public void stateChanged(ChangeEvent event)
{
repaint();
}
public void mouseClicked(MouseEvent event)
{
requestFocus();
}
public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void focusGained(FocusEvent event)
{
repaint();
}
public void focusLost(FocusEvent event)
{
repaint();
}
public boolean isFocusTraversable()
{
return true;
}
public void actionPerformed(ActionEvent event)
{
repaint();
}
}
Listing 2
package spinner;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.JTextField;
public class JSpinner extends JPanel
implements ActionListener, KeyListener, SwingConstants
{
protected Vector listeners = new Vector();
protected BasicArrowButton north, south;
SpinModel model;
JTextField field;
public JSpinner(JTextField tf)
{
this.field = tf;
setLayout(new GridLayout(2, 1));
setPreferredSize(new Dimension(16, 16));
north = new BasicArrowButton(BasicArrowButton.NORTH);
north.addActionListener(this);
add(north);
south = new BasicArrowButton(BasicArrowButton.SOUTH);
south.addActionListener(this);
add(south);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == north)
{
increment();
}
if (event.getSource() == south)
{
decrement();
}
}
public void keyTyped(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyPressed(KeyEvent event)
{
int code = event.getKeyCode();
if (code == KeyEvent.VK_UP)
{
increment();
}
if (code == KeyEvent.VK_DOWN)
{
decrement();
}
if (code == KeyEvent.VK_LEFT)
{
model.setPrevField();
}
if (code == KeyEvent.VK_RIGHT)
{
model.setNextField();
}
}
protected void increment()
{
field.setText("plus");
}
public void decrement()
{
field.setText("min");
}
}
listing 3
package spinner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import javax.swing.text.Document;
public class Frame1 extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField2 = new JTextField();
PlusMinusTextField pm = new PlusMinusTextField();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
pm.getDocument().addDocumentListener(new MyDocumentListener()
{
public void insertUpdate(DocumentEvent e) {
pm_insertUpdated(e);
}
});
this.getContentPane().add(jTextField2, new XYConstraints(27, 105,
294, -1));
this.getContentPane().add(pm, new XYConstraints(18, 153, 350, 33));
}
//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void pm_insertUpdated(DocumentEvent ev)
{
jTextField2.setText(pm.getString());
}
}
class MyDocumentListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
update(e);
}
public void removeUpdate(DocumentEvent e) {
}
public void changedUpdate(DocumentEvent e) {
update(e);
//Plain text components don't fire these events
}
public void update(DocumentEvent e)
{
System.out.println("joepie");
}
public void updateLog(DocumentEvent e, String action) {
}
}
"John P. McGrath" wrote:
> On Tue, 23 May 2000 14:41:41 +0200, sven goetgeluck wrote:
>
> > this downt work always crashes when at firePropertyChange
> > what al i doing wrong???
>
> I can't tell without seeing the exception that it threw or the source
> code that you used.
>
> --
> John P. McGrath
> <JPMcGrath@fast.net>
> <jpm@nscs.fast.net>
--------------C040021EAD675B67CC266F0E
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hi Paul this is my code , but i cannot figure out how to get an event from
the pluMinus panel to the frame so i can fill up the other textfield
with the value of the plusminus field.
<br>When using firePropertyChange like you given me in earlier reply is
doen't work at all
<p>can you figure this out??????
<br>would be very helpfull , allready have putten "real" spinner look to
the panel but cannot use it withoutr the right events
<p>greeting sven<u></u>
<p><u>listing 1</u>
<br>package spinner;
<p>import java.awt.*;
<br>import javax.swing.*;
<br>import com.borland.jbcl.layout.*;
<br>import java.awt.event.*;
<br>import javax.swing.border.*;
<br>import javax.swing.text.Document;
<br>import javax.swing.event.DocumentEvent;
<br>import javax.swing.event.DocumentListener;
<br>import javax.swing.plaf.basic.BasicArrowButton;
<br>import javax.swing.*;
<br>import javax.swing.event.*;
<br>
<p>public class PlusMinusTextField extends JPanel implements ChangeListener,
MouseListener, FocusListener,ActionListener{
<br> JButton minusButton = new JButton();
<br> JButton plusButton = new JButton();
<br> JTextField tf = new JTextField();
<br> GridBagLayout gridBagLayout1 = new GridBagLayout();
<br> Border border1;
<br> Border border2;
<br> private String text;
<br> protected BasicArrowButton north, south;
<br> JTextField jTextField1 = new JTextField();
<br> GridLayout gridLayout1 = new GridLayout();
<br>
<br>
<p> public PlusMinusTextField() {
<br> try {
<br> jbInit();
<br> }
<br> catch (Exception ex) {
<br> ex.printStackTrace();
<br> }
<br> }
<p> private void jbInit() throws Exception {
<br> border1 =
<br> BorderFactory.createBevelBorder(
<br> BevelBorder.LOWERED,
<br> Color.white,Color.white,
<br> new Color(149, 142, 130),
<br> new Color(104, 99, 91)
<br> );
<br> // Hide the border of the textfield itself.
<br> border2 = BorderFactory.createEmptyBorder();
<br> tf.setPreferredSize(new Dimension(40, 19));
<br> tf.setBackground(new Color(242, 242, 242));
<br> tf.setBorder(border2);
<br> tf.setMinimumSize(new Dimension(40, 19));
<br> tf.setHorizontalAlignment(JTextField.CENTER);
<br> tf.setText("0");
<br> setLayout(new BorderLayout());
<br> add(BorderLayout.CENTER, jTextField1);
<br> JSpinner spinner = new JSpinner(jTextField1);
<br> addKeyListener(spinner);
<br> addMouseListener(this);
<br> addFocusListener(this);
<br> add(BorderLayout.EAST, spinner);
<p> }
<p> Document getDocument()
<br> {
<br> return tf.getDocument();
<p> }
<br> String getString()
<br> {
<br> return jTextField1.getText();
<br> }
<p> void north_actionPerformed(ActionEvent e) {
<p> }
<br> public void stateChanged(ChangeEvent event)
<br> {
<br> repaint();
<br> }
<p> public void mouseClicked(MouseEvent event)
<br> {
<br> requestFocus();
<br> }
<p> public void mousePressed(MouseEvent event) {}
<br> public void mouseReleased(MouseEvent event) {}
<br> public void mouseEntered(MouseEvent event) {}
<br> public void mouseExited(MouseEvent event) {}
<p> public void focusGained(FocusEvent event)
<br> {
<br> repaint();
<br> }
<br>
<br> public void focusLost(FocusEvent event)
<br> {
<br> repaint();
<br> }
<br>
<br> public boolean isFocusTraversable()
<br> {
<br> return true;
<br> }
<br> public void actionPerformed(ActionEvent event)
<br> {
<br> repaint();
<br> }
<p>}<u></u>
<p><u>Listing 2</u>
<br>package spinner;
<br>import java.awt.*;
<br>import java.awt.event.*;
<br>import java.util.*;
<br>import javax.swing.*;
<br>import javax.swing.plaf.basic.BasicArrowButton;
<br>import javax.swing.JTextField;
<p>public class JSpinner extends JPanel
<br> implements ActionListener, KeyListener, SwingConstants
<br>{
<br> protected Vector listeners = new Vector();
<br> protected BasicArrowButton north, south;
<br> SpinModel model;
<br> JTextField field;
<p> public JSpinner(JTextField tf)
<br> {
<br> this.field = tf;
<br> setLayout(new GridLayout(2, 1));
<br> setPreferredSize(new Dimension(16, 16));
<p> north = new BasicArrowButton(BasicArrowButton.NORTH);
<br> north.addActionListener(this);
<br> add(north);
<p> south = new BasicArrowButton(BasicArrowButton.SOUTH);
<br> south.addActionListener(this);
<br> add(south);
<br> }
<p> public void actionPerformed(ActionEvent event)
<br> {
<br> if (event.getSource() == north)
<br> {
<br> increment();
<br> }
<br> if (event.getSource() == south)
<br> {
<br> decrement();
<br> }
<br> }
<p> public void keyTyped(KeyEvent event) {}
<br> public void keyReleased(KeyEvent event) {}
<br> public void keyPressed(KeyEvent event)
<br> {
<br> int code = event.getKeyCode();
<br> if (code == KeyEvent.VK_UP)
<br> {
<br> increment();
<br> }
<br> if (code == KeyEvent.VK_DOWN)
<br> {
<br> decrement();
<br> }
<br> if (code == KeyEvent.VK_LEFT)
<br> {
<br> model.setPrevField();
<br> }
<br> if (code == KeyEvent.VK_RIGHT)
<br> {
<br> model.setNextField();
<br> }
<br> }
<p> protected void increment()
<br> {
<br> field.setText("plus");
<br>
<br> }
<p> public void decrement()
<br> {
<br>
field.setText("min");
<br> }
<br>}
<p><u>listing 3</u>
<br>package spinner;
<p>import java.awt.*;
<br>import java.awt.event.*;
<br>import javax.swing.*;
<br>import com.borland.jbcl.layout.*;
<br>import java.beans.*;
<br>import javax.swing.event.DocumentListener;
<br>import javax.swing.event.DocumentEvent;
<br>import javax.swing.text.Document;
<br>public class Frame1 extends JFrame {
<br> XYLayout xYLayout1 = new XYLayout();
<br> JTextField jTextField2 = new JTextField();
<br> PlusMinusTextField pm = new PlusMinusTextField();
<br> //Construct the frame
<br> public Frame1() {
<br> enableEvents(AWTEvent.WINDOW_EVENT_MASK);
<br> try {
<br> jbInit();
<br> }
<br> catch(Exception e) {
<br> e.printStackTrace();
<br> }
<br> }
<p> //Component initialization
<br> private void jbInit() throws Exception {
<br> this.getContentPane().setLayout(xYLayout1);
<br> this.setSize(new Dimension(400, 300));
<br> this.setTitle("Frame Title");
<p> pm.getDocument().addDocumentListener(new MyDocumentListener()
<br> {
<br> public void insertUpdate(DocumentEvent e) {
<br> pm_insertUpdated(e);
<br> }
<br> });
<p> this.getContentPane().add(jTextField2, new XYConstraints(27,
105, 294, -1));
<br> this.getContentPane().add(pm, new XYConstraints(18,
153, 350, 33));
<br> }
<p> //Overridden so we can exit on System Close
<br> protected void processWindowEvent(WindowEvent e) {
<br> super.processWindowEvent(e);
<br> if(e.getID() == WindowEvent.WINDOW_CLOSING) {
<br> System.exit(0);
<br> }
<br> }
<p> void pm_insertUpdated(DocumentEvent ev)
<br> {
<br> jTextField2.setText(pm.getString());
<br> }
<br>}
<p>class MyDocumentListener implements DocumentListener {
<p> public void insertUpdate(DocumentEvent e) {
<br> update(e);
<br> }
<br> public void removeUpdate(DocumentEvent e) {
<br> }
<br> public void changedUpdate(DocumentEvent e) {
<br> update(e);
<br> //Plain text components
don't fire these events
<br> }
<br> public void update(DocumentEvent e)
<br> {
<br> System.out.println("joepie");
<br> }
<br> public void updateLog(DocumentEvent e, String action)
{
<br> }
<br>}
<br>
<br>
<br>
<br><u></u>
<p>"John P. McGrath" wrote:
<blockquote TYPE=CITE>On Tue, 23 May 2000 14:41:41 +0200, sven goetgeluck
wrote:
<p>> this downt work always crashes when at firePropertyChange
<br>> what al i doing wrong???
<p>I can't tell without seeing the exception that it threw or the source
<br>code that you used.
<p>--
<br>John P. McGrath
<br><JPMcGrath@fast.net>
<br><jpm@nscs.fast.net></blockquote>
</html>
--------------C040021EAD675B67CC266F0E--
Technical Details
Message-ID:
<392BEFE0.6249F1E8@officeline.be>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3921af12@dnews> <39230C94.3B09A3A0@home.com> <39242302@dnews> <MPG.138e6cabc7cff616989936@forums.borland.com> <39250aa2@dnews> <MPG.139148a8c31034b098993c@forums.borland.com> <392A7C85.7D72C823@officeline.be> <MPG.139505deb9177169989947@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,e3d8a7969574fa02 X-Google-Attributes: gid111d45,public From: sven goetgeluck <sven@officeline.be> Subject: Re: Jtextfield problem Date: 2000/05/24 Message-ID: <392BEFE0.6249F1E8@officeline.be> X-Deja-AN: 626755563 References: <3921af12@dnews> <39230C94.3B09A3A0@home.com> <39242302@dnews> <MPG.138e6cabc7cff616989936@forums.borland.com> <39250aa2@dnews> <MPG.139148a8c31034b098993c@forums.borland.com> <392A7C85.7D72C823@officeline.be> <MPG.139505deb9177169989947@forums.borland.com> X-Accept-Language: en Content-Type: multipart/alternative; boundary="------------C040021EAD675B67CC266F0E" X-Trace: 24 May 2000 07:56:56 -0800, 127.0.0.1 MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing