Get the value of a specific cell of JTable with mouse clicking - java

I have a JTable and Given such codes:
jTable.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println(jTable.getRowCount());
System.out.println(jTable.getModel().getValueAt(jTable.getRowCount(), 0));
}
});
If I click on a certain row, like in the picture above, I clicked the second row, how can I get that row's content?(How to get the Canada)?

Personally, I use the Mouse Clicked event. You could try something like this inside your event method:
private void myTableMouseClicked(java.awt.event.MouseEvent evt) {
int row = this.myTable.getSelectedRow();
int column = this.myTable.getSelectedColumn();
this.myTable.getValueAt(selectedRow, selectedColumn);
}
Be aware that the getValueAt method returns an Object. You probably will need to cast the Object returned into the object it is supposed to be. And also you could have a global variable that's going to have the value returned by getValueAt for using it as you need.
I hope it helps.

Related

Java: How to make a listener get specific data from a table in Java?

Ok so I need to make a listener that by clicking on a row it will retrieve me the data from one of the columns, the ID of the element on this row, but I have no clue on how to retrieve that. So far I made a listselectionlistener but it only gets the number of the row selected, not the data in the ID column. This is the listener method. Anyone can help me with that?
//This is the listener
ListSelectionModel model = NPC_Table.getSelectionModel();
model.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!model.isSelectionEmpty()) {
int selectedRow = model.getMinSelectionIndex();
}
}
});
Use the parameter to method valueChanged. It contains details of the actual event, i.e. the action performed by the user in order to select a row in the JTable.
int idColumn = 2; // Just a guess. I assume you know the correct column index.
ListSelectionModel model = NPC_Table.getSelectionModel();
model.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int row = e.getFirstIndex();
Object requestedValue = NPC_Table.getValueAt(row, idColumn);
}
}
});
Refer to How to Write a List Selection Listener.
Also consider adopting java naming conventions. Use npcTable instead of NPC_Table. Using conventions makes it easier for others to read and understand your code. After all, you are asking for help with your code, so you should make the effort to make your code as clear as possible for others to read.

How to get first row value from JTable

My question is that how to get first row value of jTable and display it in a textfield, but when ever the first row value changes, the value in the text field should change.
First off - your table has a TableModel.
You can access by calling
TableModel tm = table.getModel();
That TableModel has a method getValueAt(int row, int column) - use this to collect data from your first row (index 0).
The TableModel further allows for a TableModelListener to be added. That TableModelListener in turn receives TableModelEvents.
Use the event data to figure out if the first row was affected by your change and then apply the changed data to your textField:
public void tableModelChanged(TableModelEvent te) {
if(te.getFirstRow() == 0) { //First Row changed
//Receive Data and update TextField Here
}
}
Without knowing your specific case, I think this sounds like an application for using a TableCellListener, which will keep track of changes in your cell. Your jTable will fire a PropertyChangeEvent which is used by the listener. You might take a look at here and the code provided there to get the idea. Hope this helps in any way.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
node n=new node(Integer.parseInt(push.getText()));
q.push(n);
model=(DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{n.getele()});
push.setText(null);
}
Here is the code for my jtable, how do I get to the value of first row to the text field?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
node n=new node(Integer.parseInt(push.getText()));
q.push(n);
model=(DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{n.getele()});
push.setText(null);
}

How to get cell value of jtable depending on which row is clicked

I am trying to use an update method on my jtable that's connected to the database and would like to fill in the textfields on the form depending on which row the users clicks. I understand I will be needing a getValueAt() method however I am uncertain of how to fill in which row depending on which row the user clicks. I am unable to find anything on Google or anything so any information would be helpful!
private final UrTableModel urTableModel;
private JTable urTable;
...
// 1. Create your table model class that should extends from DefaultTableModel, instantiate it
urTableModel=new UrTableModel();
// 2. creates table
table = TableUtils.createStandardSortableTable(urTableModel);
// 3. customize your table
table.setBackground(Color.WHITE);
table.getTableHeader().setReorderingAllowed(false);
// 4. Add the mouse listner to it
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(final MouseEvent e) {
if (e.getClickCount() == 1) {
final JTable target = (JTable)e.getSource();
final int row = target.getSelectedRow();
final int column = target.getSelectedColumn();
// Cast to ur Object type
final UrObjctInCell urObjctInCell = (UrObjctInCell)target.getValueAt(row, column);
// TODO WHAT U WANT!
}
}
});
Cheers,
You will need to call getValueAt() your table's model to get the values you need. You will also need a listener on the table to listen for selections. So that once a user selects a row you call getValueAt() to get the value for the specific column of data in that row.

problem when implementing a selection listener in a JTable

I am developing a JTable with different rows. I would like to associate an event to the selection of a row in this table. I have used the following selection class to provide behaviour to the table selection:
public class TableSelectionListener implements ListSelectionListener{
public Integer item;
public TableSelectionListener(Integer item){
this.dialog = item;
}
public void valueChanged(ListSelectionEvent e) {
System.out.println("The row clicked is "+item);
}
}
When I create an instance of this table, sai tabletest, I have added the following piece of code:
tabletest.getSelectionModel().addListSelectionListener(new TableSelectionListener(tabletest.getSelectedRow());
The problem is that when I click on one row once, instead of retrieving the related message once, I retrieve the same message several times, suggesting that the actions repeated several times. For example:
The row clicked is 0
The row clicked is 0
The row clicked is 0
The row clicked is 0
Does anyone know where the problem may be?
Well, that's just normal.
Your selection listener is created with the value of tabletest.getSelectedRow() at its creation table (which is zero). And, as you never change the value of item in your listener, this listener fcan only display 0as a result.
If I were you, I wouold replace the valueChanged() method by something like (although it's untested and I remember strange things happens sometimes when mixing view and model row values) this :
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) // added as sometimes, multiple events are fired while selection changes
System.out.println("The row clicked is "+e.getFirstIndex());
}
Firstly, it's perfectly normal to get multiple ListSelectionEvents, while the selection is being changed. You can use the getValueIsAdjusting method to determine when selection has ended (it will return false).
Secondly, there's no need to construct your TableSelectionListener with a row number. When your valueChanged method is called, you can get the index of the first/last selected row (remember it's possibly to select multiple rows in the table, unless you disable that) using e.getFirstIndex() and e.getLastIndex() respectively.
An easier way of doing it, is as follows:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
System.out.println("e...."+table.getSelectedRow());
}
});

Java Swing: How to bind a JLabel's text to a column in the selected row of a JTable?

I am using Netbeans and am trying to find a way for the IDE to auto-generate the code for me. I remember binding a JLabel's text to a column in the selected row of the JTable before, but in that case, the JTable's values were from an entity manager, and it was very easy. I was wondering if there is a way to do it even if the JTable is not tied to a database.
Also, how else could one do it? I was thinking of implementing a ListSelectionListener, and whenever an event got generated, just update the text of the label.
I think your second solution is best way to do it, something like this:
public class LabelSyncer implements ListSelectionListener {
private JLabel toSync;
private int columnIndex;
public LabelSyncer(JLabel toSync, int columnIndex) {
}
public void valueChanged(ListSelectionEvent e) {
JTable table = (JTable) e.getSource();
int row = table.getSelectedRow();
toSync.setText(table.getModel().getValueAt(row, columnIndex).toString());
}
}
and then
table.getSelectionModel().addListSelectionListener(new LabelSyncer(label, columnIndex));
Something like this. Probably a more generic solution, but this should work.

Categories

Resources