Results 1 to 5 of 5

Thread: JTABLE problem

  1. #1
    MaximInf is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Default JTABLE problem

    Hi ,
    i have this code
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class CellSelectionExample implements ListSelectionListener {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run() {
                    new CellSelectionExample().go();
                }
            });
        }
     
        void go() {
            JFrame f = new JFrame("CellSelectionExample");
            f.getContentPane().add(new JScrollPane(createTable()));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
     
        JTable createTable() {
            table.setCellSelectionEnabled(true);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.getSelectionModel().addListSelectionListener(this);
            table.getColumnModel().getSelectionModel().addListSelectionListener(this);
            return table;
        }
     
        @Override()
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                System.out.format("(%2d,%2d)%n",
                    table.getSelectedColumn(), table.getSelectedRow());
            }
        }
     
        private JTable table = new JTable(20,10);
    }
    the problem is in this method:public void valueChanged(ListSelectionEvent e)

    Sometimes when I make a click on a cell, is printed 2 times the same value. Why?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    An event is generated for deselection and again for selection. To filter out the deslection part you can use the ListSelectionEvent getValueIsAdjusting method.

  3. #3
    MaximInf is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Default

    Example? how can I fix this code?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Sorry, I didn't look closely enough at the code you posted. I see you already have the getValueIsAdjusting method in place.
    The multiple events are being sent by the two listSelectionModels which you added your ListSelectionListener to. The table selectionModel reports selection changes for the rows and the columnModel selectionModel reports changes in the column selection. So you only get two events reported when you select both a new row and a new column. You can filter for these if you like. Maybe something like:
    Java Code:
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                Object source = e.getSource();
                if(source == table.getSelectionModel())
                    System.out.printf("row    %2d selected%n",
                                       table.getSelectedRow());
                else if(source == table.getColumnModel().getSelectionModel()) 
                    System.out.printf("column %2d selected%n",
                                       table.getSelectedColumn());
            }
        }

  5. #5
    MaximInf is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-06-2009, 11:15 PM
  2. JTable problem
    By robbertds1 in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 06:34 PM
  3. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  4. problem in redrawing JTable
    By abhinav in forum AWT / Swing
    Replies: 0
    Last Post: 11-21-2007, 09:08 PM
  5. Problem with JTable
    By Felissa in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 09:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •