How can I find out which row in a JTable the user just clicked?
Try this:
aJTable.rowAtPoint(evt.getPoint());
If you only ever care about listening to selections on the JTable:
jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int sel = jTable.getSelectedRow();
}
});
Related
I've a JTable with a ListSelectionModel and ListSelectionListener.
The selection model is set in the JTables constructor: lsm.getSelectionModel()
and the ListSelectionListener is set via a public method:
public void setListSelectionListener(ListSelectionListener l){
lsm.addListSelectionListener(l);
}
called from the Controller class:
view.setTableSelectionListener(new ListSelectionListener(){
#Override
public void valueChanged(ListSelectionEvent e){
if (!e.getValueIsAdjusting()) {
int viewRow = e.getFirstIndex();
System.out.println(viewRow + " is selected");
}
}
});
because the listener is created in another class I can't use the JTable's getSelectedRow(); method, but using the ListSelectionEvent object's getFirstIndex(); obviously doesn't get the current selection.
So I'm now using int viewRow = ((ListSelectionModel)e.getSource()).getLeadSelectionIndex());
Does that seem like the correct way to get the current selection? It seems to be working, but I'm not sure if this is a bad way of doing it. Thanks
Only getMinSelectionIndex() and getMaxSelectionIndex() works, which returns the min and max of selected indices respectively. Lead/anchor selected index may >= 0 even when there is no selected row.
I want to make a JComboBox in which a particular item text should change and becomes editable on selection.For example if JComboBox has two items "ONE","TWO" in it's list then on Selection of "TWO".
I have wrote a sample program in which either i can make field editable or can change the Text but not both.So someone please suggest how to make selective item editable and changed text as well
Object[] items = new Object[]{"One","Two"};
DefaultComboBoxModel dcbm = new DefaultComboBoxModel(items);
final JComboBox comboBox = new JComboBox(dcbm);
comboBox.setPreferredSize(new Dimension(200, 20));
comboBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
Object selectedItem = comboBox.getSelectedItem();
boolean editable = selectedItem instanceof String && ((String)selectedItem).equals("Two");
comboBox.setEditable(editable);
//comboBox.setSelectedItem("text has changed");
}
});
Something like...
String[] data = {"One", "Two"};
JComboBox<String> cb = new JComboBox<>(data);
add(cb);
cb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
cb.setEditable(cb.getSelectedIndex() != 0);
}
});
will basically do it, but what it won't do, is update the value of the model, just so you know ;)
If you want to make the editor "blank" when the combobox becomes editable, you could add...
if (cb.isEditable() && cb.getSelectedIndex() != -1) {
cb.setSelectedItem("");
}
to the ActionListener
So I'm not the best with jComboBox off the top of my head so this may not help but I would assume it uses an array to set the strings for the objects in the combo box along the lines of
(new String[] {"ONE","TWO"});
and with my understanding of arrays you could do something like
comboBox.addMouseListener(new MouseAdapter(){
public void ActionPerformed(MouseEvent click){
optionTwoClicked(click);
}
}
and then add the handler with something like
private void optionTwoClicked(MouseEvent click){
if (click.getSelectedItem()=String[2]){
String onTwo = JOptionPane.showInputDialog(null,"Enter your message","Messages",2);
textItem.setText()="onTwo";
}else{ //do something here?
}
}
Like I said before, not absolutely familiar with jComboBox, but,
Hope that helps!
I have a flextable, and in that table I have a column where each row has a button to remove that row. How can I get the index of that row so that I can remove it. I only need to get a index.
A solution:
FlexTable myTable = new FlexTable();
myTable.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Cell cell = myTable.getCellForEvent(event);
int receiverRowIndex = cell.getRowIndex();
}
});
Can someone help me with this listener?
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting()){
ListSelectionModel model = table.getSelectionModel();
int lead = model.getLeadSelectionIndex();
displayRowValues(lead);
}
}
private void displayRowValues(int rowIndex){
String country = "";
Object oCountry = table.getValueAt(rowIndex, 0);
country += oCountry.toString();
countryTxt.setText(country );
}
});
It's supposed to send data from cell in jtable (table) into a textfield (countryTxt) when one of the row's is selected, but it works only when I click on row and not when I'm cycling trough my table with arrow key's.
The problem is with this line:
if (e.getValueIsAdjusting()) {
Replace this with:
if (e.getValueIsAdjusting()) return;
This is a check for multiple selection events BTW.
If you comment out e.getValueIsAdjusting() it works.
http://docs.oracle.com/javase/6/docs/api/javax/swing/ListSelectionModel.html#setValueIsAdjusting(boolean)
I added a mouse clicked listner to my jtable, when i double click the row, will pop up an window accordingly.
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
double amount = Double.parseDouble(jTable.getValueAt(getSelectedRow(), 4).toString());
String remarks = jTable.getValueAt(getSelectedRow(), 3).toString();
String transactionID = jTable.getValueAt(getSelectedRow(), 1).toString();
new EditFrame(...)
}
});
This code I used to retrieve the row selected row.
public int getSelectedRow() {
jTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int viewRow = jTable.getSelectedRow();
selectedRow = viewRow;
System.out.println(viewRow);
}
});
return selectedRow;
}
In my case, I realised when I clicked the second row in the first time, I get null for selectedRow, only when I select first row then second row, I then can get the correct data. And If I removed the mouse listener the problem then be solved. Is it because I doing something wrong at the mouse click listener?
If you just want to know what row was clicked then you don't need the selection listener. Just use:
table.rowAtPoint();
You're doing it the wrong way. Remove your current getSelectedRow() method completely and never try to code something similar. Here is a better version:
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
int selectedRow = jTable.getSelectedRow();
double amount = Double.parseDouble(jTable.getValueAt(selectedRow, 4).toString());
String remarks = jTable.getValueAt(selectedRow, 3).toString();
String transactionID = jTable.getValueAt(selectedRow, 1).toString();
new EditFrame(...)
}
});