使用Vectors字符串刷新JTable

时间:2014-05-13 03:39:35

标签: java swing vector jtable defaulttablemodel

我正在尝试刷新我的桌子,但它不会刷新。我尝试使用fireTableDataChanged()以及创建一个新模型但我的表不会让步。我可以获得初始值,但我无法弄清楚如何刷新它。 HS = new Vector(Arrays.asList(finalHSArr)); SF = new Vector(Arrays.asList(finalFlagsArr)); 我有两个单独的JTable,我正在尝试刷新,这些是上面的新值。我现在已经尝试了6个小时。

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


        }


    }

1 个答案:

答案 0 :(得分:4)

当你这样做时

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

您可以更改HSSF所指的位置,但是,它不会在内部改变TableModel所指向的内容。

您可以使用DefaultTableModel#setDataVector,但我可以更简单地创建新的TableModel并将其设置为JTable

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

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

因为它几乎是一样的......

<强>更新

DefaultTableModel预计Vector Vector,但您提供的Vector String

尝试类似......

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

,而不是...