Hi,
I want to set the column size of a text field from another text field by the input from the user. However, it just doesn't work. The following is my code. Just check out the last anonymous inner class action listener. Somehow i can get the user text, but it just doesn't work.
Thanks for any helpful inputs.
/**
* Introduction to Java Programming: Comprehensive, 6th Ed.
* Excercise 15.11 - Demonstrating JTextField properties, dynamically.
* @Kaka Kaka
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Ex15_11 extends JFrame{
// Create two text fields and three radio buttons
private JTextField jtfUserText = new JTextField(10);
private JTextField jtfColumnSize = new JTextField(new Integer(10));
private JRadioButton jrbLeft = new JRadioButton("Left");
private JRadioButton jrbCenter = new JRadioButton("Center");
private JRadioButton jrbRight = new JRadioButton("Right");
public static void main(String[] args){
Ex15_11 frame = new Ex15_11();
frame.pack();
frame.setTitle("Excercise 15.11 - Text Field Property");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// Start of Constructor
public Ex15_11(){
// Set the the frame layout
setLayout(new BorderLayout(5, 5));
// Create three panels and two labels
JPanel jpText = new JPanel();
JPanel jpHorizontalAlignment = new JPanel();
JPanel jpColumn = new JPanel();
JLabel jlblTextField = new JLabel("Text Field");
JLabel jlblColumn = new JLabel("Column Size");
// Create a button group for the radio buttons to be grouped
ButtonGroup group = new ButtonGroup();
// Group the radio buttons
group.add(jrbLeft);
group.add(jrbCenter);
group.add(jrbRight);
// set a titled border for a panel
jpHorizontalAlignment.setBorder(new TitledBorder("Horizontal Alignment"));
// Create a line border
Border lineBorder = new LineBorder(Color.BLACK, 1);
// the all the components to their corresponding panels
jpText.add(jlblTextField);
jpText.add(jtfUserText);
jpHorizontalAlignment.add(jrbLeft);
jpHorizontalAlignment.add(jrbCenter);
jpHorizontalAlignment.add(jrbRight);
jpColumn.setBorder(lineBorder);
jpColumn.add(jlblColumn);
jpColumn.add(jtfColumnSize);
// add the panels to the frame
add(jpText, BorderLayout.NORTH);
add(jpHorizontalAlignment, BorderLayout.WEST);
add(jpColumn, BorderLayout.EAST);
jrbLeft.addActionListener(new ActionListener(){
// Handle event
public void actionPerformed(ActionEvent e) {
jtfUserText.setHorizontalAlignment(SwingConstants.LEFT);
}
});
jrbCenter.addActionListener(new ActionListener(){
// Handle event
public void actionPerformed(ActionEvent e) {
jtfUserText.setHorizontalAlignment(SwingConstants.CENTER);
}
});
jrbRight.addActionListener(new ActionListener(){
// Handle event
public void actionPerformed(ActionEvent e) {
jtfUserText.setHorizontalAlignment(SwingConstants.RIGHT);
}
});
// Register the listener for the coloum size
jtfColumnSize.addActionListener(new ActionListener(){
// Handle event
public void actionPerformed(ActionEvent e) {
System.out.println(Integer.parseInt(jtfColumnSize.getText()));
jtfUserText.setColumns(Integer.parseInt(jtfColumnSize.getText()));
}
});
}
}
Edited by: ChangBroot on Dec 16, 2008 6:13 PM