Results 1 to 9 of 9
  1. #1
    gawalker is offline Member
    Join Date
    May 2016
    Posts
    5
    Rep Power
    0

    Default How to resize a jTextField using the Swing GUI designer

    For some reason, when I try to use the following code,

    Java Code:
    idiomText.setPreferredSize(new Dimension((int)width, 24));
    I can't change the width of the text box, but I can change the height. Can anyone tell me what I'm doing wrong??

    If you need more details please let me know

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to resize a jTextField using the Swing GUI designer

    A better way is to use setColumns(). The width and height will be adjusted based on the current
    font. And by nature, a text field is a single row. If you want more than one row, use a JTextArea.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gawalker is offline Member
    Join Date
    May 2016
    Posts
    5
    Rep Power
    0

    Default Re: How to resize a jTextField using the Swing GUI designer

    Thank you for your response. Unfortunately this doesn't seem to fix my problem either. I've been trying to google my way out of this for hours now, and I've tried every solution, but nothing works. What I'm trying to do by the way is to resize the textfield randomly upon the click of a button

  4. #4
    gawalker is offline Member
    Join Date
    May 2016
    Posts
    5
    Rep Power
    0

    Default Re: How to resize a jTextField using the Swing GUI designer

    This seems like something that would have a really simple solution

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to resize a jTextField using the Swing GUI designer

    Can you post a simple, compilable example of how you are trying to do this?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to resize a jTextField using the Swing GUI designer

    Ok, normally I don't do this but since this may not be obvious, here is an example.

    Java Code:
    import java.awt.Dimension;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class ResizeDemo extends JPanel {
       JFrame      frame    = new JFrame("Resize Demo");
       JTextField  f        = new JTextField(10);
       JButton      b1    = new JButton("Increase");
       JButton      b2    = new JButton("Decrease");
    
       public ResizeDemo() {
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setPreferredSize(new Dimension(500, 100));
          frame.add(this);
          b1.addActionListener(event -> adjustCols(5));
          b2.addActionListener(event -> adjustCols(-5));
    
          JPanel buttonPanel = new JPanel();
          buttonPanel.setPreferredSize(new Dimension(500, 50));
          buttonPanel.add(b1);
          buttonPanel.add(b2);
          add(f);
          add(buttonPanel);
    
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    
       }
    
       public static void main(String[] args) {
          new ResizeDemo();
       }
    
       private void adjustCols(int val) {
          int cols = f.getColumns();
          // check boundary conditions
          if ((val > 0 && cols >= 30) || (val < 0 && cols <= 10)) {
             return;
          }
          f.setColumns(cols + val);
          frame.revalidate();
       }
    }
    Most of the code is boilerplate. The key is to revalidate the frame so it makes the size adjustment.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gawalker is offline Member
    Join Date
    May 2016
    Posts
    5
    Rep Power
    0

    Default Re: How to resize a jTextField using the Swing GUI designer

    Like so,

    Java Code:
      startButton.setEnabled(false);
      idiomText.setEnabled(true);
      idiomText.setText("");
      int width=(int) (30.0+Math.random()*120);
      
      
    
    
     idiomText.setPreferredSize(new Dimension(width,9));
     
     
     
     System.out.println(idiomText.getHeight());
    when i print that out, I get 16 for height. I have no idea where it's coming from

  8. #8
    gawalker is offline Member
    Join Date
    May 2016
    Posts
    5
    Rep Power
    0

    Default Re: How to resize a jTextField using the Swing GUI designer

    after fixing a couple things, my output is fine. I can pass ANYTHING into the y value of new Dimension. Whatever x value I put in is ignored

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to resize a jTextField using the Swing GUI designer

    Reread my first comment.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 7
    Last Post: 12-04-2014, 09:20 PM
  2. How to make Swing JTextField look like AWT TextField?
    By LeonLanford in forum AWT / Swing
    Replies: 1
    Last Post: 09-04-2010, 05:06 PM
  3. Replies: 4
    Last Post: 04-08-2009, 06:51 AM
  4. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  5. GUI Designer for Swing/Java
    By balajishenbagarajan in forum AWT / Swing
    Replies: 20
    Last Post: 01-27-2009, 01:17 AM

Tags for this Thread

Posting Permissions

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