Results 1 to 4 of 4
  1. #1
    stelzergil is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Thumbs down Changing JTableModel does not change JScrollpane size

    I basically want to let the user filter data within a table and change row colors accordingly.

    The data for my table is held within the table model.
    Table rows should be colored white or gray. Looking only at a certain column, if the cell's value of the current row is incremented by 1 compared to the previous one the color should switch. I seem to have accomplished this by overriding the prepareRenderer method.
    However, the user can decide to filter rows in the table. I have also managed to use a filter for the table that works. However, after filtering, rows color should be rerendered since some rows could be omitted and the policy of one row's color depending on the previous still holds.
    This does not happen. Rows retain the color previously assigned to them before filtering.

    I read on the net that cell rendering happens only once, when the table is first painted. Therefore, I a filter does NOT invoke the table to rerender it is understandable that rows would retain their color.

    Therefore, I tried changing the data itself in the data model and use fireTableDataChanged() so that the entire table would get rerendered.
    Well that doesn't solve the problem.
    Although the table gets updated, the jscrollpane within which it is placed gets partially updated. If the table gets shortened the top of the table gets updated but the scroll bar does not and when I scroll down I see lines that are not supposed to be there and the table freezes.
    I tried using revalidate() and repaint() for the scrollpane as well for its parent but nothing works.

    table = new JTable(tableModel);
    scrollPane = new JScrollPane();
    scrollPane.setViewportView(getJTable());

    Help !!!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    > I read on the net that cell rendering happens only once, when the table is first painted.

    Not true. Cell are constantly being rerendered. For example when you click on a different row, you need to rerender all the cells in the previous row without the highlight and then rerender all the cell in the current row with the rendering.

    The easiest way to force repainting is to just use table.repaint(). If this doesn't work then I would guess your custom code in prepareRenderer() is wrong. Maybe you are still referencing the data in the model and not the data in the table.

    Also if you want to change the data in the table, then you can just use table.setModel(...). There is no need to recreate the table and/or scrollpane. Those 3 lines of code you posted don't make any sense.

    If you need more help post your SSCCE.

  3. #3
    stelzergil is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    table = new JTable(tableModel)
    //first attempt to color rows with white / gray if the reference residue number is incremented by more than 1
    {
    private static final long serialVersionUID = 1L;

    public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int colIndex)
    {
    Component comp = super.prepareRenderer(renderer, rowIndex, colIndex);
    //alternating gray and white rows for testing
    // if (rowIndex % 6 == 0 && !isCellSelected(rowIndex, colIndex))
    // comp.setBackground(new Color(220,220,220));//Color.lightGray);
    // else
    // comp.setBackground(Color.white);
    //

    if(rowIndex > 0)
    {
    int prevResNum = (Integer) table.getModel().getValueAt(rowIndex -1 , referenceResidNumColIndx);
    int currResNum = (Integer) table.getModel().getValueAt(rowIndex, referenceResidNumColIndx);
    if((currResNum - prevResNum ) > 1 && colIndex == 0)
    {
    if(currentColor == Color.WHITE)
    currentColor = Color.LIGHT_GRAY;
    else
    currentColor = Color.WHITE;
    }
    }
    Color temp = currentColor;
    this.setBackground(currentColor);
    return comp;
    }
    }
    ;

    Is there something obviously wrong with this code?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Is there something obviously wrong with this code?
    1) All the code is left aligned.

    2) The code isn't compileable.

Similar Threads

  1. Changing size of Array
    By ravian in forum New To Java
    Replies: 3
    Last Post: 06-05-2012, 08:17 PM
  2. Changing default Applet Viewer Size?
    By dsym@comcast.net in forum Java Applets
    Replies: 6
    Last Post: 08-27-2010, 06:42 PM
  3. Changing the stack size
    By saif.hakim in forum New To Java
    Replies: 1
    Last Post: 04-26-2009, 06:58 AM
  4. changing font size
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 12-25-2008, 07:48 AM
  5. Listener for JFrame size change
    By Thez in forum AWT / Swing
    Replies: 10
    Last Post: 02-14-2008, 03:10 PM

Tags for this Thread

Posting Permissions

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