Reputation: 127
I have problem with my mouselistener on jTable. This is my code:
final JTable jTable1 = new JTable(tablemodel);
JScrollPane scroll = new JScrollPane(jTable1);
jTable1.addMouseListener(new MouseAdapter(){
public void mouseClicked(Event e){
System.out.println("clicekd on table");
}
});
But this code not working, When I click on the cell data from the sysout does not show up.
Eclipse tells me: The method mouseClicked(Event)
from the type new MouseAdapter(){}
is never used locally
Edit:
This is my function, everything works well except jTable.addMouseListener
public void categoryShow() {
// TODO Auto-generated method stub
appListener.getCategory();
List<Category> people = model.getPeople();
DefaultTableModel tablemodel;
tablemodel = new DefaultTableModel();
final JTable jTable1 = new JTable(tablemodel);
JScrollPane scroll = new JScrollPane(jTable1);
jTable1.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
System.out.println("clicekd on table");
}
});
tablemodel.addColumn("ID");
tablemodel.addColumn("Nazwa");
for (Category person : people) {
tablemodel.addRow(new Object[]{person.getId(),person.getName()});
}
JPanel controls = new JPanel(new BorderLayout(5,5));
JPanel buttons = new JPanel(new GridLayout(0,1,4,4));
JButton deletebutton = new JButton("Usuń");
JButton newrow = new JButton("Dodaj");
JButton print = new JButton("Drukuj");
print.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try{
jTable1.print();
}catch (PrinterException pe){
System.err.println("Blad przy drukowaniu");
}
}
});
jTable1.getModel().addTableModelListener(new TableModelListener(){
public void tableChanged(TableModelEvent e){
// TODO Auto-generated method stub
if(jTable1.getCellEditor() != null){
int col = jTable1.getSelectedColumn();
String columnname = jTable1.getColumnName(col);
System.out.println(jTable1.getSelectedColumn());
System.out.println("--" + columnname);
System.out.println(jTable1.getValueAt(jTable1.getSelectedRow(),jTable1.getSelectedColumn())); //nowa wartosc
System.out.println(jTable1.getValueAt(jTable1.getSelectedRow(), 0)); //id
}
}
});
newrow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
final JFrame bankTeller = new JFrame("Dodaj nową kategorie");
bankTeller.setSize(500, 280);
bankTeller.setLocationRelativeTo(null);
bankTeller.setResizable(false);
bankTeller.setLayout(new GridBagLayout());
bankTeller.setBackground(Color.gray);
//bankTeller.getContentPane().add(everything, BorderLayout.CENTER);
GridBagConstraints c = new GridBagConstraints();
JPanel acctInfo = new JPanel(new GridBagLayout());
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 1;
c.insets = new Insets(5,5,5,5);
bankTeller.add(acctInfo, c);
c.gridwidth = 1;
JLabel custNameLbl = new JLabel("Nazwa kategorii");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0,0,0,0);
acctInfo.add(custNameLbl, c);
c.weightx=1.;
c.fill=GridBagConstraints.HORIZONTAL;
custNameTxt = new JTextField("",1000);
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(5,5,5,5);
acctInfo.add(custNameTxt,c);
closeBtn = new JButton("Anuluj");
c.gridx = 0;
c.gridy = 3;
c.insets = new Insets(5,5,5,5);
acctInfo.add(closeBtn,c);
savingsBtn = new JButton("Dodaj");
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets(5,5,5,5);
acctInfo.add(savingsBtn,c);
bankTeller.setVisible(true);
closeBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
bankTeller.dispose();
}
});
savingsBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String name = custNameTxt.getText();
if(!name.isEmpty()){
fireCategoryEvent(new CreateCategoryEvent(name));
}else{
// showMessageDialog(this, "Uzupełnij pole nazwa", "Error", JOptionPane.WARNING_MESSAGE);
}
}
});
}
});
deletebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selRow = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
if(selRow >= 0) {
System.out.println(selRow);
}
}
});
buttons.add(newrow);
buttons.add(deletebutton);
buttons.add(print);
buttons.setBorder(new TitledBorder("Zarządzaj"));
controls.add(buttons,BorderLayout.NORTH);
card1.add(scroll);
card1.add(controls);
}
Upvotes: 0
Views: 4122
Reputation: 159784
The signature of
public void mouseClicked(Event e){
should be
@Override
public void mouseClicked(MouseEvent e) {
Upvotes: 2
Reputation: 127
I had to delete the files within the Project workspace:
And the folder:
Then I create new projekt with jre8 and import files. My JRE System Library in project = JavaSE-1.7 and everything works well. I must delete unnecessary @Override
in my function. My MouseListener function looks like @Reimeus told.
Upvotes: 0