MouseClicked not triggered for JTable in a GUI with both Action and Mouse Listener
Tag : java , By : Scott Everts
Date : March 29 2020, 07:55 AM
this will help You don't need a MouseListener anywhere, simply let the button's Action query the table's selection state, something like Action delete = new AbstractAction("Delete Selected Row") {
@Override
public void actionPerformed(ActionEvent e) {
if (table.getSelectedRow() < 0) return;
int modelRowIndex = table.convertRowIndexToModel(table.getSelectedRow());
((DefaultTableModel) table.getModel().removeRow(modelRowIndex);
}
};
JButton button = new JButton(delete);
|
JButton's action listener not working in JTable
Date : March 29 2020, 07:55 AM
it helps some times I think that while the button is shown on the table, it is not really a button but a cell that is rendered like a button. You can listen to the table's model to see when the used clicked on a cell that has a button and act when this happens.
|
Tag : java , By : ArmHead
Date : March 29 2020, 07:55 AM
hop of those help? Seen as you seem to be determined to follow this path, you have a number of options... The hard way... public void actionPerfomed(ActionEvent e) {
TableModel model = //... Create the new model based on you needs
JTable table = new JTable(model);
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class ModelPane extends JPanel {
private JTable table;
public ModelPane(TableModel model) {
setLayout(new BorderLayout());
table = new JTable(model);
add(new JScrollPane(table));
}
}
public class ModelFrame extends JFrame {
public ModelFrame(TableModel model) {
setLayout(new BorderLayout());
add(new ModelPane(model));
pack();
setLocationRelativeTo(null);
}
}
public void actionPerfomed(ActionEvent e) {
TableModel model = //... Create the new model based on you needs
ModelFrame frame = new ModelFrame(model);
frame.setVisible(true);
}
public abstract class AbstractModelAction extends AbstractAction {
public abstract TableModel getModel();
@Override
public void actionPerformed(ActionEvent e) {
ModelFrame frame = new ModelFrame(getModel());
frame.setVisible(true);
}
}
public class CurrencyModelAction extends AbstractModelAction {
@Override
public TableModel getModel() {
return //... Create the new model based on you needs
}
}
|
JTable - Checkbox add action listener
Date : March 29 2020, 07:55 AM
I wish this helpful for you For an example, when my check box is checked I need to pop up a confirmation message.
|
How to add action listener to JTable?
Tag : java , By : Tamizhvendan
Date : March 29 2020, 07:55 AM
I hope this helps you . For add action listner in JTable cell you can do in this way. You can rename this Calender_Table variable into calenderTable. calenderTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
// do some stuff
}
}
});
|