reading the value of jtable selected row

2.9k views Asked by At

I am working with java RMI and swing and I have read the values from database but i am unable to read the value of selected row in this code. What i want to JTable to show all databases and it is showing all the available databases in server but i am unable to read the selected row value in this

package schoolclient;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import schoolserver.SchoolInterface;


public class DatabaseList {
    JFrame jFrame = null;
    JPanel jPanel = null;
    JList jList = null;
    JTable jTable = null;
    String data = null;
    schoolserver.SchoolInterface schoolInt = null;

    public DatabaseList(SchoolInterface sii) {

        schoolInt = sii;
        jFrame = new JFrame();
        jTable = new JTable(){
            public boolean isCellEditable(int row, int column) {                
                return false;               
        }
        };
        jTable.setModel(createTable());
        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable.addMouseListener(new MouseAdapter() {
            public void MouseClicked(MouseEvent e) {
                if(SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() == 2)) {
                    new createListSelection();
                }
            }
        });
        JScrollPane scrollPane = new JScrollPane(jTable);
        jFrame.add(scrollPane);
        jFrame.setSize(200, 200);
        jFrame.setVisible(true);
    }

    private DefaultTableModel createTable() {
        DefaultTableModel dtm = new DefaultTableModel();
            dtm.addColumn("Databases", createArray());
        return dtm;
    }

    private class createListSelection implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if(!e.getValueIsAdjusting()) {
                ListSelectionModel lsm = jTable.getSelectionModel();
                data = (String) jTable.getValueAt(jTable.getSelectedRow(), 0);
                System.out.println(data);
                }
        }
    }

    Object[] createArray() {

        ArrayList<Object> al = null;
        Object[] x = null;

        try {   
            al = schoolInt.availableDatabases();
            x = new Object[al.size()];
            int i = 0;
            for(Object o : schoolInt.availableDatabases())
                x[i++] = o;
        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(null, "no connection to database or "
                    + "remote server availabe", "Server Information", JOptionPane.OK_OPTION);
        }

        return x;
    }
}
1

There are 1 answers

1
DontKnowMuchBut Getting Better On BEST ANSWER

You look to be over-complicating things. Don't re-add listeners within a listener, but rather simply add one listener, a MouseListener to the JTable, and add it once. Within it check for double clicks (presses) and respond. Something like:

    jTable.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                data = (String) jTable.getValueAt(jTable.getSelectedRow(), 0);
                System.out.println(data);
            }
        }

    });

Other problems, your method within your MouseAdapter will never be called, ever:

    jTable.addMouseListener(new MouseAdapter() {
        public void MouseClicked(MouseEvent e) {
            // ....
        }
    });

Your capitalization is wrong, and since MouseAdapter/MouseListener does not have a MouseClicked method (it is mouseClicked) this method is never called. Always place an @Override annotation above any method that you think might be an override, and let the compiler warn you if it is not so. Had you done this, you'd get a prompt warning from the compiler.

Regarding your comment

  • You never add a selection listener to the JTable. Again the method within the MouseAdapter is never called since it is not capitalized correctly
  • Even if it did get called, what use is there repeatedly adding a ListSelectionListener?
  • If your goal is to only respond to double-clicks, a ListSelectionListener is not what you want. Only a MouseListener would work in this situation.
  • Do read the appropriate tutorials as they explain all of this and well. Please check out the links to be found within the Swing Info tag.