Results 1 to 2 of 2
  1. #1
    Trdmrk is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default Using input from JTextField to adjust column size of another

    I'm trying to use one JTextField to adjust the column size of another JTextfield based on the users input. I implemented an action listener and the getText method to retreive the input. Converted the string to an integer and then called the setColumn method. The program compiles but does not do what it was intended to. Any advice or assistance would be greatly appreciated.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class TxtTest extends JFrame{
      private JTextField txtField = new JTextField(10);
      private JTextField clmnSize = new JTextField("Type Anything");
      
      //Main Method
      public static void main (String[] args){
        JFrame frame = new TxtTest();
        frame.setTitle("TxtTest");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,110);
        frame.setVisible(true);
      }
      
      public TxtTest() {
        JPanel panel = new JPanel();
        JPanel panel2= new JPanel();
        
        setLayout(new GridLayout(2,1)); 
        add(panel = new JPanel());
        add(panel2 = new JPanel());
        
        panel.add(clmnSize);
        panel2.add(txtField);
        
        txtField.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent evt) {
            String input = txtField.getText();
            int number =Integer.parseInt(input);
            clmnSize.setColumns(number);
          }  
        });
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What if you call revalidate on the Container that is holding the JTextField:
    Java Code:
          txtField.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                String input = txtField.getText();
                int number = Integer.parseInt(input);
                clmnSize.setColumns(number);
                JPanel cPane = (JPanel) TxtTest.this.getContentPane();
                cPane.revalidate();
             }
          });
    This tells the container to re-layout its components.

Similar Threads

  1. Applet JTextField Size Problem
    By a13w in forum New To Java
    Replies: 4
    Last Post: 09-19-2009, 07:52 AM
  2. Replies: 14
    Last Post: 09-18-2009, 04:48 AM
  3. Convert Input from JTextField to int?
    By jls7168 in forum New To Java
    Replies: 7
    Last Post: 02-20-2009, 02:29 AM
  4. Constraining Input in JTextField
    By kataya in forum AWT / Swing
    Replies: 1
    Last Post: 06-26-2008, 06:25 PM
  5. How To:Use a JSlider to adjust Text size in a JPanel
    By louiebagz in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 07:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •