Question

I'm trying to refresh my table but it won't refresh. I tried using the fireTableDataChanged() as well as creating a new model but my Table will not budge. I can get the initial values in but I can't figure out how to refresh it. HS = new Vector(Arrays.asList(finalHSArr)); SF = new Vector(Arrays.asList(finalFlagsArr)); I have two separate JTables that I am trying to refresh and these are the new values above. I've been trying for 6 hours now.

public class TemplateGui extends JFrame  {
        private final ButtonGroup buttonGroup = new ButtonGroup();
        private JTextField textField;
        private static String [] sortedRoles_Flags,finalFlagsArr,finalHSArr;
        private static String finalFlags="",finalHS="",columnToConvert="Result";
        private Vector<String> SF,HS,column;
        private JTable hotelSecurityTable,securityFlagsTable;
        private DefaultTableModel hsTableModel,sfTableModel;


        public TemplateGui(){

            super("Galaxy Template Generator V1.0");
            //column name
            column = new Vector(Arrays.asList(columnToConvert));

            getContentPane().setForeground(new Color(0, 0, 0));
            getContentPane().setLayout(null);
            getContentPane().setBackground(new Color(51, 51, 51));

            //radio buttons
            JRadioButton rdbtnNewRadioButton = new JRadioButton("Central User ");
            rdbtnNewRadioButton.setFont(new Font("Tahoma", Font.BOLD, 11));
            rdbtnNewRadioButton.setBackground(Color.WHITE);
            buttonGroup.add(rdbtnNewRadioButton);
            rdbtnNewRadioButton.setBounds(222, 75, 127, 36);
            getContentPane().add(rdbtnNewRadioButton);

            final JRadioButton rdbtnPropertyUser = new JRadioButton("Property User");
            rdbtnPropertyUser.setFont(new Font("Tahoma", Font.BOLD, 11));
            rdbtnPropertyUser.setBackground(Color.WHITE);
            buttonGroup.add(rdbtnPropertyUser);
            rdbtnPropertyUser.setBounds(222, 38, 127, 34);
            getContentPane().add(rdbtnPropertyUser);

            textField = new JTextField();
            textField.setFont(new Font("Tahoma", Font.BOLD, 18));
            textField.setBounds(10, 35, 53, 34);
            getContentPane().add(textField);
            textField.setColumns(10);

            JLabel lblHotelSecurity = new JLabel("Hotel Security (H S)");
            lblHotelSecurity.setHorizontalAlignment(SwingConstants.CENTER);
            lblHotelSecurity.setFont(new Font("Tahoma", Font.BOLD, 13));
            lblHotelSecurity.setBounds(10, 144, 189, 23);
            lblHotelSecurity.setBackground(new Color(204, 204, 204));
            lblHotelSecurity.setOpaque(true);
            getContentPane().add(lblHotelSecurity);

            JLabel label = new JLabel("Security Flags (S F)");
            label.setHorizontalAlignment(SwingConstants.CENTER);
            label.setFont(new Font("Tahoma", Font.BOLD, 13));
            label.setBounds(222, 144, 372, 23);
            label.setBackground(new Color(204, 204, 204));
            label.setOpaque(true);
            getContentPane().add(label);

            JLabel lblEnterTemplateCode = new JLabel("ENTER TEMPLATE CODE");
            lblEnterTemplateCode.setForeground(new Color(255, 255, 255));
            lblEnterTemplateCode.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblEnterTemplateCode.setBounds(10, 9, 175, 23);
            getContentPane().add(lblEnterTemplateCode);

            JLabel lblSelectUserRole = new JLabel("SELECT USER ROLE LEVEL");
            lblSelectUserRole.setForeground(new Color(255, 255, 255));
            lblSelectUserRole.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblSelectUserRole.setBounds(222, 13, 195, 14);
            getContentPane().add(lblSelectUserRole);

            //Submit button action
            Button button = new Button("Generate Template");
            button.setFont(new Font("Dialog", Font.BOLD, 12));
            button.setBackground(new Color(102, 255, 102));
            button.setForeground(Color.BLACK);
            button.setBounds(467, 83, 127, 41);
            getContentPane().add(button);


            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {

                    Query excell = new Query();
                    //get template text
                    String template = textField.getText().toUpperCase();
                    System.out.println(template);

                    if(rdbtnPropertyUser.isSelected()){
                        try {
                        //property user was selected
                            excell.runProcess(1);
                            System.out.println("you selected Property user");
                        } catch (IOException e) {

                            e.printStackTrace();
                        }
                    }
                    else{
                        try {
                        //Central User was selected 
                            excell.runProcess(2);
                            System.out.println("you selected central user");
                        } catch (IOException e) {

                            e.printStackTrace();
                        }
                    }

                    System.out.println("NOW WERE HERE");
                    //get static variables from Excel Query 
                    for(int i = 0; i< Query.sortedGF.length; i++)
                    {
                        if(Query.sortedGF[i].contains(template)){
                             sortedRoles_Flags  =Query.sortedGF[i].split(" ");
                             System.out.println("THIS RAN"+" :"+i); 
                             break;
                        }

                    }
                    System.out.println("NOW WERE HERE 103 " +Query.securityFlags.length);
                    //add data to table
                    int j=0;
                    int sizeOfFlags = Query.securityFlags.length;


                    //Add HS to FinalHS Variable only if Yes
                    for(int i=0;i< sortedRoles_Flags.length-sizeOfFlags;i++)
                    {
                        if(sortedRoles_Flags[i].matches("Y|y|Y\\?|\\?Y|y\\?|\\?y"))
                            {
                                System.out.println("Hotel security:"+" "+sortedRoles_Flags[i]+" HS Added: "+Query.hotelSecurity[i]);
                                finalHS += Query.hotelSecurity[i]+" ";
                                System.out.println("Hotel security:"+" "+finalHS);
                            }
                    }

                    //add Security Flags to Final Flags
                    for(int i=(sortedRoles_Flags.length-sizeOfFlags);i< sortedRoles_Flags.length;i++)
                    {                   
                            finalFlags += Query.securityFlags[j]+": "+ sortedRoles_Flags[i]+" + ";
                            j++;
                    }

                    //Leave open just incase they would prefer a text file for template in which case we just write it
                    System.out.println(finalFlags);

                    System.out.println(finalHS);

                    //Convert to String Arrays in order to add to our JTable
                    finalFlagsArr= finalFlags.split("\\+");
                    finalHSArr = finalHS.split(" ");
                    //convert to vectors
                    HS = new Vector<String>(Arrays.asList(finalHSArr));
                    SF = new Vector<String>(Arrays.asList(finalFlagsArr));
                    System.out.print(HS);

                    hsTableModel.fireTableDataChanged();
                    sfTableModel.fireTableDataChanged();

                }
            });
            //scroll panes for flags
            JScrollPane scrollPaneHS = new JScrollPane();
            scrollPaneHS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPaneHS.setBounds(10, 170, 189, 185);
            getContentPane().add(scrollPaneHS);

            JScrollPane scrollPaneSF = new JScrollPane();
            scrollPaneSF.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPaneSF.setBounds(222, 170, 372, 187);
            getContentPane().add(scrollPaneSF);

            //tables for updates
            hsTableModel = new DefaultTableModel(HS,column);
            hotelSecurityTable = new JTable(hsTableModel);
            scrollPaneHS.setViewportView(hotelSecurityTable);

            sfTableModel = new DefaultTableModel(SF,column);
            securityFlagsTable = new JTable(sfTableModel);
            scrollPaneSF.setViewportView(securityFlagsTable);


        }


    }
Was it helpful?

Solution

When you do

HS = new Vector<String>(Arrays.asList(finalHSArr));
SF = new Vector<String>(Arrays.asList(finalFlagsArr));

You change what HS and SF where point at, however, it does not change what, internally, the TableModel is pointing at.

You could use DefaultTableModel#setDataVector, but i might simpler to simply create new TableModels and set them to the JTables instead.

hsTableModel = new DefaultTableModel(HS, column);
hotelSecurityTable.setModel(hsTableModel);

sfTableModel = new DefaultTableModel(HS, column);
securityFlagsTable.setModel(sfTableModel);

As it's pretty much the same thing...

Updated

DefaultTableModel is expecting a Vector of Vectors, but you're supplying a Vector of Strings.

Try something like...

hsTableModel.setRowCount(0);
for (String row : HS) {
    hsTableModel.addRow(new Object[]{row});
}

Instead...

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