Article View: borland.public.jbuilder.java.swing
Article #9145Re: Jtextfield problem
From: Paul Furbacher [TeamB]
Date: Wed, 17 May 2000 00:00
Date: Wed, 17 May 2000 00:00
139 lines
4788 bytes
4788 bytes
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:
<39230C94.3B09A3A0@home.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3921af12@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,e3d8a7969574fa02 X-Google-Attributes: gid111d45,public From: "Paul Furbacher [TeamB]" <pnf@home.com> Subject: Re: Jtextfield problem Date: 2000/05/17 Message-ID: <39230C94.3B09A3A0@home.com> X-Deja-AN: 624706327 Content-Transfer-Encoding: 7bit References: <3921af12@dnews> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pnf@home.com Newsgroups: borland.public.jbuilder.java.swing