/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoundedRangeModel;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
public class TextSlider extends JPanel {
private JTextField textField;
private JScrollBar scrollBar;
public TextSlider() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
textField = new JTextField();
scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
BoundedRangeModel brm = textField.getHorizontalVisibility();
scrollBar.setModel(brm);
add(textField);
add(scrollBar);
}
public JTextField getTextField() {
return textField;
}
public String getText() {
return textField.getText();
}
public void addActionListener(ActionListener l) {
textField.addActionListener(l);
}
public void removeActionListener(ActionListener l) {
textField.removeActionListener(l);
}
public JScrollBar getScrollBar() {
return scrollBar;
}
public static void main(String args[]) {
JFrame f = new JFrame("Text Slider");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final TextSlider ts = new TextSlider();
ts.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Text: " + ts.getText());
}
});
Container c = f.getContentPane();
c.add(ts, BorderLayout.NORTH);
f.setSize(300, 200);
f.setVisible(true);
}
}
|