3

I'm fairly new to java, especially this side of java. I am currently trying to create a JTable which will take in a CSV file of data, the table will then be displayed onto a GUI which you can select specific parts of the data, creating visualisations from it. At the moment I am using a MouseListener for mouseClicked - this prints out the cell to the command line correctly. However, when using a MouseMotionListener for mouseDragged it just prints out the cell clicked (from mouseClicked) multiple times. I know it's something to do with mouseClicked (or atleast think it is) but can't get my head around it.

Code:

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Color;

public class TableView extends ChartGUI{

        /**
         *
         */
        public JScrollPane GetPanel() {
                return scrollPane;
        }
        public void SetScrollPane(JScrollPane pane) {
                scrollPane = pane;
        }
        public TableView(String title, Object[][] data) {
                if(data != null) {
                        Object[] columnNames = data[0];
                        Object[][] tableData = new Object[data.length][];
                    for(int i = 0; i < (data.length - 1); i++){
                        tableData[i] = data[i+1];
                    }

                    table = new JTable(tableData, columnNames);
                    table.setCellSelectionEnabled(true);
                    JScrollPane scrollPane = new JScrollPane(table);
                    scrollPane.setPreferredSize(new Dimension(PANE_WIDTH, PANE_HEIGHT));
                    SetScrollPane(scrollPane);
                } else {
                        System.out.println("Null Data");
                }

        /** MouseListener to allow users to select specific cells, printing the data to the command line */            
                table.addMouseListener(new MouseAdapter() {              
                        public void mouseClicked(MouseEvent evt) {       
                                col = table.getSelectedColumn();  
                                row = table.getSelectedRow();  
                                selData = GetData(table, row, col);

                                System.out.println("Selected data: MouseClicked ::  " + (String)selData);
                        }  
                });
                table.addMouseListener(new MouseAdapter() {
                        public void mouseReleased(MouseEvent evt) {
                                col = table.getSelectedColumn();
                                row = table.getSelectedRow();
                                selData = GetData(table, row, col);

                                System.out.println("Selected data: MouseReleased ::  " + (String)selData);
                        }
                });
                table.addMouseMotionListener(new MouseMotionAdapter() {
                        public void mouseDragged(MouseEvent evt) {                     
                                col = table.getSelectedColumn();  
                                row = table.getSelectedRow();  
                                selData = GetData(table, row, col);

                                System.out.println("Selected data: MouseDragged ::  " + (String)selData);

                        }        
                });

        }      
        /** A method to return the values at an index selected by the user, as a String */
        public String GetData(JTable table, int row_index, int col_index){
                return (String)table.getModel().getValueAt(row_index, col_index);
        }  

        public static void main(String[] args){
                System.out.println("Why are you doing this?, maybe we should have some JUnit Tests");          
    }

        private static final long serialVersionUID = 4901866775892531001L;
        private JScrollPane scrollPane;
        private JTable table;
        private String selData;
        private int col;  
        private int row;
        private int PANE_WIDTH = 900;
        private int PANE_HEIGHT = 600;
}

Thankyou for any help, much appreciated!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Connor McFadden
  • 441
  • 1
  • 4
  • 20
  • 1
    You haven't said what should be printed. At the moment, you're printing the selected cell, but we don't know what you want to print. If you want to print the whole selection each time the selection changes, you shouldn't use Mouse and MouseMotion listeners, but a ListSelectionListener added on the table's selection model. – JB Nizet Apr 10 '13 at 17:24
  • 1
    unrelated: a) [don't use setXXSize, ever](http://stackoverflow.com/a/7229519/203657) b) Please learn java naming conventions and stick to them. – kleopatra Apr 10 '13 at 18:40
  • The advice to never use setPreferredSize(), setMaximumSize(), and setMinimumSize() is complete nonsense. You should most definitely should use them sparingly and only when absolutely needed, and you need to know which layout managers support each of those methods. However, those methods are hints to the layout manager and are perfectly valid to use when needed. – Michael Apr 30 '13 at 14:10

1 Answers1

0

Table cells are not selected by mouse motion, they are selected by clicking. Your mouseDragged method will print out the info of the selected table cell every time it is fired, but the selected table cell only changes when you click. You probably want to get the current mouse position from the mouseEvent and use that info to select a new table cell, then print out the information.

Eric Fitting
  • 160
  • 1
  • 7