从Combobox中选择时,JTable不会刷新

时间:2014-03-19 16:03:10

标签: java swing combobox jtable defaulttablemodel

每次从组合中选择内容时,scrollPane表都不会刷新。最初它有数据,但在我选择了某些内容后,数据被成功删除,但新数据不会填入

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);
}

这是我的Combobox在选择某些东西后即将前进的地方,我已尝试过重新制作&amp; amp;重新验证

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();
}

1 个答案:

答案 0 :(得分:1)

scrollPane = new JScrollPane(table);

上面的代码行创建了一个新的scrollPane,但是没有将scrollPane添加到框架中。

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

相反,您只需使用以下命令更改JTable的模型:

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

所以基本上你的方法就变成了一行代码。

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

相关问题