column size... run-time

In the following code, After typing

12345

in the text field, I enter 3 in column size.

Question: How can I get force 12345 to change to 123 because the column size has been changed in column size?

Thank you
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;

public class GuiTest extends JFrame {
    
    JPanel leftPanel;
    JPanel rightPanel;
    JPanel topPanel;
    JTextField field;
    JTextField field2;
    JRadioButton center;
    JRadioButton left;
    JRadioButton right;

public GuiTest() {
    
    topPanel = new JPanel();
    leftPanel = new JPanel();
    rightPanel = new JPanel();
    field = new JTextField(30);
    field2 = new JTextField(8);
    center = new JRadioButton("Center");
    right = new JRadioButton("Right");
    left = new JRadioButton("Left");

    left.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            field.setHorizontalAlignment(JTextField.LEFT);
            right.setSelected(false);
            center.setSelected(false);}});

    center.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            field.setHorizontalAlignment(JTextField.CENTER);
            right.setSelected(false);
            left.setSelected(false);}});

    right.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            field.setHorizontalAlignment(JTextField.RIGHT);
            center.setSelected(false);
            left.setSelected(false);}});

    field2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            field.setColumns(Integer.parseInt(field2.getText()));
            topPanel.revalidate();}});

    setLayout(new BorderLayout(5,5));
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
        topPanel.add(new JLabel("Text field"));
        topPanel.add(field);

    leftPanel.setLayout(new BorderLayout(1,1));
    leftPanel.add(new JLabel("Horizontal Alignment"),BorderLayout.NORTH);
    leftPanel.add(center,BorderLayout.CENTER);
    leftPanel.add(left,BorderLayout.WEST);
    leftPanel.add(right,BorderLayout.EAST);

    rightPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));
    rightPanel.add(new JLabel("Column Size"));
    rightPanel.add(field2);

    add(topPanel,BorderLayout.NORTH);
    add(leftPanel,BorderLayout.WEST);
    add(rightPanel,BorderLayout.EAST);
}

public static void main(String[] args){
    GuiTest frame = new GuiTest ();
    frame.setTitle("GuiTest");
    frame.setSize(430,120);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
ActionListener when atteched to textfield fires when you hit cariage return
0
 
for_yanCommented:
You should add action listener to JTextField - when
someone type enter - actionPerfored will fire and
in actionPerformd you should replace the string where ever you waht to replace it according to the coluumn wodth specified in the textfied
0
 
for_yanCommented:
Try this:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;

public class GuiTest extends JFrame {

    JPanel leftPanel;
    JPanel rightPanel;
    JPanel topPanel;
    JTextField field;
    JTextField field2;
    JRadioButton center;
    JRadioButton left;
    JRadioButton right;

public GuiTest() {

    topPanel = new JPanel();
    leftPanel = new JPanel();
    rightPanel = new JPanel();
    field = new JTextField(30);
    field2 = new JTextField(8);
    center = new JRadioButton("Center");
    right = new JRadioButton("Right");
    left = new JRadioButton("Left");

    left.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            field.setHorizontalAlignment(JTextField.LEFT);
            right.setSelected(false);
            center.setSelected(false);}});

    center.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            field.setHorizontalAlignment(JTextField.CENTER);
            right.setSelected(false);
            left.setSelected(false);}});

    right.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            field.setHorizontalAlignment(JTextField.RIGHT);
            center.setSelected(false);
            left.setSelected(false);}});

    field2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        //    field.setColumns(Integer.parseInt(field2.getText()));
            field.setText(field.getText().substring(0,Integer.parseInt(field2.getText())));
            topPanel.revalidate();}});

    setLayout(new BorderLayout(5,5));
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
        topPanel.add(new JLabel("Text field"));
        topPanel.add(field);

    leftPanel.setLayout(new BorderLayout(1,1));
    leftPanel.add(new JLabel("Horizontal Alignment"),BorderLayout.NORTH);
    leftPanel.add(center,BorderLayout.CENTER);
    leftPanel.add(left,BorderLayout.WEST);
    leftPanel.add(right,BorderLayout.EAST);

    rightPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));
    rightPanel.add(new JLabel("Column Size"));
    rightPanel.add(field2);

    add(topPanel,BorderLayout.NORTH);
    add(leftPanel,BorderLayout.WEST);
    add(rightPanel,BorderLayout.EAST);
}

public static void main(String[] args){
    GuiTest frame = new GuiTest ();
    frame.setTitle("GuiTest");
    frame.setSize(430,120);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
It is not changing

12345

to

123

when I enter 3 in column size and hit tab key. Do I need to use repaint() for this?
0
 
for_yanCommented:
No you need to hit the carriage return key
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.