How to increase the number of labels letters shown by sacrificing the width of the text field in library formsFX? In my case, a maximum of 7 letters are shown.
Related
I'm looking for a way to get the number of characters that can appear in a row in a JTextArea.
I tried using getColumns() but this returned 0:
jTextArea.getColumns();
Does anyone know how this can be achieved? I need it so that I can display data in fixed width columns, so I need the number of characters that can fit in a row in order to calculate this.
If you don't specify number of rows and columns when creating JTextArea, it default the rows and columns to zero. This mode allows the JTextArea to expand/contract according to available space for itself and actual number of letters shown in a line etc can be adjusted.
In the default mode, the number of letters that would fit in a line will depend on the current dimension of the component, the line wrapping style (word boundary or character boundary) and the font used.
If you cannot set the row, columns in the JTextArea then you can probably use javax.swing.JTextArea.getColumnWidth() to get the size of one character and use the current width of the component to get approximate number of letters that will fit in a line.
When using "Grab Excess Horizontal Space" on multiple SWT controls within the same space, the default behavior does not divide the space between them exactly equally. Some sort of behind-the-scenes calculation seems to be done to divide it "sort of" equally, but giving a higher ratio to larger controls.
In my example here, I have created a custom table-like control using grid layouts in which the user can add any number of rows, as well as any number of boxes (custom canvases) for each row individually. My intent is to have all boxes within a given row be of equal size - and by that virtue, all rows with an equal number of boxes will have equally-sized boxes, despite being separate. In my example, however, you can see that the one box that has label text within it grabs more space than those on the same row, due to the calculation believing that it "needs more" than the others.
What would be the best way to tackle this issue?
You can try to use makeColumnsEqualWidth from GridLayout.
I have a JTextField which can accept a fixed number of characters (eg. 10 characters). I want to restrict the width of the TextField to exactly take that many characters. So given the no.of characters, is there a way to find out the width (pixels) it will take? Assume that we know the font.
This will get you the exact width, though you'd want some extra pixels of padding to make it pretty.
myJTextField.getFontMetrics(myFont).stringWidth(myString);
This approach only makes sense for unproportional fonts. Otherwise you'd had to find the 'biggest' char in the charset to make the box wide enough for 10 chars of this type.
A practical approach would be to 'guess' a wide character and compute the width so you a good propability that the text field will be big enough for usual input. Adapting lins314159 example code:
myJTextField.getFontMetrics(myFont).stringWidth("wwwwwwwwww);
JTextField textField = new JTextField(10);
The UI will size the text field automatically. I believe it does in fact use a "W" as the sizing character, so it most cases it will be larger than you need. If you want the width to be exact then use a monospaced font.
I have a screen with a RitchTextField added, which has a custom font applied. I want to find the total number of lines that the RitchTextField can hold to avoid vertical scrolling.
I tried to do this by getting the height of the RichTextField then dividing it by the the height of the font, the problem however is using rtfField.getHeight() always returns the current height of field, and using the screens getHeight() returns the screen size not taking other fields into consideration.
For example:
Screen Size using getHeight() = 360.
Font Size using: this.rtfField.getFont().getHeight() = 25
Total Lines ~ 12
However doing a manual count the screen only comfortably displays 8 lines.
Not sure if this is what you're looking for, but I think you want the current height of the field divided by the font height:
int lines = this.rtfField.getHeight() / this.rtfField.getFont().getHeight();
First problem: You have 400 pixels width to go on, and need to fit some text within that constraint as large as possible (thus, the text shall use that amount of space).
Throw in a new constraint: If the text is just "A", then it shall not zoom this above 100 pixels height (or some specific font size).
Then, a final situation: Linebreaks. Fit some text in the largest possible way within e.g. 400 x 150 pixels.
An obvious way is to simply start with point 1, and then increase until you can't fit it anymore. This would work for all three problems, but would be very crude. The fitting of a single line within bounds could be done by writing it with some fixed point size, check the resulting pixel bounds of the text, and then simply scale it with a transform (the text scales properly too then, check out TransformUI).
Any ideas of other ways to attack this would be greatly appreciated!
As what you are modelling is complex, especially with line breaks, then your initial proposal of trying all sizes is along the right lines, especially if it needs to be accurate.
However, rather than testing each value, you can use a binary search to find the appropriate font size. You know the size is somewhere between 1 and 100 (your upper range). using a binary search, each test sets the font size and checks the resulting layout. If the text is too large, then we search the lower half of the current range of possible values. If the font size fits, then we search the upper half. Your search will use at most 7 attempts (100 log base 2 rounded up), it will be exact, finding the largest size without going over, and it will be flexible if you need to add more requirements later, such as a mix of fonts or more stringent constraints on the layout.
I'm assuming you are using a text component that does line wrapping, and that you can set the maximum width to 400. So, you set the font size and it does the layout giving you back the required height, laying out text within the given width.
You can use hints to try to guide the algorithm to the result quicker, such as making your first guess close to the expected size, but text rendering is fast, that the performance increase may not be worth the implementation effort.
See Wikipedia - Binary Search Algorithm
I would do the following:
Assume you want W pixels wide text.
Pick an arbitrary size, say 10pt, and see what bounding box the text-string gets for that size. Lets say it gets N pixels wide.
Set the new size to 10pt * W/N, and repeat from step one, until you get within a reasonable threshold. (Hopefully it would work within one iteration.)
This relies on the fact that the width of the string, is roughly proportional to the size of the font.
I'd instantiate the Font at the largest desired size: say 72 for one inch glyphs at 72 dpi. Use TextLayout to get the bounds and scale using AffineTransform (direct) or AffineTransformOp (offscreen), while preserving the aspect ratio. Suitable RenderingHints help, too.