简体   繁体   English

Java jTable MouseListener无法正常工作

[英]Java jTable MouseListener not working

I have problem with my mouselistener on jTable. 我在jTable上的mouselistener有问题。 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. 但是此代码不起作用,当我单击sysout中的单元格数据时不会显示。 Eclipse tells me: The method mouseClicked(Event) from the type new MouseAdapter(){} is never used locally Eclipse告诉我:从未在本地使用new MouseAdapter(){}类型的方法mouseClicked(Event)

Edit: This is my function, everything works well except jTable.addMouseListener 编辑:这是我的功能,除了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);
    } 

The signature of 的签名

public void mouseClicked(Event e){

should be 应该

@Override
public void mouseClicked(MouseEvent e) {

I had to delete the files within the Project workspace: 我必须删除Project工作区中的文件:

  • .project 。项目
  • .classpath .classpath

And the folder: 和文件夹:

  • .settings 。设置

Then I create new projekt with jre8 and import files. 然后,我使用jre8创建新的项目并导入文件。 My JRE System Library in project = JavaSE-1.7 and everything works well. 我在项目= JavaSE-1.7中的JRE系统库,一切正常。 I must delete unnecessary @Override in my function. 我必须删除函数中不必要的@Override My MouseListener function looks like @Reimeus told. 我的MouseListener函数看起来像@Reimeus。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM