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.
Related
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.
I want to change the column width of a column I have seen many answers suggesting the use of "AutoSizeColumn(int)" but the problem with that is that all columns have different sizes and it creates kind of a mess so i want to set all columns ( except one ) to one single width how do i do this??
You can set a default column width that controls all columns in the sheet that don't have their own custom width set. Use the setDefaultColumnWidth method.
Set the default column width for the sheet (if the columns do not define their own width) in characters
sheet.setDefaultColumnWidth(numChars);
Individual columns can override this default with the setColumnWidth method. The width here is in 1/256ths of a character, different from the default width units.
Set the width (in units of 1/256th of a character width)
I want to create a jtable where each cell should be the size of a single character, without any separation between adjacent characters (i.e adjacent columns). There are no borders between columns so the visualization of a sequence of columns is indistinguible of a jLabel. I have tried many things like making the width of a column as small as possible until each cell is displayed as ... meaning there is no space to print the character. So say I want to have 5 consecutive cells to print the name Alice. What I have achieved so far is a table with this aspect
A l i c e
If I make the column width smaller it shows
... ... ... ... ...
And what I want is:
Alice
Thanks!!
Alvaro
What is the point of doing something like this? This will only work for a single row.
What if you have two rows:
Alice
SWITCH
The minimum width of the column will be controlled by the "W" in the second row, which means you will always have extra space around the "l" in Alice.
Why don't you just use a JPanel with a BoxLayout and then add multiple JLabels to the panel?
Anyway, if you feel you still need a JTable then you can check out the Table Column Adjuster which attempts to fit the text. The basic code would be:
TableColumnAdjuster tca = new TableColumnAdjuster(table, 0);
tca.setColumnHeaderIncluded( false );
tca.adjustColumns();
Also, make sure you use JTable.setIntercellSpacing(...) to set the Dimension to 0.
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.