Question

The scrollPane table isn't refreshing for every time I selected something from the combo. Initially it has data but after I selected something, the data is removed successfully but new data isn't populating in

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

This is where my Combobox's is heading as soon as something is selected and I've tried repaint & revalidate

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();
}
Was it helpful?

Solution

scrollPane = new JScrollPane(table);

The above line of code creates a new scrollPane, but doesn't add the scrollPane to the frame.

However, there is no need to even create a new JTable or a new JScrollPane. Get rid of all the code.

Instead you just change the model of your JTable by using:

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

So basically your method becomes one line of code.

Also, use proper method names. Method names should NOT start with an upper case character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top