Swing

Setting swing text component size through PreferredSize during construction of JTextfield, JFormattedTextfield, JCombobox and other JTextComponent based classes that use the TextUI class

Method 1: (Works and accounts for everything correctly) //Note: The field will be the exact size of the string you use. Pad by a couple of characters so it looks better. String str = "The longest representative sized string"; myJComponent.setText(str); myJComponent.setPreferredSize(myJComponent.getUI().getPreferredSize(myJComponent)); myJComponent.setText(""); There are a lot of class jumps that go into calculating the size of a field based on it's contents. All of it goes through the preferredSize methods. If the preferredSize has not been set, a bunch of class calls leads through PlainView to javax.swing.text.Utilities.getTabbedTextWidth() where the math is finally performed. You could call this directly since it is a static method. It requires the text in the form of a Segment, the FontMetrics, and that is about it. javax.swing.text.Utilities.getTabbedTextWidth(new javax.swing.text.Segment(myString.toCharArray(),0,myString.length()), aJComponent.getFontMetrics(aJComponent.getFont()), 0, null, 0) works to get the width as well although having the TabExpander null causes the field to be a little too small. I was able to create the TabExpander but it seems to be returning zero sized tabs. Method 2: (Failed Experiment in imitating how Sun does it because FieldView doesn't have correct info) Here is what I have so far. The TabExpander part isn't returning the right answer so it is the same as having null in the TabExpander field (Note: ViewField implements TabExpander). If the tab size gets figured out, this way would be faster, but the other way is easier :). You could skip the FieldView part and just pad in a 4 or 5 extra characters to make up for the border region plus a little white on either side of character string.. jtf = new JFormattedTextField() ViewFactory f = jtf.getUI().getRoutView(jtf).getViewFactory(); Element elem = jft.getDocument().getDefaultRoutElement(); //This may be where I am failing FieldView fv = new FieldView(elem); Segment seg = new javax.swing.text.Segment("bla bla bla".toCharArray(),0,"bla bla bla".getLength()) FontMetrics fm = jtf.getFontMetrics(jtf.getFont()); int width = Utilities.getTabbedTextWidth(seg, fm, 0, fv, 0); int height = fm.getHeight(); jtf.setPreferredSize(new Dimension(width,height)); Method 3: (Works but fudging in the space on the sides because otherwise an exact string size gets a character or 2 cut off) jft = new JFormattedTextField(); String str = "PPPLargestTextStringPPP"; //PPP is padding to make it look nice (A little white space around string) and to make room for the Tab space. More padding needed here than in Method 1. FontMetrics fm = jtf.getFontMetrics(jtf.getFont()); int width = Utilities.getTabbedTextWidth(new Segment(str.toCharArray(),0,str.getLength()),fm,0,null,0); int height = fm.getHeight(); jtf.setPreferredSize(new Dimension(width,height));