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
Related
I've added several rows to a Jtable, and I don't know if it's possible but I'd like if you click on any cell within a particular column then the connecting row is removed.
Are functions like this possible?
(I'm not asking anyone to do all the work for me. Just asking for information or a tutorial link) thanks :-)
attach a mouse listener to the table and when mouse click event occurs if column matches your particular column then remove that row.
tbl.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = tbl.rowAtPoint(e.getPoint());
int col = tbl.columnAtPoint(e.getPoint());
if(col == SPECIFIC_COLUMN_INDEX){
((DefaultTableModel)tbl.getModel()).removeRow(row);
}
}
});
This is my code:
JTable1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
{
JTable target = (JTable)e.getSource();
Point pMouse = new Point();
pMouse = target.getMousePosition();
}
}
}
So I am retrieving the point (coordinates) relative to the JTable. So let's say the user clicks somewhere in a cell and the returned Point is X=272 and Y=50. So now I want to position a JDialog exactly by those coordinates. I tried:
jDialog1.setLocation(pMouse);
jDialog1.setVisible(true);
But this positions the Dialog somewhere else (the coordinates of the screen instead of the Table). Does somebody have a suggestion on how I can position the JDialog relative to the cell?
You are using the co-ordinates in relation to the client area of the JTable content. You want the global co-ordinates in relation to the entire window. For this you can use:
Point location = MouseInfo.getPointerInfo().getLocation();
jDialog1.setLocation(location);
In general, a user should be able to use your application either by mouse or keyboard. What happens if the user tabs to that cell? Should they not be able to see the same dialog? So for a more general solution that works whether you use a mouse or not:
SwingUtilities.convertPointToScreen(point, table);
Check out the other convertXXX methods in SwingUtilities for future reference.
Or, you can always use:
Component.getLocationOnScreen();
and then add on the mouse point.
I am writing a small POS application, which shows a JTable with ticket information inside its cells. CellRenderer is a class which extends JPanel and implements TableCellRenderer, and contains a few JTextFields showing basic information (quantity, descripcion, price).
Also, I have another class extending JPanel and implementing TableCellEditor, which is used as CellEditor. This class contains more JTextFields and also a few jButtons.
What I need is simple: when I edit any cell by clicking with the mouse (or touching the screen, which is, as far as I know, the same event), dynamically increase the height of the cell I'm going to edit, so it can show all the components inside the editor. And when I finish editing, return cell height to its previous value.
Any idea about doing it?
Thanks in advance. :-)
CellRenderer is a class which extends JPanel and implements TableCellRenderer, and contains a few JTextFields showing basic information (quantity, descripcion, price). Also, I have another class extending JPanel and implementing TableCellEditor, which is used as CellEditor. This class contains more JTextFields and also a few jButtons.
better could be to create popup window (JDialog) based on event from JPopupMenu,
Dynamically Increase JTable row height when editing, and decrease when finish edit
don't confused users and wrong concept could be caused by jumping JTables row on the screen
What I need is simple: when I edit any cell by clicking with the mouse (or touching the screen, which is, as far as I know, the same event), dynamically increase the height of the cell I'm going to edit, so it can show all the components inside the editor. And when I finish editing, return cell height to its previous value.
don't do that, but have to override, is possible by
DefaultCellEditor#setClickCountToStart(int) for TableCellEditor
start, stop and cancelEdit for CellEditor
have to notify or re_Layout JTable, the same on stop and cancelEdit
Not an answer to how-to-adjust-rowHeights, but for an alternative mentioned in my comment: "oversize" the editorComponent only instead of updating the complete rowHeight (which I think would be too irritating to users, but up to you to decide, of course :)
// fake a bigger editing component
JTextField oversized = new JTextField() {
#Override
public Dimension getPreferredSize() {
Dimension dim = super.getPreferredSize();
dim.height *= 5;
return dim;
}
};
TableCellEditor editor = new DefaultCellEditor(oversized);
JTable table = new JTable(new AncientSwingTeam()) {
#Override
public boolean editCellAt(int row, int column, EventObject e) {
boolean result = super.editCellAt(row, column, e);
if (result) {
// adjust cell bounds of the editing component
Rectangle bounds = getEditorComponent().getBounds();
bounds.height = getEditorComponent().getPreferredSize().height;
getEditorComponent().setBounds(bounds);
}
return result;
}
#Override
public void removeEditor() {
int editingColumn = getEditingColumn();
super.removeEditor();
if (editingColumn >= 0) {
// cleanup
repaint();
}
}
};
table.getColumnModel().getColumn(0).setCellEditor(editor);
Didn't try it, but I would say implementing MouseListener's mouseClicked() method is the way to go. Keep track of whether the cells height was already increased, and change the height accordingly.
Since MouseListener is an interface, CellRenderer could implement this interface too, keeping all cell-behavior in one class.
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 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());
}