Usage of Jtextarea in JTable - java

I have a JTABLE with three columns. First column is textfield, the Second and third is JTextarea.. Using tab key I can navigate from one cell to another. While navigating from Jtextfield column to JTextarea column , The JTextarea is not highlighted, how to set a color for JTextarea when it get focused , so I can easily find which column has the focus.. How I will set that color.?
Please give some suggestions..
Thanks in Advance
Vishwa

Create a Color object and invoke the setBackground(Color) method on your JTextArea, after checking if it has focus using the hasFocus().
For example:
JTextArea area = new JTextArea();
Color c = Color.CYAN;
if(area.hasFocus())
{
area.setBackground(c);
}
Don't forget to save the original color of the (accessible by calling area.getBackground()) before you change it, and to set it back to this when it loses focus.

Related

How to set columns in a JTextArea to show text

I'm using (in a JPanel with GridLayout) some JTextAreas (with Editable=false) to show some text after a query to a database XML.
JTextArea obj = new JTextArea();
obj.setColumns(37);
obj.setText(r.getProtocolloList().get(i).getOggetto());
The problem is that this text can be quite long and it is showed all in a single line so that the user has to scroll the horizontal JScrollPane to read the rest.
I thought that setting the columns the line would be restricted so that the text would be showed in different lines. But nothing happens.
Is there a way to get that?
Thanks
Use the setLineWrap(); method:
obj.setLineWrap(true);

JTable cell value passed to textarea on single click

On my form I have a jtable and a textarea. My table only has 2 columns - ID and Comment
Is it possible that when a user clicks on a cell in the comment column. The cell value will appear in the textarea in edit mode?
I did set the cell editor to singleclick
selectTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableColumn col = selectTable.getColumnModel().getColumn(1);
DefaultCellEditor singleclick = new DefaultCellEditor(new JTextField());
singleclick.setClickCountToStart(1);
col.setCellEditor(singleclick);
I have a method outputSelection() that gets called from a edit button. The method gets the value from the selected cell and puts the value in the textarea for edit.
Can the click activate the method so the user does not have to click a edit button?
You could attach a mouse listener to the table and monitor the mouse clics from there, getting the selected column/row & thus the value
You could supply your own cell editor that updates the text area when the editors value is set
You could extend te jtable & when cell editing is started, update the text area
Yes, this is a process I learned to use after having duplicate code throughout my swing application. I started making standalone methods that did the work I wanted, and then I call those methods from the action events from the button or mouse click. That way they all execute the same code.
Even if you have a tab or enter key command, you can also have it execute your same method as the others for more consistent code.
If your button performs specific code with cell values, just extract all of that code out into a method that takes the cell value as input. Then you can call that same method from any event and pass in the input data you want to display in the text area.

Is it possible to gain Tab Focus of a JTable Header Cell?

I have a JTable that has a table header which contains a JPanel with a JLabel with a mouse listener on the JLabel. When the JLabel is clicked a dialog is shown. I'm trying to trigger this JLabel in the JTable heading without a mouse. In order to trigger this JLabel without a mouse, I need the focus to be on the JTable heading (that is, I need the JTable heading to be in the focus traversal policy). Does anybody know if its possible to Tab to a JTable heading cell?
table.getTableHeader().setFocusable(true)
alternatively, you might add the action "triggered by the JLabel" to the table's action map and bind it to a keyStroke in the table's when_ancestor inputMap
Focus can be placed on the TableHeader by using F8 when focus is on the table.
You can then use the right/left keys to move between the header cells.
You use the Tab key to place focus back on the table.

JComboBox sizing issue in a table with Multi line Cells

I recently had a problem where I needed to have a field that can wrap the text and increase the height of the row as the text is wrapped, similar to Microsoft Excel. I managed to get it working correctly, the only problem is that the table contains multiple JComboBoxes. When the row's height increases from the field that wraps the text, the size of the JComboBox window and ArrowButton also increase. I am using a DefaultCellEditor for the JComboBox fields, and created my own Editor/Renderer to be used with the JTextArea field. Once the JComboBox's value is selected, the value gets displayed correctly in the field, the only problem is while I am selecting the value, the JComboBox window and ArrowButton could be HUGE depending on the size of the row. Is there any way to increase the height of the row, but have the JComboBox field height remain the same instead of growing to fill the column that it's in? I am thinking I might need to make a Custom Cell Editor for the JComboBox fields as well instead of using the default. Thanks in advance!
I am thinking I might need to make a
Custom Cell Editor for the JComboBox
fields as well instead of using the
default
That would probably be the solution since the size of the editor is determined by the size of the cell.
I would try using a JPanel with a BorderLayout as the editor component. Then you add your editor to the North of the panel.
It would be the easiest editor to create since all the mouse events and key events are passed to the editor I believe, which means the panel will get the events, not the combo box. So I guess you would need to forward these events to the combo box.
First, is the JComboBox in a BorderLayout and set to BorderLayout.CENTER?
If so, I'd change it to a different layout such as AbsoluteLayout so it does not stretch to fill the cell.
Also, I will also refer you to this post Putting JComboBox into JTable

Is it possible to have a JPanel over JTable

I have a JTable whose associated TableModel could be initially empty. Therefore, it currently shows a JTable with its columns and no rows.
In order to fill this JTable, I want the user to drag and drop elements from another component. The problem is that I would like to hint the user that he/she should drag elements to this table, with some message like "Drag xxx here to add a row".
I thought that I could achieve this by putting a panel over the JTable , but I don't think it is possible with any java layout.
Does anyone know how to do this? Or should I stick to a CardLayout to switch to/from the hint and the JTable?
Thanks a lot
Take a look at OverlayLayout, I think it might do what you want.
A couple of suggestions:
Add a tooltip to the JTable using setToolTipText(String). You'll need to add it to the surrounding JScrollPane in order for the tooltip to be displayed when the user hovers on the empty viewport.
Add a titled Border; e.g. scrollPane.setBorder(BorderFactory.createTitledBorder("Drag items here"));
I would do it like this...Create a JPanel to contain the JTable on top and a JLabel on the bottom. Something like this...
JPanel container = new JPanel( new BorderLayout() );
container.add( table, BorderLayout.CENTER );
JLabel label = new JLabel( "Drag XXX here to add a row" );
container.add( table, BorderLayout.SOUTH );
Then make both the label and table drop targets. You might need to play with the label to make it look and behave right. Set opaque to true possibly? Set its background to white to match the table? Also maybe play with the borders of all three to make them look integrated with each other. You could remove the label after the first row is added if that is the behavior you want.

Categories