简体   繁体   English

使用ActionListener刷新JFrame

[英]Refresh JFrame with ActionListener

This has been asked many times already, but i really cant solve this problem. 这已经被问过很多次了,但是我真的不能解决这个问题。 I'm trying to refresh a table in a JFRame while it is running after i added a row so I dont have to restart it.I've tried the following method, but it still doesnt work. 我在添加行后尝试在JFRame中刷新表以使其运行,因此我不必重新启动它。我尝试了以下方法,但仍然无法正常工作。 The method is used in a ActionListener. 该方法在ActionListener中使用。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class GUI2 extends JFrame {

    private static JPanel contentPane;
    private static JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
            GUI2 frame = new GUI2();

    }

    /**
     * Create the frame.
     */
    public GUI2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        setContentPane(contentPane);

        table = new JTable();
        contentPane.add(table, BorderLayout.CENTER);

        JButton btnRefresh = new JButton("Refresh");
        contentPane.add(btnRefresh, BorderLayout.NORTH);
        btnRefresh.addActionListener(new Refresh());
    }
    public class Refresh implements ActionListener{
        public void actionPerformed(ActionEvent e){
            repaintGUI();
        }
    }
    public static void repaintGUI() {

            contentPane.repaint();
            contentPane.validate();
            table.repaint();
            table.validate();
    }
}

Something similar to that. 类似的东西。 The Button is pressed after i have changed something in the table. 我更改了表中的某些内容后,按下了按钮。

Your posted code doesn't work well as an example since the JTable is not visualized, but if you give the contentPane a BorderLayout, the JTable displays, and if refresh is pushed, then the GUI does in fact repaint, but as expected, it doesn't change its appearance since a simple repaint or revalidate won't change appearances if nothing else is done. 由于未可视化JTable,因此您发布的代码不能很好地作为示例,但是如果为contentPane提供BorderLayout,则会显示JTable,并且如果按下刷新,则GUI实际上会重新绘制,但正如预期的那样不会更改其外观,因为如果不执行其他任何操作,则简单的重新绘制或重新验证不会更改外观。

OK, here's where I need to guess since your question is still lacking key details (in the future, please ask it with an eye towards our point of view: people who have no idea what your current program does, looks like or what your problem is), but if the problem is that you're trying to reset a JTable's data, then the solution is to do just that -- reset your JTable's model by calling setRowCount(0) which will remove all data from the JTable, if it uses a DefaultTableModel. 好的,这是我需要猜测的地方,因为您的问题仍然缺少关键细节(将来,请以我们的观点进行询问:不知道您当前程序的工作方式,外观或问题的人是),但是如果问题是您试图重置JTable的数据,那么解决方案就是这样做-通过调用setRowCount(0)重置JTable的模型,如果这样做,它将从JTable中删除所有数据使用DefaultTableModel。 If it doesn't use the DefaultTableModel, then make sure to either give the JTable a new table model, or else call a method of your own table model that removes all data. 如果它不使用DefaultTableModel,则请确保为JTable提供一个新的表模型,或者调用您自己的表模型的方法来删除所有数据。

For instance: 例如:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class GUI3 extends JPanel {
   private static final String[] COLS = { "Foo", "Bar", "Baz" };
   private static final int ROWS = 5;
   private DefaultTableModel tableModel = new DefaultTableModel(COLS, ROWS);
   private JTable table = new JTable(tableModel);
   private ResetAction resetAction = new ResetAction("Reset", KeyEvent.VK_R);
   private RandomizeAction randomizeAxn = new RandomizeAction("Randomize",
         KeyEvent.VK_Z);

   public GUI3() {
      JPanel btnPanel = new JPanel();
      btnPanel.add(new JButton(resetAction));
      btnPanel.add(new JButton(randomizeAxn));

      setLayout(new BorderLayout());
      add(new JScrollPane(table));
      add(btnPanel, BorderLayout.PAGE_END);
   }

   private class ResetAction extends AbstractAction {
      public ResetAction(String name, int mnemonic) {
         super(name);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         tableModel.setRowCount(0);
      }
   }

   private class RandomizeAction extends AbstractAction {
      private Random random = new Random();

      public RandomizeAction(String name, int mnemonic) {
         super(name);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      public void actionPerformed(ActionEvent e) {
         tableModel.setRowCount(ROWS);
         int rows = ROWS;
         int columns = tableModel.getColumnCount();
         for (int row = 0; row < rows; row++) {
            for (int col = 0; col < columns; col++) {
               int data = random.nextInt(10);
               tableModel.setValueAt(data, row, col);
            }
         }
      };
   }

   private static void createAndShowGui() {
      GUI3 mainPanel = new GUI3();

      JFrame frame = new JFrame("GUI3");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM