简体   繁体   English

如何重绘JPanel

[英]How to repaint JPanel

I am having a problem with JPanel repaint after update. 更新后,JPanel重涂出现问题。 So, I have a function which adds a few (user selection) JTable to JPanel. 因此,我有一个向JPanel添加一些(用户选择)JTable的功能。 On first use, everything is ok, all components are ok. 首次使用时,一切正常,所有组件均正常。 But when I run a second time the same function, all components disappear. 但是,当我第二次运行相同的功能时,所有组件都消失了。 And I do not understand why. 而且我不明白为什么。 When I check list of components in JPanel that is ok but I do not see not even one. 当我检查JPanel中的组件列表是可以的,但我什至看不到一个。

private void refreshTables(){
tablePanel.removeAll();

List resultListUzytkownicy=null;
    try {
        resultListUzytkownicy = DAO.GlobalDOA.getQuery("from Uzytkownik u where u.widokWPlanownaiu=1");}
    catch (IOException ex) {Logger.getLogger(PlanowanieFrame.class.getName()).log(Level.SEVERE, null, ex);}

int tabelaNr = 0;

Vector<String> tableHeaders = new Vector<String>();   
tableHeaders.add("ID:"); 
tableHeaders.add("Kategoria:");
tableHeaders.add("Podkategoria:");
tableHeaders.add("Data przyjęcia:");
tableHeaders.add("Przyjmujący:");
tableHeaders.add("Serwisujący:");
tableHeaders.add("Data zamknięcia:");
tableHeaders.add("Zamykający:");
tableHeaders.add("Klient:");
tableHeaders.add("Status:");
tableHeaders.add("Opis:");
tableHeaders.add("Termin:");
tableHeaders.add("Priorytet:");
tableHeaders.add("Podzlecenia:"); 

for(Object o : resultListUzytkownicy){
    Uzytkownik user = (Uzytkownik)o;

    List resultList = null;
    try {
        resultList = DAO.GlobalDOA.getQuery("from Zlecenie z where z.podzlecenie=0 and z.serwisujacy="+user.getId()+" order by id desc");}
    catch (IOException ex) {Logger.getLogger(PlanowanieFrame.class.getName()).log(Level.SEVERE, null, ex);}

    Vector tableData = new Vector();
    for(Object b : resultList) {
        Zlecenie actor = (Zlecenie)b;
        Vector<Object> oneRow = new Vector<Object>();
        oneRow.add(actor.getId());
        if(actor.getKategoria()!=null){oneRow.add(actor.getKategoria().getTemat());}
           else{oneRow.add(null);};
        if(actor.getPodkategoria()!=null){oneRow.add(actor.getPodkategoria().getTemat());}
           else{oneRow.add(null);};
        oneRow.add(DateRender.longDate(actor.getDataPrzyjecia()));
        oneRow.add(actor.getPrzyjmujacy().getImie()+" "+actor.getPrzyjmujacy().getNazwisko());
        if(actor.getSerwisujacy()!=null){oneRow.add(actor.getSerwisujacy().getImie()+" "+actor.getSerwisujacy().getNazwisko());}
           else{oneRow.add(null);};
        if(actor.getDataZamkniecia()!=null){oneRow.add(DateRender.longDate(actor.getDataZamkniecia()));}
           else{oneRow.add(null);}; 
        if(actor.getZamykajacy()!=null){oneRow.add(actor.getZamykajacy().getImie()+" "+actor.getZamykajacy().getNazwisko());}
           else{oneRow.add(null);};       
        if(actor.getKlient()!=null){oneRow.add(actor.getKlient().getNazwa());}   
           else{oneRow.add(null);};
        oneRow.add(DAO.StatusDAO.getStatusById(actor.getStatus()).getOpis());
        oneRow.add(actor.getOpis());
        oneRow.add(actor.getTermin());
        oneRow.add(actor.getPriorytet());
        oneRow.add(actor.getPodzlecenia());
        tableData.add(oneRow);
        }

    ZleceniaModel model = new ZleceniaModel(tableHeaders,tableData);
    JTable table = new JTable(model);              
    JScrollPane scrollPane = new JScrollPane(table);                  
    tableResizer(tablePanel, scrollPane, tabelaNr);
    tablePanel.add(scrollPane);
    tablePanel.revalidate();
    tablePanel.repaint();
    System.out.println(tablePanel.getComponents());
    tabelaNr++;
 }
}

 public static void tableResizer(JPanel panel, JScrollPane scrollPane, int tableaNr){

ComponentListener componentListener = new ComponentListener (){
        @Override
        public void componentResized(ComponentEvent e) {
                int panelWidth = panel.getWidth();
                scrollPane.setPreferredSize(new Dimension(panelWidth, 100));                   
                scrollPane.setBounds(0, tableaNr*100, panelWidth, 100);
                }
        @Override
        public void componentMoved(ComponentEvent e) {}
        @Override
        public void componentShown(ComponentEvent e) {    }
        @Override
        public void componentHidden(ComponentEvent e) {  }
    };
panel.addComponentListener(componentListener);  
 }

Don't remove the components from the panel. 不要从面板上卸下组件。

All you need to do is replace the TableModel of the table and the table will update itself. 您需要做的就是替换表的TableModel,表将自动更新。

So when you create the frame create the table and scroll pane in the constructor and add the scroll pane to the frame. 因此,当您创建框架时,请在构造函数中创建表和滚动窗格,然后将滚动窗格添加到框架中。

JTable table = new JTable();
JScrollPane = new JScrollPane(table);
panel.add( scrollPane );

Then in your refresh logic the code just becomes: 然后在您的刷新逻辑中,代码变为:

ZleceniaModel model = new ZleceniaModel(tableHeaders,tableData);
table.setModel( model );
//JTable table = new JTable(model);              
//JScrollPane scrollPane = new JScrollPane(table);                  
//tableResizer(tablePanel, scrollPane, tabelaNr);
//tablePanel.add(scrollPane);
//tablePanel.revalidate();
//tablePanel.repaint();

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

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