0

I'm working with Groovy / Java with Swing, and trying to build JOptionPane showInputDialog frame, which pops to user, and takes user input in text field.

Yes it's simple. But my problem is that showInputDialog text area is by default quite small, and for usability I want it to be bigger.

Does anyone know how to resize textarea?

Example: This is what I get small

But this is what I want big

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Note: You seem to use the terms textfield & textarea interchangeably, but they are entirely separate components. The first is for a single line of text, the 2nd is for multi-line text. Please use terms more carefully, and [edit] this question to remove all references to the one that is not relevant to the question. – Andrew Thompson Jul 07 '17 at 13:13

3 Answers3

0

You might have success with Sergiy Medvynskyy's answer on this question, or at least use it in some way;

JLabel label = new JLabel("MESSAGE");
label.setFont(new Font("Arial", Font.BOLD, 18));
JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);

Since JOptionPane accepts a JLabel as parameter, you could experiment a bit by trying to resize that one, and then just passing it on.

Edit:

Assuming you are using Swing, this code should work.
(You might have to experiment a bit to reach your desired size/layout)

        TextField tf = new TextField();
        tf.setFont(new Font("Arial", Font.BOLD, 20));

        JPanel jp = new JPanel(new BorderLayout(0, 0));

        JLabel jl = new JLabel("Example Prompt Message!");
        jl.setFont(new Font("Arial", Font.BOLD, 16));

        jp.add(jl, BorderLayout.NORTH);
        jp.add(tf, BorderLayout.SOUTH);

        JOptionPane.showConfirmDialog(null, jp);

Result should be something similiar to this. (You can change the dialogue type too of course, though showInputDialog probably won't work, due to it already having a default input line.)

Sample Output with re-sized text field

Cath
  • 460
  • 6
  • 21
0

Building on AscendedKittens answer, this shows how you actually get the inputted data back.

    TextField tf = new TextField();
    JPanel panel = new JPanel(new BorderLayout(0, 0));
    tf.setFont(new Font("Arial", Font.BOLD, 24));
    JLabel label = new JLabel("Example Prompt Message!");
    label.setFont(new Font("Arial", Font.BOLD, 16));
    panel.add(label, BorderLayout.NORTH);
    panel.add(tf, BorderLayout.SOUTH);
    int result = JOptionPane.showConfirmDialog(null, panel);
    if(result==JOptionPane.OK_OPTION)
    {
        System.out.println(tf.getText());
    }
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
0

The text inside JOptionPane.showInputDialog is considered a TextField . You can solve this problem by using the UIManager.put method .

UIManager.put("TextField.font", 
         new FontUIResource(new Font("SansSerif",Font.BOLD,21)));

Changing the font of the letters will make the TextField considerably longer too , so if you want to avoid this you can do this :

UIManager.put("TextField.margin", 
             new InsetsUIResource(0,0,0,-100));