JTable Cells - Handling Long Text - java

I have a JTable where one column occasionally has a fair amount of text in it. There are algorithms that we're using to expand the height of each row to the tallest cell. The problem is for long text cells we get "fat" rows.
It looks something like this:
=============================
| Col1 | Col2 | This is some|
| | | very long |
| | | text! |
=============================
I've considered a couple solutions:
Clipping the text and adding a mouse listener to "expand" the clipped text
Clipping the text and adding a tooltip or dialog to show the extra contents
Does anyone know of any libraries that fix this? I'm open to using some other technique...I'm not convinced my solution is the best.
Thanks in advance!

I would just use a tooltip.
You can override the getToolTipText(() method of JTable to do this.
JTable table = new JTable(...)
{
public String getToolTipText( MouseEvent e )
{
int row = rowAtPoint( e.getPoint() );
int column = columnAtPoint( e.getPoint() );
Object value = getValueAt(row, column);
return value == null ? null : value.toString();
}
};
Or if its only for certain columns you can use the renderer to set the tooltip text. See Specifying Tool Tips for Cells.

It took me a while to find out how to display the tooltip on Netbeans but your answer helped so much.
here it is implemented on netbeans GUI Builder...
right click on your Jtable->customize code.
choose "custom creation" where the new "javax.swing.JTable();" code is and before the semi colon ; add the code of the answer below... looks like this:
YourTable = new javax.swing.JTable(){
//add tooltip to display the full cell text when not displayed
public String getToolTipText( MouseEvent e )
{
int row = rowAtPoint( e.getPoint() );
int column = columnAtPoint( e.getPoint() );
Object value = getValueAt(row, column);
return value == null ? null : value.toString();
}
}
;

Adding a mouse motion listener to your JTable, getting the value of where the mouse is at, then adding the long value to a tooltip is an efficient way to easily see the longer table cell value when the mouse is hovered over that row:
JTable yourTable = new JTable(<the data>, <the fields>);
// Add a mouse motion listener to your JTable
yourTable.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {
// do nothing
}
#Override
public void mouseMoved(MouseEvent e) {
// Set the tooltip to an empty string
yourTable.setToolTipText("");
// Get the table cell value where the mouse is located
String value = (String) yourTable.getValueAt(yourTable.rowAtPoint(e.getPoint()),
yourTable.columnAtPoint(e.getPoint()));
// If the length of the value is greater than some number...
if (value.length() > 50) {
// add a tooltip to the JTable that shows the value
yourTable.setToolTipText(value);
}
}
});
See result here: https://i.stack.imgur.com/3LHyc.png

Related

jTable not coloring row correctly

i have a problem, basically my program looks like this:
the thing is, it works, it is supposed to color the rows that have "N" in green, but the first time it loads the values, as you can see, the first row have bits of white, but for some reason if i click the list it fixes the issue, i need the rows to be colored properly without the user needing to click on the list to fix the issue, this is my Render code:
public class Render extends JTable {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
int columnIndex){
Component componente = super.prepareRenderer(renderer, rowIndex, columnIndex);
String val = getValueAt(rowIndex, columnIndex).toString();
if(val.equals("N")){
componente.setBackground(Color.GREEN);
}
return componente;
}
}
I figured i could use Repaint(); in the MouseMoved event in the JTable, but i think is not a proper way to fix it... Any help is appreciated, cheers!
Start by changing
String val = getValueAt(rowIndex, columnIndex).toString();
to
String val = getValueAt(rowIndex, 3).toString();
This will ensure that no matter what column the cell represents, you are checking the appropriate columns value - this is why the leading cells are white
You should also consider providing a default color for when the column values doesn't match
if (val.equals("N")) {
componente.setBackground(Color.GREEN);
} else {
componente.setBackground(getBackground());
}

How to get the currently selected cell's indices in JTable immediately after it is highlighted?

I'm building a spread sheet application. But this is not a question like using table.getSelectedColumn() and table.getSelectedRow() to find the selected cell in a JTable.
In Microsoft Excel, when we navigate through cells using arrow keys, the content in the cells are displayed in the Formula Bar immediately after highlighting the cell. Here, the most important thing is, when a cell is highlighted by the selection as above, the value inside the cell is displayed at the sametime. So my question is, how can we do the same in JTable?
I have tried to do something similar using keyEvent listener, but the problem with that is, when a key event is generated, the next cell is being highlighted but the indices of the previous(which was previously highlighted) is being returned in the getSelectedRow() and getSelectedColumn() methods.
Also i tried the ListSelectionListener. but the same fault exists.
If there's any way to get the selected cell's indices immediately after the new cell is highlighted when navigated using arrow keys, that will work. Also an event should be generated since I want to update the formula bar as in Excel. Can someone help me with this?
Thanks in advance!
You can use this simple trick!
Only have to make two excess jTextfields. (can set to be invisible at the run time)
Try to get an idea from below code segment.
private int r;
private int c;
private String buffer;
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
jTextField2.requestFocus();
buffer = jTextField1.getText();
jTable1.getModel().setValueAt(jTextField1.getText(), r, c);
}
private void jTable1KeyTyped(java.awt.event.KeyEvent evt) {
r = jTable1.getSelectedRow();
c = jTable1.getSelectedColumn();
jTable1.putClientProperty("terminateEditOnFocusLost", true);
jTextField1.requestFocus();
}
private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {
buffer = (String)jTable1.getModel().getValueAt(r, c);
jTextField1.setText(buffer);
jLabel1.setText(buffer);
}
private void jTextField2FocusGained(java.awt.event.FocusEvent evt) {
buffer = jTextField1.getText();
jTable1.getModel().setValueAt(buffer, r, c);
jTextField1.requestFocus();
}

Implement a rename function for a cell in jtable in java

As a beginner,i am creating a jtable with some functionalities like adding and removing contents. I would like to know how to make a rename functionality to my application that on selecting this menu should highlight all the contents of the cell as in an editing mode. Thanks in advance
Continuing from your previous post.... Did you want something like below, when when you hit edit in the context menu, you can edit in some popup window?
→
You pretty much already have to tools for this functionality (in your code). For the new Action, you simply need to show a JOptionPane input dialog with the value of the selected cell. The return input of the JOptionPane will be the value you set back to the table. Something like. Keep in mind though, depending on the type of data, you may want to do some logical parsing or conversion. Below I just take the value as a String.
class EditCellAction extends AbstractAction {
private JTable table;
public EditCellAction(JTable table) {
putValue(NAME, "Edit");
this.table = table;
}
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
String newValue = JOptionPane.showInputDialog(table,
"Enter a new value:", table.getValueAt(row, col));
((DefaultTableModel) table.getModel()).setValueAt(
newValue, row, col);
}
}
If you don't want the popup, and just want to programmatically start the cell editing, you can simple use table.editCellAt(row, col) to start the editing, and use the underlying text field of the cell editor to select the field contents. Something like below (tested and works)
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
table.editCellAt(row, col);
JTextField field = (JTextField) ((DefaultCellEditor) table
.getCellEditor()).getComponent();
field.requestFocus();
field.setSelectionStart(0);
int endSelection =
(!field.getText().isEmpty()) ? field.getText().length() -1 : 0;
field.setSelectionEnd(endSelection);
}
Keep in mind though, if the cell is editable, the user can just double click the cell to edit it. I guess this adds some more functionality

Need to update JTable2 along with the JEditorPane on mouse click event on JTable1, all are in the same jframe

I am displaying 2 JTables and a JEditorPane in a JFrame. Both the Tables have different data. On double clicking the table2 i want to update table1 and the editor pane. I am able update the editor pane but not the table1. I tried the add e.getClickCount() == 2 for table1 , but its not working.
Basically when i click a row(which is the thread number) in Tabel2, the editorPane and table1 should update with the thread details. which looks like-
| 3105 | BOUNDARY_CORE_FCS | 20101216 105754399 | Entering XATransaction::getInstance
on doubleClick I am able to display that in the editorPane but not able to update it in the table. Any help would be greatly appreciated. Thanks.
The Code below is addMouseListener for table2-
JTable clsNewJTable = new JTable(new RCGUITableModel(arroData, arroHeaders));//... table2
JTable m_clsJTable = RCGUI.m_clsJTable2;// ... table 1
clsNewJTable.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if (e.getClickCount() == 2){
JTable clsNewJTable1 = (JTable)e.getSource(); // gets table 2
int rowIndex = clsNewJTable1.getSelectedRow();
int colIndex = clsNewJTable1.getSelectedColumn();
clsNewJTable1.getSelectedRows();
Object strCellValue = clsNewJTable1.getValueAt(rowIndex, colIndex);
doUpdateThreadsInTextArea(strCellValue); // this displays in the jeditorPane
//Should i create the new table1 here?and then update it or adding a new mouselistener to table1 is better?
clsNewJTable1.setVisible(true);
}
}
});
More code is a must. More interesting would be to see how exactly you are making the update of the components. Do you fire table data change for the changed table'd model, e.g. tablemodel.fireTableDataChanged()?
Hope this helps, Boro.

Show a tooltip above a cell in a JTable

I need to show a tooltip above (or below :) a cell when the user enter a wrong value in it (see the image below).
I have a tooltip, but I need a Point to display it at the right position, so I want to get a cell position. Do you know how to get this?
BUT, if you have a better solution to realize this behaviour, I'm open to all proposition (especially for the fact that the tooltip is not bind with the cell/Jtable/Panel and if I move/close/minmize my window the tooltip is display at the same position)
Thanks,
Damien
Please refer below code snippet, and you'll get the solution
JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
jc.setToolTipText(getValueAt(row, column).toString());
}
return c;
}
};
If you want to only show the specific cell, all you have to do is change the column param in the params of getValueAt(...) method to a specific column which contains that cell
You have an example of such feature in the Swing components visual guide.
Edit: In fact, it is not really a tooltip that you need here, as the tooltip need to have the cursor positionned over the cell. You want to display the tooltip even if the cursor is outside the cell, right?
Anyway, an alternative solution is to change the background of the cell when the value entered by the user is invalid (in orange or red for example), and then add a "real" tooltip (using the link I provided) in order to give the user a complete error message.
Just use below code while creation of JTable object.
JTable auditTable = new JTable(){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
try {
//comment row, exclude heading
if(rowIndex != 0){
tip = getValueAt(rowIndex, colIndex).toString();
}
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
Try getCellRect
Use the following code to get the value for the correct row if using RowSorter:
jc.setToolTipText(getValueAt(convertRowIndexToModel(row), column).toString());

Categories