Results 1 to 9 of 9
  1. #1
    Ralphw is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default Jtable + TableRowSorter

    Hi *

    i am currently facing this problem:

    if u select a row in the table and finally sort the table the coloured row will not stay on the selected one but will jump to different ones.

    if i comment out the method getSelectedRows (only use the super call) it will work for SINGLE_SELECTION, but for MULTI_SELECTION the problems keeps the same.

    Try the demo.

    Any ideas whats wrong ??

    greets
    Ralph
    <code>
    /*
    * TableSortDemo.java requires no other files.
    */

    import java.awt.Dimension;
    import java.awt.GridLayout;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableModel;

    public class TableSortDemo extends JPanel
    {
    public TableSortDemo()
    {
    super(new GridLayout(1,0));
    ExtJTable table = new ExtJTable();
    table.setModel(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 100));
    table.setFillsViewportHeight(true);
    table.setSortierung(true);
    table.setHeaderLabels(new MyTableModel().columnNames);
    table.setContents( new MyTableModel().data );
    table.setRowSelectionInterval(0,0);
    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.getViewport().add(table, null);
    //Add the scroll pane to this panel.
    add(scrollPane);
    }

    class MyTableModel extends AbstractTableModel
    {
    public String[] columnNames = {"First Name",
    "Last Name",
    "Sport",
    "# of Years",
    "Vegetarian"};
    public Object[][] data = {
    {"Mary", "Campione",
    "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
    "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
    "Knitting", new Integer(2), new Boolean(false)},
    {"Sharon", "Zakhour",
    "Speed reading", new Integer(20), new Boolean(true)},
    {"Philip", "Milne",
    "Pool", new Integer(10), new Boolean(false)},
    };

    public int getColumnCount() {
    return columnNames.length;
    }

    public int getRowCount() {
    return data.length;
    }

    public String getColumnName(int col) {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    return data[row][col];
    }

    public Class getColumnClass(int c) {
    return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
    //Note that the data/cell address is constant,
    //no matter where the cell appears onscreen.
    if (col < 2) {
    return false;
    } else {
    return true;
    }
    }

    public void setValueAt(Object value, int row, int col)
    {
    data[row][col] = value;
    }

    }

    public class ExtJTable extends JTable
    {
    private String[] headerLabels;
    private boolean sortierung = false;
    public ExtJTable()
    {
    this.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    this.getTableHeader().setReorderingAllowed(false);
    }

    public void setHeaderLabels(String[] pHeaderLabels)
    {
    headerLabels = pHeaderLabels;
    }

    public void setContents(final Object[][] pContents)
    {
    TableModel dataModel = new AbstractTableModel()
    {
    public int getColumnCount() { return headerLabels.length; }
    public int getRowCount() { return pContents.length;}
    public Object getValueAt(int row, int col) {return pContents[row][col];}
    public String getColumnName(int column) {return headerLabels[column];}
    public Class getColumnClass(int c)
    {
    Object o = this.getValueAt(0, c);
    if (o != null)
    {
    return o.getClass();
    }
    else
    {
    return Object.class;
    }
    }

    public boolean isCellEditable(int row, int col)
    {
    return false;
    }

    public void setValueAt(Object aValue, int row, int column)
    {
    pContents[row][column] = aValue;
    }
    };

    this.setModel(dataModel);
    }

    /**
    * Bei Sortierung muss der RowIndex in den passenden Index im Model
    * konvertiert werden
    */
    public int getSelectedRow()
    {
    int selectedRowIndex = super.getSelectedRow();
    if (sortierung)
    {
    if (selectedRowIndex < 0)
    {
    return selectedRowIndex;
    }

    selectedRowIndex = this.convertRowIndexToModel(selectedRowIndex);
    }

    return selectedRowIndex;
    }


    public int[] getSelectedRows()
    {
    int[] tJetztIndexe = super.getSelectedRows();
    if (sortierung)
    { // analog getSelectedRow()
    int[] tReturn = new int[tJetztIndexe.length];
    for (int i = 0; i < tJetztIndexe.length; i++)
    {
    int tJetztIndex = tJetztIndexe[i];
    //tReturn[i] = sorter.getRealIndex(tJetztIndex);
    tReturn[i] = this.convertRowIndexToModel(tJetztIndex);
    }
    return tReturn;
    }
    return tJetztIndexe;
    }

    public void setSortierung(boolean pSortierung)
    {
    sortierung = pSortierung;
    this.setAutoCreateRowSorter(sortierung);
    }


    }
    /**
    * Create the GUI and show it. For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    */
    private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TableSortDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    //Create and set up the content pane.
    TableSortDemo newContentPane = new TableSortDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }
    }
    </code>

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you do not override both getSelectedRow and getSelectedRows method, it works correctly

  3. #3
    Ralphw is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    if you do not override both getSelectedRow and getSelectedRows method, it works correctly

    yes thats right, but if i sort the rows and finally select any row, to edit the attributs i did not get the correct row because the model is not synchronized with the view.
    This is the reason why i implement the methods.

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    edit the attributs?
    do you mean click the checkbox or modify text of table cell?

    if yes, return true in isCellEditable in AbstractTableModel of ExtJTable Class

  5. #5
    Ralphw is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    edit the attributs?
    do you mean click the checkbox or modify text of table cell?

    if yes, return true in isCellEditable in AbstractTableModel of ExtJTable Class
    please adapt the demo code with

    <code>
    table.addMouseListener(new java.awt.event.MouseAdapter()
    {
    public void mouseClicked(MouseEvent e)
    {
    do_it();
    }
    });

    private void do_it()
    {
    String values = "";
    if (table.getSelectionModel().getSelectionMode()== ListSelectionModel.SINGLE_SELECTION )
    {
    int row = table.getSelectedRow();
    table.getModel().getValueAt(row, 0);
    javax.swing.JOptionPane.showMessageDialog(this,"Na me is "+ table.getModel().getValueAt(row, 0));
    }
    else
    {
    int[] rows = table.getSelectedRows();
    for (int i = 0; i < rows.length; i++)
    {
    values = values + " " + table.getModel().getValueAt(rows[i], 0);
    javax.swing.JOptionPane.showMessageDialog(this,"Na mes are "+ values );
    }
    }
    }
    </code>

    1.) rename the methods getSelectedRow/s so that the super ist used.
    2.) run the demo and u see that sorting and selecting any row returns the wrong values.
    3.) rename the methods from 1. back to the getSelectedRow/s and run the demo again.
    4.) sorting and selecting returns now the correct value.
    But u will see that the blue rendered row will change to a different row any time u sort the table.
    5.) Switching to ListSelectionModel.MULTIPLE_INTERVAL_SELECTION returns the correct values but the blue rows changes also

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    sorry... i am stupid...
    i dont know where to place do_it so that "table" can be resolved

  7. #7
    Ralphw is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    sorry... i am stupid...
    i dont know where to place do_it so that "table" can be resolved
    make table a class variable
    <code>
    public class TableSortDemo extends JPanel
    {
    ExtJTable table = new ExtJTable();

    </code>

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  9. #9
    Ralphw is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    i mean the "table" inside do_it method

    <code>

    public class TableSortDemo extends JPanel
    {
    ExtJTable table = new ExtJTable();
    public TableSortDemo()
    {
    super(new GridLayout(1,0));

    table.setModel(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 100));
    table.setFillsViewportHeight(true);
    table.setSortierung(true);
    table.setHeaderLabels(new MyTableModel().columnNames);
    table.setContents( new MyTableModel().data );
    table.setRowSelectionInterval(0,0);

    table.addMouseListener(new java.awt.event.MouseAdapter()
    {
    public void mouseClicked(MouseEvent e)
    {
    do_it();
    }
    });


    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.getViewport().add(table, null);
    //Add the scroll pane to this panel.
    add(scrollPane);
    }

    private void do_it()
    {
    String values = "";
    if (table.getSelectionModel().getSelectionMode()== ListSelectionModel.SINGLE_SELECTION )
    {
    int row = table.getSelectedRow();
    table.getModel().getValueAt(row, 0);
    javax.swing.JOptionPane.showMessageDialog(this,"Na me is "+ table.getModel().getValueAt(row, 0));
    }
    else
    {
    int[] rows = table.getSelectedRows();
    for (int i = 0; i < rows.length; i++)
    {
    values = values + " ; " + table.getModel().getValueAt(rows[i], 0);
    }
    javax.swing.JOptionPane.showMessageDialog(this,"Na mes are "+ values );
    }
    }
    </code>

Similar Threads

  1. Regarding the JTable
    By suresh.uvce in forum AWT / Swing
    Replies: 5
    Last Post: 02-11-2009, 06:46 AM
  2. Regarding JTable
    By adeeb in forum AWT / Swing
    Replies: 12
    Last Post: 06-19-2008, 07:39 AM
  3. Regarding JTable
    By adeeb in forum SWT / JFace
    Replies: 0
    Last Post: 06-18-2008, 06:13 PM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:57 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
  •