Java/JDBC - Using TableModels to display individual data - java

My hobby-level programing won't let me extend my program the way I want to, and my books/google won't help me out too much, hopefully you guys can!
I'm writing a simple database viewing program using the 'ResultSetTableModel' to import data from my local mySQL database.
The current program displays the entries in a nice list, and all of the data contained in each entry is presented as an individual cell in the JTable.
However, I now want to implement a feature to the JTable rows, that 'on click' displays the content (in my case an image using an image path)
However, after trying endlessly I cannot seem to find an easy and short way to add an eventhandler that does this for me.
I've managed to do similar things with manually populated tables before, but when trying to apply the lessons learned there to this new project gets me nowhere.

You can simply add a ListSelectionListener to the the table selectionmodel:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
// Last selected row:
table.getSelectedRow(); // -1 if no row selected
// All selected rows:
table.getSelectedRows();
}
});

Have you tried adding a MouseListener (or MouseAdapter) to the the table - e.g.
table.addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent e){
// do something cool...
}
}

Related

Swing design JTable auto refresh?

I have a question about my application's design.
I have 2 JTables (with my own TableModel) and I want to create a third table, which is built based on the selection in the other two tables. So when I start my application nothing is selected and this third table is supposed to be empty.
How do I find out what is selected in the two tables and how do I refresh the third one?
How do I find out what is selected in the two tables and how do I refresh the third one?
Please note you have two different yet related problems here.
First one is about selection in tables (see User Selections section of How to Use Tables tutorial). Basically you need to attach a ListSelectionListener to the table's ListSelectionModel in order to listen for selection changes. Something like this:
JTable table1 = new JTable();
table1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
// Implementation here
}
});
See JTable API:
public ListSelectionModel getSelectionModel()
public int[] getSelectedRows()
public int getSelectedRowCount()
public int convertRowIndexToModel()
The second problem is about refreshing/updating your third table. In order to accomplish this you need to work with the model of this third table by adding/removing/updating rows. Then this model will notify the view that something has changed and your table will be automatically updated.

Get Multiple raws selected in Jtable in java

Im triying to get all raws selected in table , I us the GetSelectedRaw() method to get the raw wich its selected by the user :
int raw_index = table.getSelectedRow();
but what if the user select more than one raw ??? I need to get all index of all raws selected in the table ...
I put onMoussPressed and onMoussReleased to the table :
int start_rows_to_delete; // the first selected raw
int end_rows_to_delete; // the last selected raw
private void tableMousePressed(java.awt.event.MouseEvent evt) {
start_rows_to_delete = table.getSelectedRow();
}
private void tableMouseReleased(java.awt.event.MouseEvent evt) {
end_rows_to_delete = table.getSelectedRow();
}
runing of prog. give me:
start_rows_to_delete = start_rows_to_delete !!!!!!
To more understande my goal , plz check this picture :
How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.
Best regards and thanks in advance, Fadel
From the JavaDocs
Returns the index of the first selected row, -1 if no row is selected.
Try using JTable#getSelectedRows, which will return an array of the selected row indicies
You may also want to take a look JTable#convertRowIndexToModel which will convert the view index to the model index which is useful when the table is sorted.
Instead of using a MouseListener you should use a ListSelectionListener which will let you know when the selection has changed, as the use may change the selection using the keyboard, which the MouseListener, obviously, won't tell you about.
Check out How to write a List Selection Listener for more details
Use the ListSelectionModel.
You can get it from a table Table.getSelectionModel()
The ListSelectionModel gives you the selected indexes. With them you can get the selected objects through the TableModel.

Detecting Selection Changes in a JTable

I'm trying to find a way to detect changes in which column the user selected in a JTable. I did some poking around and it appears that you need to somehow use a TableColumnModelListener in order to detect the changes, but that doesn't seem to fire an event when you change the column you have selected.
You need to add a ListSelectionListener instead. That will capture selection events. Here are some Swing tutorials that go further in depth:
http://download.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#selection
From what I read, I think you need to add a MouseListener to your table, which for example in mouseClicked will get the row and column using the following code, below:
table.addMouseListener(new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e)
{
Point pnt = evt.getPoint();
int row = table.rowAtPoint(pnt);
int col = table.columnAtPoint(pnt);
}
}
It should work great for you I have used similar thing myself before.
BTW it look similar to the problem I found on coderanch, link:
http://www.coderanch.com/t/332737/GUI/java/detect-single-click-any-cell
Good luck, Boro
If by "change" you mean changing the value of a cell then you can use an AbstractTableModel and implement the fireTableCellUpdated method

Java: JTable adding and moving columns

im quite new in Java.
I want to add Columns in a JTable at a specified index.
For this i am using addColumn(..) and then move them with moveColumn(...), this works great at the first time, but when i add another column it kind of moves also the other(before added columns).
Do you have any suggestions?
this is the code i've written in the TableModel is:
public void addColumn(Object columnName,
Vector columnData, JTable table) {
int moveTo = ((Integer)columnName);
boolean unselected = moveTo==-1;
super.addColumn(this.getColumnCount(), columnData);
if(!unselected) {//if a column was selected
table.moveColumn(this.getColumnCount()-1, moveTo+1);
}
}
this works great at the first time,
but when i add another column it kind
of moves also the other(before added
columns).
I don't understand what that means.
If you need more help post your SSCCE that demonstrates the problem.

JTable selection change event handling: find the source table dynamically

I've implemented my own event handler and added it to the selection model of the table:
table.getSelectionModel().addListSelectionListener(event);
And implemented the method for "event" (mentioned above):
public void valueChanged(ListSelectionEvent e) {
log.debug("value changed");
}
Unfortunately the event fires twice if I chance the selection and it doesn't seem possible to find the associated table, because e.getSource provides javax.swing.DefaultListSelectionModel.
Hence my questions are:
1) Why does it fire twice although the eventListener is only registered once?
2) How can I find the table for which the selection applies? The DefaultListSelectionModel doesn't seem to offer any getSource() or similar.
Many thanks!
Thanks Draemon..It Works fine....
Our Code
vMachinesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
System.out.println("Selection Changed");
}
}
});
Thanks By
TF Team
1) I think you'll find it fires once for de-selecting the old selection and once for selecting the new selection. If you log the details of the event you should see exactly what's going on. I can't remember the details, so perhaps this is wrong. Either way you should be able to call getValueIsAdjusting() on the event and only use the last one in the chain (ie when it returns false).
2) You shouldn't normally need to, but AFAIK the only way to do this is to create your Listener specifically for the table (ie pass the table to the constructor and remember it).
Since more than one JTable (or other component I'm guessing) can share the same selection model, it doesn't make sense to ask for the associated JTable from the event. This is the same reason that you can't retrieve a JTable from a TableModel. As Draemon suggests, store the reference to the JTable in (or make it accessible to) your listener class.

Categories

Resources