I have a JTable with a set of uneditable cells and I want all the cells in a particular column to have a different mouse cursor displayed whilst the mouse is hovering over them.
I am already using a custom renderer and setting the cursor on the renderer component doesn't seem to work (as it does for tooltips).
It does seem to work for editors.
Is this not possible in JTable when your cell is not being edited or am I missing something?
Add a MouseMotionListener to the JTable and then on mouseMoved() determine which column it is using JTable's columnAtPoint() and if it's the particular column you are after, setCursor() on the JTable.
Here is one way of changing the cursor at a particular column in JTable:
if(tblExamHistoryAll.columnAtPoint(evt.getPoint())==5)
{
setCursor(Cursor.HAND_CURSOR);
}
else
{
setCursor(0);
}
I wanted to only change the cursor when the mouse was over the text in the cell. This was my solution:
private JTable table = ...;
#Override
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
int column = table.columnAtPoint(point);
int row = table.rowAtPoint(point);
Component component = table.getCellRenderer(row, column).getTableCellRendererComponent(table,
getValueAt(row, column), false, false, row, column);
Dimension size = component.getPreferredSize();
Rectangle rectangle = table.getCellRect(row, column, false);
rectangle.setSize(size);
if (rectangle.contains(point)) {
table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
return;
}
table.setCursor(Cursor.getDefaultCursor());
}
Related
So, the thing is that I have a JTable which turns rows orange when the data on one of the columns is 'true' (using a class which extends DefaultCellRenderer), problem is I don't want to show that column, so I've tried removing it or setting its width to 0, but when I do that, it no longer highlights the row in orange. Is there a way to do this or does it always have to be shown info?
Thanks.
I've tried removing it or setting its width to 0, but when I do that, it no longer highlights the row in orange
I guess because the width is 0, there is nothing to render so the renderer is never invoked.
In any case, don't use a cell width of 0. As you tab through the table that column will still get focus, but the user won't know it has focus which will confuse the user.
Instead if you want to hide a column then remove the TableColumn from the TableColumnModel. You get the TableColumnModel from the JTable by using the getColumnModel() method. Removing the column from the model just prevents the column from being displayed but the data is still contained in the TableModel.
I have a JTable which turns rows orange when the data on one of the columns is 'true'
You can override the prepareRenderer(...) method of the JTable to do this easily. This will work even you have columns with different types of data so there is no need to create multiple renderers.
The basic logic is:
JTable table = new JTable(...)
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
if (!isRowSelected(row))
{
c.setBackground(getBackground());
int modelRow = convertRowIndexToModel(row);
boolean highlight = (Boolean)getModel().getValueAt(modelRow, ???);
if (highlight) c.setBackground(Color.ORANGE);
}
return c;
}
};
Check out Table Row Rendering for more information and a working example. The example on the "Data" tab does what you want.
Can you try this. I assume that you are familiar with table cell renderer. you can override the getTableCellRendererComponent method and change the color like below:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// Get default renderer from the table
TableCellRenderer renderer = table.getDefaultRenderer(table.getColumnClass(column));
Component comp = renderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (value.equals("true") && row == 1 && col== 1){
comp.setBackground("YOUR COLOR");
}
}
I'm developing a simple java application using swing. I use JTable element.
The problem is that by default rows of tables are white and grey like in this post Setting color in a row of a Jtable .
I want to make them the same color, for example all rows white.
You can override the prepareRenderer method of JTable like this
JTable table = new JTable(...)
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
c.setBackground(Color.WHITE);
return c;
}
};
Or you could create your own TableCellRenderer which does the same thing (picking the background color to render) but on a Cell level and use that renderer for each of your columns.
I have JTable and right now I get the Point of a clicked point in a cell like this:
table.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
JTable target = (JTable)e.getSource();
Point pMouse = new Point();
pMouse = target.getMousePosition();
}
}
When I click in a specific place in cell 1 I get:
java.awt.Point[x=527,y=32]
If I click on the same place in cell2 I get:
java.awt.Point[x=527,y=96]
The Y is different and that is of course because it's different cells. But how do I get so that both X and Y is the same when I click in the same place in different cells?
I need to get MousePoint relative to cell only.
I think that you looking for JTable.rowAtPoint(Point point), the same for ColumnModel
don't forget to convertColumnIndexToModel, the same for RowIndex (JTables view can be sorted, filtered, ColumnModel can be reordered, column(s) can be removed from JTables view too)
for more info please to read Oracles JTable tutorial, part Specifying Tool Tips for Cells
I have a jTable as from the attached picture
Right click on a row starts a jPopup, with a single item "Thread Stop".
I would like to return the row number by clicking on this menu item
How to accomplish this?
Thanks.
In your MouseListener where you show your popup, simply get the row and column numbers via the JTable methods:
table.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
System.out.printf("row, col: [%d, %d]%n", row, col);
// show pop-up menu here
}
});
Your implementation of TableCellEditor includes the row as a parameter, but you should act only when the TableModel is updated, as shown here. TablePopupEditor is a related example.
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());