Article View: borland.public.jbuilder.java.swing
Article #9157Re: Jtextfield problem
From: Sven Goetgeluck
Date: Fri, 19 May 2000 00:00
Date: Fri, 19 May 2000 00:00
315 lines
10482 bytes
10482 bytes
Thanx pal this is what i want but have still small problem , need an event
triggering on the panel
so when you click one off the buttons or change the textfield ,it has to put
the text to another textfield,
must be in the class because need more than one plusminustectfield in my
panel
is that possible??????
now i use an ordinary textfield and i check on keyreleased with :
void ObjectVrexForceStepsTextField_keyReleased(KeyEvent e)
{
panelnode.Op_ForceStep =ObjectVrexForceStepsTextField.getText();
recalcRG();
}
recalc Muda is a panel with piechart on it
Do you also have solutions on printpreview?????
greetings from Belgium
----- Original Message -----
From: Paul Furbacher [TeamB] <pnf@home.com>
Newsgroups: borland.public.jbuilder.java.swing
Sent: Wednesday, May 17, 2000 11:18 PM
Subject: Re: Jtextfield problem
> Sven Goetgeluck wrote:
> >
> > want to create a jtextfield with 2 buttons beside it , one + and one -
> > so when clikking on one of the buttons the textfield in - decrease with
1
> > how can a do this?
> >
> > in c i used a scrollpane and programmes the knobs but dont know how to
do
> > this in java
>
> You would create a compound component which consists of a panel into
> which you would nest two buttons and a single textfield. Set the
> images of the buttons. Restrict the size of the textfield by setting
> its preferred and minimum sizes. Use a layout manager which
> allows the textfield to resize, if necessary.
>
> Add mouse event handling that responds to the mouse clicks in the
> buttons.
>
> An even more elegant touch would be to write a custom Document
> for the JTextField which allows only 0 - 9 characters as input.
>
> Here's a quick draft of it:
>
> *****
>
> import java.awt.*;
> import javax.swing.*;
> import com.borland.jbcl.layout.*;
> import java.awt.event.*;
> import javax.swing.border.*;
>
>
>
> /** A simple kind of numeric spinner control. It could
> * be enhanced by replacing the text on the buttons with
> * anti-aliased images, and by restricting the legal characters
> * to 0 through 9 by extending PlainDocument [override the
"insertString()"
> * method].
> *
> * Also, listening to mouse down would allow for the possibility
> * of holding the mouse down to repeatedly change the value. As
> * it is now, the user must click once for each change.
> *
> Copyright: Copyright (c) 2000
> @author Paul Furbacher
> */
>
> public class PlusMinusTextField extends JPanel {
> JButton minusButton = new JButton();
> JButton plusButton = new JButton();
> JTextField tf = new JTextField();
> GridBagLayout gridBagLayout1 = new GridBagLayout();
> Border border1;
> Border border2;
>
>
> 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();
> minusButton.setText("-");
> minusButton.setMargin(new Insets(0, 4, 0, 4));
> minusButton.addActionListener(new java.awt.event.ActionListener() {
> public void actionPerformed(ActionEvent e) {
> minusButton_actionPerformed(e);
> }
> });
> 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");
> plusButton.setMargin(new Insets(0, 4, 0, 4));
> plusButton.addActionListener(new java.awt.event.ActionListener() {
> public void actionPerformed(ActionEvent e) {
> plusButton_actionPerformed(e);
> }
> });
> plusButton.setText("+");
> this.setLayout(gridBagLayout1);
> this.setBorder(border1);
> this.add(minusButton,
> new GridBagConstraints2(2, 0, 1, 1, 0.0, 0.0,
> GridBagConstraints.NORTHWEST,
> GridBagConstraints.NONE,
> new Insets(0, 0, 0, 0), 0, 0));
> this.add(plusButton,
> new GridBagConstraints2(1, 0, 1, 1, 0.0, 0.0,
> GridBagConstraints.NORTHWEST,
> GridBagConstraints.NONE,
> new Insets(0, 0, 0, 0), 0, 0));
> this.add(tf, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
> GridBagConstraints.NORTHWEST,
> GridBagConstraints.BOTH,
> new Insets(0, 0, 0, 0), 0, 0));
> }
>
> void plusButton_actionPerformed(ActionEvent e) {
> try {
> int i = Integer.valueOf(tf.getText()).intValue();
> tf.setText(String.valueOf(++i));
> }
> catch ( Exception ex ) {
>
> }
> }
>
> void minusButton_actionPerformed(ActionEvent e) {
> try {
> int i = Integer.valueOf(tf.getText()).intValue();
> tf.setText(String.valueOf(--i));
> }
> catch ( Exception ex ) {
>
> }
> }
>
> }
>
> *****
>
>
> Paul Furbacher (TeamB)
Paul Furbacher [TeamB] <pnf@home.com> schreef in berichtnieuws
39230C94.3B09A3A0@home.com...
> Sven Goetgeluck wrote:
> >
> > want to create a jtextfield with 2 buttons beside it , one + and one -
> > so when clikking on one of the buttons the textfield in - decrease with
1
> > how can a do this?
> >
> > in c i used a scrollpane and programmes the knobs but dont know how to
do
> > this in java
>
> You would create a compound component which consists of a panel into
> which you would nest two buttons and a single textfield. Set the
> images of the buttons. Restrict the size of the textfield by setting
> its preferred and minimum sizes. Use a layout manager which
> allows the textfield to resize, if necessary.
>
> Add mouse event handling that responds to the mouse clicks in the
> buttons.
>
> An even more elegant touch would be to write a custom Document
> for the JTextField which allows only 0 - 9 characters as input.
>
> Here's a quick draft of it:
>
> *****
>
> import java.awt.*;
> import javax.swing.*;
> import com.borland.jbcl.layout.*;
> import java.awt.event.*;
> import javax.swing.border.*;
>
>
>
> /** A simple kind of numeric spinner control. It could
> * be enhanced by replacing the text on the buttons with
> * anti-aliased images, and by restricting the legal characters
> * to 0 through 9 by extending PlainDocument [override the
"insertString()"
> * method].
> *
> * Also, listening to mouse down would allow for the possibility
> * of holding the mouse down to repeatedly change the value. As
> * it is now, the user must click once for each change.
> *
> Copyright: Copyright (c) 2000
> @author Paul Furbacher
> */
>
> public class PlusMinusTextField extends JPanel {
> JButton minusButton = new JButton();
> JButton plusButton = new JButton();
> JTextField tf = new JTextField();
> GridBagLayout gridBagLayout1 = new GridBagLayout();
> Border border1;
> Border border2;
>
>
> 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();
> minusButton.setText("-");
> minusButton.setMargin(new Insets(0, 4, 0, 4));
> minusButton.addActionListener(new java.awt.event.ActionListener() {
> public void actionPerformed(ActionEvent e) {
> minusButton_actionPerformed(e);
> }
> });
> 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");
> plusButton.setMargin(new Insets(0, 4, 0, 4));
> plusButton.addActionListener(new java.awt.event.ActionListener() {
> public void actionPerformed(ActionEvent e) {
> plusButton_actionPerformed(e);
> }
> });
> plusButton.setText("+");
> this.setLayout(gridBagLayout1);
> this.setBorder(border1);
> this.add(minusButton,
> new GridBagConstraints2(2, 0, 1, 1, 0.0, 0.0,
> GridBagConstraints.NORTHWEST,
> GridBagConstraints.NONE,
> new Insets(0, 0, 0, 0), 0, 0));
> this.add(plusButton,
> new GridBagConstraints2(1, 0, 1, 1, 0.0, 0.0,
> GridBagConstraints.NORTHWEST,
> GridBagConstraints.NONE,
> new Insets(0, 0, 0, 0), 0, 0));
> this.add(tf, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
> GridBagConstraints.NORTHWEST,
> GridBagConstraints.BOTH,
> new Insets(0, 0, 0, 0), 0, 0));
> }
>
> void plusButton_actionPerformed(ActionEvent e) {
> try {
> int i = Integer.valueOf(tf.getText()).intValue();
> tf.setText(String.valueOf(++i));
> }
> catch ( Exception ex ) {
>
> }
> }
>
> void minusButton_actionPerformed(ActionEvent e) {
> try {
> int i = Integer.valueOf(tf.getText()).intValue();
> tf.setText(String.valueOf(--i));
> }
> catch ( Exception ex ) {
>
> }
> }
>
> }
>
> *****
>
>
> Paul Furbacher (TeamB)
Technical Details
Message-ID:
<39242302@dnews>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3921af12@dnews> <39230C94.3B09A3A0@home.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.goetgeluck@pandora.be> Subject: Re: Jtextfield problem Date: 2000/05/19 Message-ID: <39242302@dnews> X-Deja-AN: 625061094 References: <3921af12@dnews> <39230C94.3B09A3A0@home.com> X-Trace: 18 May 2000 10:06:10 -0800, 195.130.151.107 X-MSMail-Priority: Normal X-Priority: 3 Newsgroups: borland.public.jbuilder.java.swing X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200