简体   繁体   English

JTable具有2个不同的数据

[英]JTable with 2 different data

Im using a JTable , loading on it a different data depending on the button pressed. 我正在使用JTable,根据所按下的按钮在其上加载不同的数据。

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... 基本上,假设每次要切换数据集时,都在调用此方法,则每次都创建一个新的JTableJScrollPane ,然后将其添加到UI中。

What about the previous JTable ? 那以前的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. 看起来您正在使用null布局。 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. 基本上是“看起来”正在发生的事情,是您只是将JScrollPane彼此堆叠在一起,这部分解释了图形故障,因为组件实际上是在相同的z深度处添加的(基本上是),并且彼此竞争,然后对其进行更新。

Two simple answers... 两个简单的答案...

  1. Don't use null layouts. 不要使用null布局。 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 使用Swing围绕其设计的布局管理API
  2. Update the JTable s model instead of creating a new JTable / JScrollPane each time 更新JTable的模型,而不是每次都创建一个新的JTable / JScrollPane

See How to use tables and Laying Out Components Within a Container 请参阅如何 在容器中 使用表布置组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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