只有当我调用GUI()的新实例时,JTable才会刷新;

时间:2016-12-08 17:35:43

标签: java swing user-interface jtable refresh

我有简单的JTable应用程序,只有当我使用新的GUI()'在我的buttonListener方法中。否则不起作用。我已尝试在此代码中的每个位置使用每个FireTable ...方法。还试过mainFrame / mainPanel.repaint(); table.revalidate();

主要课程

public class AddressBook {

public static void main(String[] args) {
    try 
    { 
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");  
        new GUI();
    }   
    catch(Exception e){ 
        System.out.println("GUI loading error..");
    }
}
}

GUI()类

    public GUI() {

    /** Main Frame, Panels, Borders */
    mainFrame = new JFrame("Address Book");
    mainPanel = new JPanel(new GridBagLayout());
    mainPanel.setBorder(BorderFactory.createEmptyBorder(borderOffset, borderOffset, borderOffset, borderOffset));   // up, left, down, right
    GridBagConstraints c = new GridBagConstraints();

    tablePanel = new JPanel();
    tablePanel.setLayout((new BoxLayout(tablePanel,BoxLayout.PAGE_AXIS)));

    buttonsPanel = new JPanel();
    buttonsPanel.setLayout((new BoxLayout(buttonsPanel,BoxLayout.PAGE_AXIS)));

    /** Head of table */
    model = new MyTableModel();
    table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 50));

    /** Heart of table */
    scrollPane = new JScrollPane(table);

    /** Title of app */
    titleLabel = new JLabel("Address Book");
    titleLabel.setFont(new Font(null, Font.PLAIN, 15));
    titleLabel.setForeground(Color.GRAY);

    /** Buttons */
    final int BUTTONS_WIDTH = 130;
    final int BUTTONS_HEIGHT = 40;
    addButton = new JButton("Add new person");
    addButton.setPreferredSize(new Dimension(BUTTONS_WIDTH, BUTTONS_HEIGHT));
    addButton.addActionListener(new addButtonListener());
    editButton = new JButton("Edit person");
    editButton.setPreferredSize(new Dimension(BUTTONS_WIDTH, BUTTONS_HEIGHT));
    editButton.addActionListener(new editButtonListener());
    deleteButton = new JButton("Delete person");
    deleteButton.setPreferredSize(new Dimension(BUTTONS_WIDTH, BUTTONS_HEIGHT));

    /** Separator */
    separatorH = new JSeparator(JSeparator.HORIZONTAL);
    separatorH.setPreferredSize(new Dimension(2,2));

    tablePanel.add(table.getTableHeader());
    tablePanel.add(scrollPane);

    buttonsPanel.add(addButton);
    buttonsPanel.add(Box.createRigidArea(new Dimension(0, 3)));
    buttonsPanel.add(editButton);
    buttonsPanel.add(Box.createRigidArea(new Dimension(0, 3)));
    buttonsPanel.add(deleteButton);

    /** Relocate compoments */
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(0,2,2,0);
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    mainPanel.add(titleLabel, c);

    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(0,0,0,0);
    c.fill = GridBagConstraints.HORIZONTAL;
    mainPanel.add(separatorH, c);

    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(10,0,0,0);
    mainPanel.add(tablePanel, c);

    c.gridx = 1;
    c.gridy = 2;
    c.insets = new Insets(9,10,0,0);
    mainPanel.add(buttonsPanel, c);

    /** Activate Main Frame and its properties */
    mainFrame.add(mainPanel);
    mainFrame.setBounds(200, 100, 0, 0);
    mainFrame.setResizable(true);
    mainFrame.pack();
    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public class addButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent a) {
        ((MyTableModel)model).setValueAt("cos", "test", "email", 123124);
        new GUI();
    }
}
}

MyTableModel类

class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;

static private ArrayList<Person> personList = new ArrayList<Person>();
Object[][] persons = new Object[personList.size()][4];

int row;
int col;
static String name;
static String surname;
String email;
static int telephone;
private String[] columnNames = {"First Name",
                                "Last Name",
                                "E-mail",
                                "Telephone"};

@Override
public int getColumnCount() {
    return columnNames.length;
}

@Override
public String getColumnName(int col) {
    return columnNames[col];
}

@Override
public int getRowCount() {
    return persons.length;
}

@Override
public Object getValueAt(int row, int col) {
    for(int i=0; i<personList.size(); i++) {
        persons [i][0] = personList.get(i).name.toString(); 
        persons [i][1] = personList.get(i).surname.toString(); 
        persons [i][2] = personList.get(i).mail.toString();
        persons [i][3] = personList.get(i).telephone; 
    }
    return persons[row][col];
}

public void fire(Object value, int row, int col) {
    persons[row][col] = value;
}

public void setValueAt(String n, String s, String e, int t){
    name = n;
    surname = s;
    email = e;
    telephone = t;
    Person person = new Person(n, s, e, t);
    personList.add(person);
    }
}

人类

public class Person {
String name;
String surname;
String mail;
int telephone;

public Person(String n, String s, String m, int t){
    name = n;
    surname = s;
    mail = m;
    telephone = t;
}
}

1 个答案:

答案 0 :(得分:3)

你的桌子模型坏了:

  • 您使用ArrayList和2D数组都有奇怪的重复数据。完全摆脱2D Object数组,只使用ArrayList,一个是静态的字段。
  • 您需要为此类提供addRow(Person p)方法,并在该方法中向ArrayList添加另一个Person,并调用fireTableRowsInserted(...)方法,传入正确的数据。这是应该从添加ActionListener中调用的方法。
  • 或者让自己更轻松,根本不使用ArrayList,而是扩展DefaultTableModel,并使用它的方法。这将是如此简单和容易,而且是我所做的。

例如:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.table.*;

public class TableFun extends JPanel {
    public static final String[] COL_NAMES = { "First Name", "Last Name", "E-mail", "Telephone" };
    private MyTableModel2 model = new MyTableModel2(COL_NAMES);
    private JTable myTable = new JTable(model);

    public TableFun() {
        JButton addPersonBtn = new JButton(new AddPersonAction("Add Person"));
        JPanel btnPanel = new JPanel();
        btnPanel.add(addPersonBtn);

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

    private class AddPersonAction extends AbstractAction {
        public AddPersonAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = "John";
            String surname = "Smith";
            String address = "Foo Street";
            String telephone = "222-222-5555";
            model.addRow(new Person(name, surname, address, telephone));
        }
    }

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

        JFrame frame = new JFrame("Table Fun");
        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(() -> createAndShowGui());
    }

    private class MyTableModel2 extends DefaultTableModel {
        public MyTableModel2(String[] columnNames) {
            super(columnNames, 0);
        }

        public void addRow(Person person) {
            Object[] rowData = new Object[4];
            rowData[0] = person.getName();
            rowData[1] = person.getSurname();
            rowData[2] = person.getMail();
            rowData[3] = person.getTelephone();            
            super.addRow(rowData);
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (getRowCount() > 0) {
                Object value = getValueAt(0, columnIndex);
                if (value != null) {
                    return value.getClass(); 
                }
            }
            return super.getColumnClass(columnIndex);
        }
    }
}

class Person {
    private String name;
    private String surname;
    private String mail;
    private String telephone;

    public Person(String n, String s, String m, String t) {
        name = n;
        surname = s;
        mail = m;
        telephone = t;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getMail() {
        return mail;
    }

    public String getTelephone() {
        return telephone;
    }


}

请注意,如果您按照建议对其进行杂乱处理,则您自己的代码可以正常工作:

class MyTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1L;
    private ArrayList<Person1> personList = new ArrayList<Person1>();
    private String[] columnNames = { "First Name", "Last Name", "E-mail", "Telephone" };

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public int getRowCount() {
        return personList.size(); //!! 
    }

    @Override
    public Object getValueAt(int row, int col) {
        Person1 person = personList.get(row);
        switch (col) {
        case 0:
            return person.name;
        case 1:
            return person.surname;
        case 2:
            return person.mail;
        case 3: 
            return person.telephone;
        }
        return "";
    }

    public void setValueAt(String n, String s, String e, int t) {
        Person1 person = new Person1(n, s, e, t);
        personList.add(person);
        int firstRow = personList.size() - 1;
        int lastRow = firstRow;
        fireTableRowsInserted(firstRow, lastRow);
    }
}