SWT - wrap text in table cells - java

I am trying to find a way to wrap text in table cells using SWT, but
I'm stuck. Does anybody know of a way to do it? Or is there a
different way to approach the problem? Basically, I have 12 columns:
6 of them are check boxes, and remaining are text fields only.
The text in the columns should be wrap automatically if the length of the size
exceeds the width of the column.
Please help me

SWT Table does not support automatic line wrapping. You can however insert newline characters yourself. See the following links for further reference:
Bug report
TableItem does not support mnemonics, wrapping, or the SWT.WRAP style.
multiline feature or wrap text feature in jface tableviewer
Java2s example

Related

Is there a way to wrap the column header in SWT Table Header?

I have an SWT Table where the Table header is big and the table data is a single digit. I am not able to wrap it using SWT.WRAP. Is there anyway to wrap the text or any other solution to make the column smaller ?
There is no support for automatically wrapping text.
Note: you can only specify the SWT.xxx flags that are documented in the Javadoc for a control. Other flags are ignored or in a few cases may cause problems. For TableColumn only SWT.LEFT, SWT.CENTER and SWT.RIGHT are supported.

Setting highlighted text color in a row in a table with Java Swing

I know we are almost in 2018 but I'm stuck at work having to use Java Swing.
I wanted to be able to highlight particular pieces of text in one or more rows. That was quite easy, as I could use html tags for that:
The problem as can be seen, though, is that when I select a row, the inverted colors are terrible. I would like to prevent having the foreground text color turning white when a row is selected (I would think that, for instance, keeping it black would be fine).
Is this possible?
Thanks
You may use a <font> tag , that is among the supported html tags .
e.g :
"<html><font color=\"black\">"+YourText+"</font></html>"
Some more information here :
Multiple Strings colored in different way into the same cell of a JTable

how can i programatically select a particular text from JTable when i do search in it?

here are the screenshots of the application
Rows will be displayed in the Table according to the text which is written in the search textfield.
Now i want to mark that particular text as per the shown in the second image with the yellow color
I know how to select a row or a particular cell.
but I don't know how to select a particular text inside the cell of any row in the table.
I am guessing you know how to search in JTable, so I am not pasting code of it here.
You can look over the SwingX library. It has this kind of function as you said predefined it it. You just need to add it to your table. This is where you can find it. Give it a try you will surely like it.
The basic premise would be to use a custom TableCellRenderer that provided the functionality that you require.
The problem is how to implement it.
I would create a TableCellRenderer based on a JTextField, remove it's border and make it transparent. This will allow you to use the text highlighting functionality provided by JTextCompoent to highlight portions of the text, as demonstrated here.
The next problem is then knowing what to highlight. There are a number of possibilities.
You could provide a method in your table model that could return the current text that should be highlighted.
I'd, personally, probably use the JTable#putClientProperty and JTable#getClientProperty methods to seed the search text.
Or, you could actually provide a simple model that directly to the renderer which had a method that returned the current search text. This might actually be more useful as you could link it to field, the method building the filter and the renderers and allow them to simply seed each other

How can I present multiline data in grid like component in Java and AWT

I have a few records of data (less then 10). Each record consists of a few lines of text.
I want to present records to the user in a kind of grid, where user can select one of the records.
I was thinking about List component or jTable, but I couldn't make them displaying more then one line of text. What component should I use then, or how to approach this?
In subject I suggested AWT because size does matter, i.e. I want use this functionality in the applet and would like to avoid any extra libraries.
Thanks in advance
Thanks to maksimov's link I found examples of how to tackle this issue, and also very interesting link I missed somehow - http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
To specify that a component's text has HTML formatting, just put the
tag at the beginning of the text, then use any valid HTML in
the remainder. Here is an example of using HTML in a button's text:
button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");
In my case it was just enough to set height of the row and add tag just before string data to be displayed. HTML tagging also let me use extra formatting, colors, etc,
Brilliant,
Thank you maksimiov

Java JTextArea Question

I am designing an on-screen form to be filled in, and I think it makes sense to stick it together as a collection of text areas. I note in the documentation of JTextArea that a text area can be subdivided into rows and columns, but I can't find any methods that appear to deal with placing text directly in any specific row/column cell in a text area.
Are there such methods, or is there an alternative text component that would work better for this purpose?
Thanks in advance for any insights.
John Doner
If you want a table, there is JTable but it is a bit more complicated. (Here is a tutorial) Alternatively you could put your JTextAreas into a layout such as GridLayout
A bit hackish, but you could insert (row-1) newlines and (column-1) space characters before your actual content. Of course that would only work on a previously empty text area.
However, you can extend that approach so that you only insert characters if needed, and otherwise just count already existing characters. That is, to go to a row, you skip the first row-1 newlines. Then in that line, you skip the first column-1 characters.
If there aren't enough newlines or characters already in the text area, you add more at the end of the text.
However, it gets trickier if your text content contains newlines itself.
It's doable, but it's gonna be ugly.
The row and columns values are just used to give the text area a preferred size.
I don't see the point of trying to set text at a specific row when creating a form. Generally forms would be designed with label/text field pairs, so the user know where there are entering the text.
If you text area is for output then you just add new lines when you want to display text on a different line.
Forcing a text area to be able to insert text at a given row/column is definitely not the way it was intended to be used.
I don't understand the real requirement so I can't make any other suggestions.

Categories

Resources