Java 刷新按钮不可用';t刷新jtable 公共类天气框架扩展JFrame{ 私有JPanel内容窗格; 专用JTable表; HealthData health=新的HealthData(); 私有模型; String[]columnNames={“zipcode”、“县”、“市”、“州”、“年”、“月”、“年龄组”, “Numof”、“MonthlyMax”、“MonthlyMin”、“MonthlyNor”}; /** *启动应用程序。 */ 公共静态void main(字符串[]args){ invokeLater(新的Runnable(){ 公开募捐{ 试一试{ 风挡框=新风挡框(); frame.setVisible(true); }捕获(例外e){ e、 printStackTrace(); } } }); } /** *创建框架。 */ 公共天气架(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 立根(100100800300); contentPane=newjpanel(); contentPane.setBounds(100100750200); setContentPane(contentPane); contentPane.setLayout(null); JScrollPane scrollPane=新的JScrollPane(); 滚动窗格.立根(6,25,788,180); 添加(滚动窗格); populateTable(); 表=新JTable(型号); scrollPane.setViewportView(表); JButton btnisert=新JButton(“插入”); btnInsert.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ InsertFrame=新的InsertFrame(); frame.setVisible(true); } }); b插入立根(279、217、117、29); contentPane.add(btn插入); JButton btnDelete=新JButton(“删除”); btnDelete.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ DeleteFrame delete=新建DeleteFrame(); delete.setVisible(true); } }); BTN删除立根(412、217、117、29); contentPane.add(btnDelete); JButton btnSearch=新JButton(“搜索”); addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ SelectFrame搜索=新建SelectFrame(); search.setVisible(true); } }); b.研究挫折(530217117 29); contentPane.add(btnSearch); JLabel lblWeatherTable=新JLabel(“天气表”); lblWeatherTable.立根(149,6,107,16); contentPane.add(lblWeatherTable); JButton btnNext=新JButton(“更新”); addActionListener(新的ActionListener(){ 已执行的公共无效操作(操作事件e){ UpdateName update=新的UpdateName(); update.setVisible(true); } }); b下一个立根(150、217、117、29); contentPane.add(btnNext); JButton btnRefresh=新JButton(“刷新”); btnRefresh.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ populateTable(); } }); btnRefresh.挫折(29217117,29); contentPane.add(btnRefresh); JButton btnAnalyze=新JButton(“分析”); btnAnalyze.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ ShowAnalyze=newshowAnalyze(health.analyze()); analyze.setVisible(true); } }); btnAnalyze.挫折(662、217、117、29); contentPane.add(btnAnalyze); } @抑制警告(“串行”) public void populateTable(){ 模型=新的DefaultTableModel(){ @凌驾 公共布尔值可编辑(int行,int列){ //所有单元格均为假 返回false; } }; for(字符串名称:columnNames) model.addColumn(名称); ArrayList temp=新的ArrayList(); temp=health.showAllData(); 对于(int i=0;i

Java 刷新按钮不可用';t刷新jtable 公共类天气框架扩展JFrame{ 私有JPanel内容窗格; 专用JTable表; HealthData health=新的HealthData(); 私有模型; String[]columnNames={“zipcode”、“县”、“市”、“州”、“年”、“月”、“年龄组”, “Numof”、“MonthlyMax”、“MonthlyMin”、“MonthlyNor”}; /** *启动应用程序。 */ 公共静态void main(字符串[]args){ invokeLater(新的Runnable(){ 公开募捐{ 试一试{ 风挡框=新风挡框(); frame.setVisible(true); }捕获(例外e){ e、 printStackTrace(); } } }); } /** *创建框架。 */ 公共天气架(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 立根(100100800300); contentPane=newjpanel(); contentPane.setBounds(100100750200); setContentPane(contentPane); contentPane.setLayout(null); JScrollPane scrollPane=新的JScrollPane(); 滚动窗格.立根(6,25,788,180); 添加(滚动窗格); populateTable(); 表=新JTable(型号); scrollPane.setViewportView(表); JButton btnisert=新JButton(“插入”); btnInsert.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ InsertFrame=新的InsertFrame(); frame.setVisible(true); } }); b插入立根(279、217、117、29); contentPane.add(btn插入); JButton btnDelete=新JButton(“删除”); btnDelete.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ DeleteFrame delete=新建DeleteFrame(); delete.setVisible(true); } }); BTN删除立根(412、217、117、29); contentPane.add(btnDelete); JButton btnSearch=新JButton(“搜索”); addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ SelectFrame搜索=新建SelectFrame(); search.setVisible(true); } }); b.研究挫折(530217117 29); contentPane.add(btnSearch); JLabel lblWeatherTable=新JLabel(“天气表”); lblWeatherTable.立根(149,6,107,16); contentPane.add(lblWeatherTable); JButton btnNext=新JButton(“更新”); addActionListener(新的ActionListener(){ 已执行的公共无效操作(操作事件e){ UpdateName update=新的UpdateName(); update.setVisible(true); } }); b下一个立根(150、217、117、29); contentPane.add(btnNext); JButton btnRefresh=新JButton(“刷新”); btnRefresh.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ populateTable(); } }); btnRefresh.挫折(29217117,29); contentPane.add(btnRefresh); JButton btnAnalyze=新JButton(“分析”); btnAnalyze.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ ShowAnalyze=newshowAnalyze(health.analyze()); analyze.setVisible(true); } }); btnAnalyze.挫折(662、217、117、29); contentPane.add(btnAnalyze); } @抑制警告(“串行”) public void populateTable(){ 模型=新的DefaultTableModel(){ @凌驾 公共布尔值可编辑(int行,int列){ //所有单元格均为假 返回false; } }; for(字符串名称:columnNames) model.addColumn(名称); ArrayList temp=新的ArrayList(); temp=health.showAllData(); 对于(int i=0;i,java,swing,jframe,jtable,jbutton,Java,Swing,Jframe,Jtable,Jbutton,我试图使用一个刷新按钮来刷新jtable,当我单击该按钮时,它似乎正在加载,但在这之后,表上没有任何变化。我如何解决这个问题?在refresh按钮的ActionPerformed方法中,我调用了populateTable,这是一个将数据加载到表中的函数 JTable及其DefaultTableModel(定义为private JTable表;和private DefaultTableModel;)不知道在public void populateTable(){中(重新)创建了一个新的模型=新的

我试图使用一个刷新按钮来刷新jtable,当我单击该按钮时,它似乎正在加载,但在这之后,表上没有任何变化。我如何解决这个问题?在refresh按钮的ActionPerformed方法中,我调用了populateTable,这是一个将数据加载到表中的函数

  • JTable
    及其
    DefaultTableModel
    (定义为
    private JTable表;
    private DefaultTableModel;
    )不知道在
    public void populateTable(){
    中(重新)创建了一个新的
    模型=新的DefaultTableModel(){
你必须

  • 将新的
    DefaultTableModel
    添加到Swing GUI中已经可见的
    JTable
    s实例中

  • (更好的选择是)将新数据直接添加到
    私有DefaultTableModel;
    ,此模型指定用于

  • @Andrew Thompson在评论中对rest进行了很好的描述

      • JTable
        及其
        DefaultTableModel
        (定义为
        private JTable表;
        private DefaultTableModel;
        )不知道在
        public void populateTable(){
        中(重新)创建了一个新的
        模型=新的DefaultTableModel(){
      public class WeatherFrame extends JFrame {
      
          private JPanel contentPane;
          private JTable table;
          HealthData health = new HealthData();
      
          private DefaultTableModel model;
          String[] columnNames = {"zipcode", "county", "city", "state", "year", "month","ageGroup",
                                  "numOfVisits", "MonthlyMax", "MonthlyMin", "MonthlyNor"};
      
          /**
           * Launch the application.
           */
          public static void main(String[] args) {
              EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      try {
                          WeatherFrame frame = new WeatherFrame();
                          frame.setVisible(true);
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              });
          }
      
          /**
           * Create the frame.
           */
          public WeatherFrame() {
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setBounds(100, 100, 800, 300);
              contentPane = new JPanel();
              contentPane.setBounds(100, 100,750, 200);
              setContentPane(contentPane);
              contentPane.setLayout(null);
      
              JScrollPane scrollPane = new JScrollPane();
              scrollPane.setBounds(6, 25, 788, 180);
              contentPane.add(scrollPane);
      
              populateTable();
              table = new JTable(model);
      
              scrollPane.setViewportView(table);
      
              JButton btnInsert = new JButton("insert");
              btnInsert.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      InsertFrame frame = new InsertFrame();
                      frame.setVisible(true);
                  }
              });
              btnInsert.setBounds(279, 217, 117, 29);
              contentPane.add(btnInsert);
      
              JButton btnDelete = new JButton("delete");
              btnDelete.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      DeleteFrame delete = new DeleteFrame();
                      delete.setVisible(true);
                  }
              });
              btnDelete.setBounds(412, 217, 117, 29);
              contentPane.add(btnDelete);
      
              JButton btnSearch = new JButton("search");
              btnSearch.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      SelectFrame search = new SelectFrame();
                      search.setVisible(true);
                  }
              });
              btnSearch.setBounds(530, 217, 117, 29);
              contentPane.add(btnSearch);
      
              JLabel lblWeatherTable = new JLabel("Weather Table");
              lblWeatherTable.setBounds(149, 6, 107, 16);
              contentPane.add(lblWeatherTable);
      
              JButton btnNext = new JButton("update");
              btnNext.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      UpdateFrame update = new UpdateFrame();
                      update.setVisible(true);
                  }
              });
              btnNext.setBounds(150, 217, 117, 29);
              contentPane.add(btnNext);
      
              JButton btnRefresh = new JButton("refresh");
              btnRefresh.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      populateTable();
                  }
              });
              btnRefresh.setBounds(29, 217, 117, 29);
              contentPane.add(btnRefresh);
      
              JButton btnAnalyze = new JButton("Analyze");
              btnAnalyze.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      ShowAnalyze analyze = new ShowAnalyze(health.analyze());
                      analyze.setVisible(true);
                  }
              });
              btnAnalyze.setBounds(662, 217, 117, 29);
              contentPane.add(btnAnalyze);
          }
      
          @SuppressWarnings("serial")
          public void populateTable() {
      
              model = new DefaultTableModel(){
      
                  @Override
                  public boolean isCellEditable(int row, int column) {
                     //all cells false
                     return false;
                  }
              };
      
              for(String name: columnNames)
                  model.addColumn(name);
              ArrayList<Health> temp = new ArrayList<Health>();
              temp = health.showAllData();
              for(int i = 0; i< temp.size(); i++) {
                  Object[] data = {temp.get(i).getZipCode(), temp.get(i).getCounty(), temp.get(i).getCounty(), temp.get(i).getState(),temp.get(i).getYear(),
                                   temp.get(i).getMonth(), temp.get(i).getAgeGroup(), temp.get(i).getNumOfVisits(), temp.get(i).getMMax(), temp.get(i).getMMin(), temp.get(i).getMNor()};
                  model.addRow(data);
              }
              table.setModel(model);
          }
      }
      
      table = new JTable(model);
      populateTable();
      
      table.setModel(model);
      
      public void populateTable() {
          boolean firstTime = (model == null);
      
          if (firstTime) {
              initializeModel();
          } else {
              model.getDataVector().clear();
          }
      
          for (Health item : health.showAllData()) {
              model.addRow(new Vector<>(Arrays.asList(
                      item.getZipCode(), item.getCounty(), item.getState(), item.getYear(),
                      item.getMonth(), item.getAgeGroup(), item.getNumOfVisits(),
                      item.getMMax(), item.getMMin(), item.getMNor()
              )));
          }
      
          if (firstTime && table != null) {
              table.setModel(model);
          }
      }
      
      private void initializeModel() {
          model = new DefaultTableModel() {
              @Override
              public boolean isCellEditable(int row, int column) {
                  //all cells false
                  return false;
              }
          };
      
          for (String name : columnNames)
              model.addColumn(name);
      }