2014-03-19 47 views
0

每次从组合中选择某个对象时,scrollPane表都不刷新。最初,它有数据,但我选择了内容之后,该数据被成功删除,但新的数据没有从Combobox中选择JTable时不刷新

public void ConsultFrame(String id, String name, String ic){ 
    GenerateMed("dp-000"); // to begin the scrollpane filled with Fever's medicine 
    JButton proc = new JButton("Proceed"); 
    JButton addmed = new JButton(">>"); 

    selected = new JTable(data, columnNames){ 
     @Override 
     public boolean isCellEditable(int row,int column){ 
      switch(column){    
       case 0: 
        return false; 
       case 1: 
        return false; 
      default: return true; 
      } 
    }}; 
    selectedPane = new JScrollPane(selected); 

    //Dispensary's category combobox related 
    disp = dbDisp.getDispensary(); 
    final JComboBox cBox = new JComboBox(); 
    for(int count=0; count<disp.size(); count++) 
     cBox.addItem(disp.get(count).getDSP_desc()); 
    cBox.addItemListener(new ItemListener() { 
     @Override 
     public void itemStateChanged(ItemEvent event) { 
      for(int count=0; count<disp.size(); count++){ 
       if(cBox.getSelectedItem().equals(disp.get(count).getDSP_desc())){ 
        System.out.println(disp.get(count).getDSP_ID()); 
        GenerateMed(disp.get(count).getDSP_ID()); 
        break; 
       } 
      } 
     } 
    }); 

    JTextArea tArea = new JTextArea(5, 30); 
    JScrollPane desc = new JScrollPane(tArea); 
    tArea.setLineWrap(true); 
    desc.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

    JPanel info = new JPanel(); 
     info.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     info.add(new JLabel("<html>Patient's ID : " + id + "<br>Patient's Name: " + name + "<br>Patient's IC : " + ic + "<br><br>Medical Description : </html>")); 
     info.add(desc); 

    JPanel medSelect = new JPanel(); 
     medSelect.setLayout(new GridLayout(1,2)); 
     medSelect.add(scrollPane); 
     medSelect.add(selectedPane); 

    JPanel medic = new JPanel(); 
     medic.setLayout(new BorderLayout()); 
     medic.add(cBox, BorderLayout.NORTH); 
     medic.add(medSelect, BorderLayout.CENTER); 
     medic.add(proc, BorderLayout.SOUTH); 

    JPanel all = new JPanel(); 
     String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(Calendar.getInstance().getTime()); 
     title = BorderFactory.createTitledBorder(timeStamp); 
     title.setTitleJustification(TitledBorder.RIGHT); 
     all.setBorder(title); 
     all.setLayout(new GridLayout(2,1)); 
     all.add(info); 
     all.add(medic); 

    JFrame consult = new JFrame(); 
     consult.setTitle(name + "'s consultation"); 
     consult.setResizable(false); 
     consult.setVisible(true); 
     consult.setSize(500, 460); 
     consult.setLocationRelativeTo(null); 

     consult.add(all); 
} 

填充这是因为什么被选中,我已经试过重绘&我的组合框的是尽快走向重新验证

public void GenerateMed(String dps_id){ 
    if (tModel != null) { 
     for (int i = tModel.getRowCount() - 1; i > -1; i--) 
      tModel.removeRow(i); 
    } 

    tModel = dbMed.getDPSMedicine(dps_id); 
    tModel.fireTableDataChanged(); 

    table = new JTable(tModel){ 
     @Override 
     public boolean isCellEditable(int row,int column){    
        return false; 
    }}; 
    table.setShowGrid(false); 
    table.setShowHorizontalLines(false); 
    table.setShowVerticalLines(false); 

    //Table customization 
    table.getTableHeader().setReorderingAllowed(false); 
    table.setRowSelectionAllowed(true); 
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    table.changeSelection(0, 0, false, false); 

    scrollPane = new JScrollPane(table); 
    scrollPane.repaint(); 
    scrollPane.revalidate(); 
} 
+0

scrollPane没有添加到任何容器中,它不是一个窗口,您必须将其添加到框架中,也不需要创建新的JScrollPane或新的JTable。 – nachokk

回答

1
scrollPane = new JScrollPane(table); 

上面的代码行创建一个新的滚动,但滚动窗格不添加到框架。

但是,甚至不需要创建新的JTable或新的JScrollPane。摆脱所有的代码。

相反,你只需使用改变你的JTable的模型:

table.setModel(dbMed.getDPSMedicine(dps_id)); 

所以基本上你的方法变得一行代码。

另外,请使用正确的方法名称。方法名称不应以大写字符开头。

+0

它像一个魅力,但两件事情: 我。它不会显示表 的列名ii。如何添加覆盖到'表'?喜欢 。 。 'code' table = new JTable(tModel){ @Override public boolean isCellEditable(int row,int column){ return false; }}; – Leon

+0

Aaa ..解决了我。 :D我使用[link](http://blog.danieldee.com/2009/07/showing-jtable-header-without-using.html)方法 – Leon

+0

@ user3083413,为什么要使用该解决方案?您只需在JScrollPane中显示表格即可。既然你说的代码最初工作,那么你所需要做的就是重置模型,列标题将自动更新。 – camickr