1

I'm trying to add JTextField to JToolBar and it works, but it is too long. I need it to take only 3 letters.

Here is the screenshot of it now... screenshot

I tried following methods,

JTextField field = new JextField(3); // thought this limits to three characters.

And I tried,

field.setColumns(3); // this didn't work either.
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Isuru Madusanka
  • 1,397
  • 4
  • 19
  • 27
  • Interesting. :) Over on Nimbus L&F, you get the opposite behaviour - the text field is always the preferred size and it's difficult to make it grow wider. – Hakanai Feb 28 '14 at 04:34

2 Answers2

3

The default layout of a tool-bar respects the maximum size set for a text field.

Text Field in Tool Bar

import java.awt.*;
import javax.swing.*;

public class TextFieldInToolBar {

    TextFieldInToolBar() {
        JPanel p = new JPanel(new BorderLayout());

        JToolBar tb = new JToolBar();
        p.add(tb, BorderLayout.PAGE_START);

        Icon disk = (Icon)UIManager.get("FileView.floppyDriveIcon"); 
        Icon pc = (Icon)UIManager.get("FileView.computerIcon"); 
        tb.add(new JButton(disk));
        JTextField tf = new JTextField(3);
        tf.setMaximumSize(tf.getPreferredSize());
        tb.add(tf);
        tb.addSeparator();
        tb.add(new JButton(pc));

        p.setPreferredSize(new Dimension(250,50));

        JOptionPane.showMessageDialog(null, p);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TextFieldInToolBar();
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"no upvotes for today"* Me too! *"but why bothering with JToolBar"* Why not? I'm bored & wanted to see if it could be as simple as setting a maximum size. ;) – Andrew Thompson Jun 05 '12 at 19:46
  • no offense but I saw there issue with height direction, why not use FlowLayout / BoxLayout directly – mKorbel Jun 05 '12 at 19:58
  • Wow, this is great. It kinda cleaner than previous. – Isuru Madusanka Jun 05 '12 at 19:59
  • I am for the strike, (persuade John and nice animal too) two weeks without upvotes in the Swing tagged posts, this will be another jokes hehehe – mKorbel Jun 05 '12 at 20:01
  • 1
    If you want the maximum size always the same as the preferred size, you should extend JTextField and override getMaximumSize(). If you don't, Swing will think you're setting the maximum size explicitly and even if the preferred size changes later, won't update the maximum size to match. Another way to do it is to handle it in your updateUI() method from some other component. – Hakanai Feb 28 '14 at 04:38
  • @Trejkaz Yes, that is a good point to override rather than set. I might update the source. See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Feb 28 '14 at 07:28
1
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319