I have an histogram created with JFreeChart API. This is a StackedBar Histogram:
My histogram represents the datas in the JTable on the right. For example the field thea=11 is present two times as Int32 and two times as Int64.
I implemented a mouse event on my chart: when one presses on a bar the corresponding record of the Jtable is selected.
For example if one presses 11.0 bar in the JTable I get:
This is my listener:
chartPanel.addChartMouseListener(new ChartMouseListener() {
#Override
public void chartMouseMoved(ChartMouseEvent arg0) {
}
#Override
public void chartMouseClicked(ChartMouseEvent arg0) {
try{
TableModel model = table.getModel();
CategoryItemEntity entity = (CategoryItemEntity) arg0.getEntity();
Comparable row = entity.getRowKey();
Comparable col = entity.getColumnKey();
System.out.println(String.valueOf(row));
String field = String.valueOf(col);
for(int i = 0; i<model.getRowCount(); i++) {
if(model.getValueAt(i, 0).equals(field)) {
int realRowNumber = table.convertRowIndexToView(i);
table.changeSelection(realRowNumber, 0, false, false);
break;
}
}
}
catch (Exception e) {
System.out.println("No bar selected");
}
}
});
Listener works with a single bar, but it does not work correctly when I have the bar divided in two or more portions.
For example if I press on the blue portion of 11 bar, the selected row in the JTable on the right is the first with 11 as thea, so in my case the selected row is [11,Int32,2] instead of [11,Int64,2] that is the right row to select.
Related
I created a simple mouse event. When the user clicks the JTable it will fetch the records in the JTable and display them in the JTextField. In this case I am trying to display the ID from the Table into the Text Field.
public void fetchRec() {
xtable.addMouseListener(new MouseAdapter() {
public void rowClicked(MouseEvent evt){
xtable =(JTable) evt.getSource();
int row = xtable.rowAtPoint( evt.getPoint() );
int column = xtable.columnAtPoint( evt.getPoint() );
String s=xtable.getModel().getValueAt(row, column)+"";
idLabelField.setText(s);
}
});
}
I am calling the method here but it keeps telling me that rowClicked method is unused. I don't understand how its unused? Everything else I am calling is working except this.
public void bookDimensions() throws Exception {
addTextLabels();
addTextFields();
addPanelButtons();
addRecord();
addTable();
fetchRec();
}
Turn on cell selection and listen to the selection model instead of mouse events. See java: how to select only one cell in a jtable and not the whole row
I am facing the same problem mentioned here SWT: Table lost selection . I am using ubuntu 12.04 NOT windows. Is there any way to highlight the selected row of a SWT table even after focus lost. I tried adding focus listener to the table and in focus lost I changed the selected item background colour and on focus gain resets the background colour. See the code.
#Override
public void focusLost(FocusEvent e) {
System.out.println("table focus los");
TableItem item = fileListTable
.getItem(fileListTable.getSelectionIndex());
prevSelItemBackground = item.getBackground();
item.setBackground(soureWindow.getSelectionBackground());//Some random colour
System.out.println(fileListTable.getSelectionIndex());
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("table focus gain");
TableItem item = fileListTable
.getItem(fileListTable.getSelectionIndex());
item.setBackground(prevSelItemBackground);
System.out.println(fileListTable.getSelectionIndex());
}
But it is not working. Is there any other solution/workaround for this?
The below snippet can be used:
this.fileListTable.addSelectionListener(new SelectionListener() {
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// Nothing to do
}
#Override
public void widgetSelected(SelectionEvent e) {
int selectionIndex = fileListTable.getSelectionIndex();
TableItem[] items = fileListTable.getItems();
TableItem newItem;
for (int i = 0; i < items.length; i++) {
String first = items[i].getText(0);
String second = items[i].getText(1);
String third = items[i].getText(2);
items[i].dispose();
newItem = new TableItem(fileListTable, SWT.NONE);
newItem.setText(new String[] { first, second, third });
if (i == selectionIndex) {
newItem.setForeground(soureWindow.getSelectionForeground());//Or Anyother color
newItem.setBackground(soureWindow.getSelectionBackground());//Or Anyother color
} else {
newItem.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));//Default foreground color
newItem.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));//Default background color
}
}
}
});
Its is working fine for me but not tested on larger data.
I have a situation where I have a popup menu created when a JTable is right clicked on. Standard way of creating the popup menu:
aJTable.setComponentPopupMenu(rightClickMenu);
Now afterwards in the action that gets registered, I am unable to find out which cell was right clicked on to get that popup menu to appear.
rightClickMenuItem.addActionListener(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
// Work out what cell was right clicked to generate the menu
}
});
Any ideas on how you do this?
Astonishing fact: with a componentPopupMenu installed, a mouseListener never sees the mouseEvent that is the popupTrigger (reason is that showing the componentPopup is handled globally by a AWTEventListener installed by BasicLookAndFeel, and that listener consumes the event).
The only place which sees the mousePosition of that trigger is the getPopupLocation(MouseEvent), so the only reliable way to get hold of it (for doing location dependent config/actions) is #Mad's suggestion to override that method and store the value somewhere for later use.
The snippet below uses a clientProperty as storage location:
final JTable table = new JTable(new AncientSwingTeam()) {
#Override
public Point getPopupLocation(MouseEvent event) {
setPopupTriggerLocation(event);
return super.getPopupLocation(event);
}
protected void setPopupTriggerLocation(MouseEvent event) {
putClientProperty("popupTriggerLocation",
event != null ? event.getPoint() : null);
}
};
JPopupMenu popup = new JPopupMenu();
Action action = new AbstractAction("show trigger location") {
#Override
public void actionPerformed(ActionEvent e) {
JPopupMenu parent = (JPopupMenu) SwingUtilities.getAncestorOfClass(
JPopupMenu.class, (Component) e.getSource());
JTable invoker = (JTable) parent.getInvoker();
Point p = (Point) invoker.getClientProperty("popupTriggerLocation");
String output = p != null ? "row/col: "
+ invoker.rowAtPoint(p) + "/" + invoker.columnAtPoint(p) : null;
System.out.println(output);
}
};
popup.add(action);
popup.add("dummy2");
table.setComponentPopupMenu(popup);
#MadProgrammer's suggestion of getPopupLocation looked promising, but I couldn't work out how to get the information across between the table and the actionEvent...
I got around this by making sure that the row was selected when you rightclicked on it -> since the popup menu prevents the selection of the row, you can add in a mouse listener that makes sure the row gets selected no matter what click (left or right) is pressed.
aTable.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
int r = aTable.rowAtPoint(e.getPoint());
if (r >= 0 && r < clt.getRowCount()) {
aTable.setRowSelectionInterval(r, r);
} else {
aTable.clearSelection();
}
}
});
This means that in the rightClickMenuItem's action listener, you can grab the table's selected cell / row
rightClickMenuItem.addActionListener(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
aTable.get details about the selected one....
}
});
Too easy! Thanks everyone for the help.
JTable has methods
int row = rowAtPoint(p);
int col = columnAtPoint(p);
So pass the MouseEvent's point and use the values
Add a MouseListener and store the last right click point somewhere.
I have a JTable which is created with the use of an EventTableModel and is in a JScrollPane. The EventTableModel takes live updates from an eventList and displays the result in the table. As new results come into the table and the new piece of information is displayed at the top of the table.
However, what I want to be able to do is freeze the table to show what is currently displayed when I press a button called 'Lock Table'. This button should have the same effect as the eclipse console 'Scroll Lock', therefore as new items appear the current items should remain on the screen and not be pushed off as new items appear. But new items should still be added just not automatically scrolled to.
Does anyone know how I can try achieve this functionality. So that as update come in, the data that is on the table is not forced off screen, therefore focuses remain on the current data when the check box is pressed.
Thanks for any help.
Michael
Basic procedure (for inserting above the current display area)
install a TableModelListener on the table's model
on enable lock: note the number of rows below the current visible rectangle
on receiving inserts while locked, scroll so that the number of rows below are kept constant
some working code (using JXTable, as it has convenience method for scrolling, for a core table simply do the calculations yourself :-)
public static class ScrollLock {
private JXTable table;
private boolean blocked;
private int rowsBelow;
public ScrollLock(JXTable table) {
this.table = table;
table.getModel().addTableModelListener(getTableModelListener());
}
private TableModelListener getTableModelListener() {
TableModelListener l = new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
if (!blocked) return;
if (e.getType() == TableModelEvent.INSERT) {
updateInsert(e.getFirstRow(), e.getLastRow());
}
}
};
return l;
}
protected void updateInsert(int firstRow, int lastRow) {
// PENDING: assumption is that insert always above
// need additional logic for other cases
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Rectangle r = table.getVisibleRect();
int row =table.rowAtPoint(new Point(0, r.y + r.height));
int lastVisible = table.getRowCount() - rowsBelow;
table.scrollRowToVisible(lastVisible);
}
});
}
public void block() {
Rectangle viewRect = table.getVisibleRect();
int lastVisibleRow = table.rowAtPoint(new Point(0, viewRect.y + viewRect.height));
rowsBelow = table.getRowCount() - lastVisibleRow;
blocked = true;
}
public void unblock() {
blocked = false;
rowsBelow = -1;
}
}
I use the TableViewer to show informations in a table. The user can select one of the shown options by selecting one line of the table.
I want to create a table in matrix form, in which the user can not only select the line. It should be possible to select every item of the table, like row 2 column 3. For every item selection an action is called to handle this item as it is in the TableViewer.
As far as i now, i can add CellModifier and CellEditors to the line of Columns of the table, but the reference for the action is always the line object and not the selected TableItem.
Does somebody have an example how to create such a matrix inside a Composite?
I can create it by setting a GridLayout and adding the components in a for-loop, but than i get issues, when i want to redraw the Composite with new childrens. The TableViewer does already have this handling, so i dont want to implement it again.
I had the same problem a while ago and the only way I found to solve it was to register a mouse listener on the SWT table widget associated to the table viewer.
MouseListener columnSelectionMouseListener = new ColumnSelectionMouseListener();
getViewer().getTable().addMouseListener(columnSelectionMouseListener);
public class ColumnSelectionMouseListener implements MouseListener {
private TableColumn selectedColumn;
#Override
public void mouseDoubleClick(MouseEvent e) {
// Nothing to do here
}
#Override
public void mouseDown(MouseEvent e) {
table = (Table) e.widget;
TableItem item = table.getItem(new Point(e.x, e.y));
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn column = table.getColumn(i);
Rectangle bounds = item.getBounds(i);
if (bounds.contains(e.x, e.y)) {
selectedColumn = column;
}
}
}
#Override
public void mouseUp(MouseEvent e) {
// Nothing to do here
}
public TableColumn getSelectedField() {
return selectedColumn;
}
}
Then, for example in the viewer's selection listener, you can ask to the mouse listener which column was selected when the mouse has been pressed and combine that with the selected line coming from the viewer's selection to perform the appropriate action.
Hope this can help.
Manu
Maybe the following JFace snippet will help:
Snippet058CellNavigationIn34
Ingo