简体   繁体   中英

JTable with 2 different data

Im using a JTable , loading on it a different data depending on the button pressed.

The problem is : when one of the data is loaded, if i try to load the other one, and pass ther mouse over the header or a cell, it updates the header/cell with the data from the first input, if there is data on the header/cell selected.

Any ideas on how to solve it? That's the code im using.

private static void setCompromissosTable(Object[][] data, Object[] header){
    try{
        compromissosTable.removeAll();
    } catch(Exception e){
        e.printStackTrace();
    }

    compromissosTable = new JTable(data, header);
    compromissosTable.setRowSelectionAllowed(true);


    // Make the entire row selectable, but not editable
    int columnMax = compromissosTable.getColumnCount();
    for(int column = 0; column < columnMax; column++){
        Class<?> col_class = compromissosTable.getColumnClass(column);
        compromissosTable.setDefaultEditor(col_class, null);
    }

    scrollPane = new JScrollPane(compromissosTable);
    pane.add(scrollPane);

    scrollPane.setBounds(btnAddCompromisso.getX(), 
            btnAddCompromisso.getHeight() + btnAddCompromisso.getY()  + 5
            , frame1.getWidth() - 20
            , frame1.getHeight() - 20);

    compromissosTable.revalidate();
    compromissosTable.repaint();

    compromissosTable.addMouseListener(new MouseAdapter() {}
     //Change mouse behavior.
    );


}

This is suspicious...

compromissosTable = new JTable(data, header);
//...
scrollPane = new JScrollPane(compromissosTable);
pane.add(scrollPane);

Basically, assuming that each time you want to switch data sets, you are calling this method, you are creating a new JTable and JScrollPane each time and then are adding it onto the UI...

What about the previous JTable ?

Next is this...

scrollPane.setBounds(btnAddCompromisso.getX(), 
        btnAddCompromisso.getHeight() + btnAddCompromisso.getY()  + 5
        , frame1.getWidth() - 20
        , frame1.getHeight() - 20);

This looks like you're using a null layout. Basically what it "looks" like is happening, is you're just stacking the JScrollPane s ontop of each other, which would explain, in part, the graphics glitches, as the components are actually been added at the same z-deepthness (essentially) and are competing with each other then they are updated.

Two simple answers...

  1. Don't use null layouts. Sure they "seem" like a good idea, but they have a tendency to turn around and bite you in strange and wonderful ways which are hard to diagnose and fix. Use the layout management API which Swing was designed around
  2. Update the JTable s model instead of creating a new JTable / JScrollPane each time

See How to use tables and Laying Out Components Within a Container

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM