How to select an entire row instead of single cell

I'm making my project and there is this one problem. I can't select the entire row. I tried setRowSelectionAllowed(true) and overriding isCellEditable() but nothing really works.

I have this code.

I have a table that displays all my on-hand products first.

        String query = "select * from tbl_items where status = 'Available'";
        Connection cn_oh_readRows = null;
        Connection cn_oh_readContent = null;

        int countRows = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            cn_oh_readRows = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");           
            Statement state = cn_oh_readRows.createStatement();

            ResultSet rowsCount = state.executeQuery(query);

            while (rowsCount.next()){
                countRows++;
            }
        }
        catch(Exception ex){}

        Object [][] rows_data = new Object [countRows][7];

        try {
            Class.forName("com.mysql.jdbc.Driver");
            cn_oh_readContent = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");            
            Statement state = cn_oh_readContent.createStatement();


            ResultSet rowsRead = state.executeQuery(query);

            int c = 0;
            while (rowsRead.next()) {
                rows_data[c][0] = rowsRead.getString(1);
                rows_data[c][1] = rowsRead.getString(2);
                rows_data[c][2] = rowsRead.getString(3);
                rows_data[c][3] = rowsRead.getString(4);
                rows_data[c][4] = rowsRead.getString(5);
                rows_data[c][5] = rowsRead.getString(6);
                rows_data[c][6] = rowsRead.getString(7);
                c++;
            }
        }
        catch(Exception ex){};

        Object columnNames [] = {"Item code", "Item name", "Brand", "Original Price", "Retail Price", "Quantity", "Classification"};

        DefaultTableModel tableModel = new DefaultTableModel(rows_data, columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        tbl_onHand = new JTable(rows_data, columnNames);
        tbl_onHand.setAutoCreateRowSorter(true);
        tbl_onHand.setShowGrid(false);
        tbl_onHand.getTableHeader().setReorderingAllowed(false);
        tbl_onHand.getTableHeader().setResizingAllowed(false);
        tbl_onHand.setModel(tableModel);
        tbl_onHand.setRowSelectionAllowed(true);
        tbl_onHand.setSelectionBackground(Color.pink);


        sp_tbl_onHand = new JScrollPane(tbl_onHand);
        sp_tbl_onHand.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        sp_tbl_onHand.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp_tbl_onHand.setBounds(0, 40, 780, 232);

Then there's this JTextField that is used for searching.

    txt_search_onHand = new JTextField();
    txt_search_onHand.setBorder(new border_round().round);
    txt_search_onHand.setBounds(2, 15, 210, 20);

I have this DocumentListener to change the value of the JTable when a string is inserted to the JtextField.

    DocumentListener dl_onHand = new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {proc_onHand();}
        @Override
        public void insertUpdate(DocumentEvent e) {proc_onHand();}
        @Override
        public void changedUpdate(DocumentEvent e) {}
    };

This is proc_onHand():

private void proc_onHand() {
    String search_onHand = txt_search_onHand.getText();

    String  query_onHand = "select * from tbl_items where status = 'Available' and item_code like '%" + search_onHand + "%' or "
            + "status = 'Available'  and name like '%"+ search_onHand +"%' or "
            + "status = 'Available' and brand like '%" + search_onHand + "%' or "
            + "status = 'Available' and classification like '%" + search_onHand + "%'";


    Connection cn_oh_readRows = null;
    Connection cn_oh_readContent = null;

    int countRows = 0;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        cn_oh_readRows = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");           
        Statement state = cn_oh_readRows.createStatement();

        ResultSet rowsCount = state.executeQuery(query_onHand);

        while (rowsCount.next()){
            countRows++;
        }
    }
    catch(Exception ex){}

    Object [][] rows_data = new Object [countRows][7];

    try {
        Class.forName("com.mysql.jdbc.Driver");
        cn_oh_readContent = DriverManager.getConnection("jdbc:mysql://localhost/cashiering and inventory system","root","");            
        Statement state = cn_oh_readContent.createStatement();


        ResultSet rowsRead = state.executeQuery(query_onHand);

        int c = 0;
        while (rowsRead.next()) {
            rows_data[c][0] = rowsRead.getString(1);
            rows_data[c][1] = rowsRead.getString(2);
            rows_data[c][2] = rowsRead.getString(3);
            rows_data[c][3] = rowsRead.getString(4);
            rows_data[c][4] = rowsRead.getString(5);
            rows_data[c][5] = rowsRead.getString(6);
            rows_data[c][6] = rowsRead.getString(7);
            c++;
        }
    }
    catch(Exception ex){};


    Object columnNames [] = {"Item code", "Item name", "Brand", "Original Price", "Retail Price", "Quantity", "Classification"};

    DefaultTableModel tableModel = new DefaultTableModel(rows_data, columnNames){
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };

    tbl_onHand = new JTable(rows_data, columnNames);
    tbl_onHand.setAutoCreateRowSorter(true);
    tbl_onHand.setShowGrid(false);
    tbl_onHand.setSelectionBackground(Color.pink);
    tbl_onHand.getTableHeader().setReorderingAllowed(false);
    tbl_onHand.getTableHeader().setResizingAllowed(false);
    tbl_onHand.setModel(tableModel);
    tbl_onHand.setRowSelectionAllowed(true);

    sp_tbl_onHand = new JScrollPane(tbl_onHand);
    sp_tbl_onHand.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    sp_tbl_onHand.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sp_tbl_onHand.setBounds(0, 40, 780, 232);


    pnl_onHand.add(sp_tbl_onHand, BorderLayout.CENTER);
}

I can select an entire row on the temporary viewing of the table, wherein it displays all the on-hand products. But when I insert a string unto the txt_search_onHand to filter the value in the JTable, the entire row of result in the JTable cannot be selected, it's selected but just the few cells instead. I have to drag my mouse to select all the cells.

Please help me! Am I doing properly? Please guide me. I am new to Java! Thanks.


table.setSelectionModel(ListSelectionModel.SINGLE_SELECTION);
链接地址: http://www.djcxy.com/p/48824.html

上一篇: JTable停止单元格编辑,无需用户单击

下一篇: 如何选择整行而不是单个单元格