数据库更改后无法重新加载我的帧

时间:2015-04-23 16:31:48

标签: java database jtable

更新数据库后,我无法重新加载我的JInternalFrame 我有2帧 在第一个我试图看到我的表有问题和建议,在第二个我试图提出新的问题和建议。
输入完所有内容后,我将保存按钮和新记录保存在数据库中,但不会重新加载到第一帧。

这是第一帧的代码

      package diplomwork2;

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeSupport;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import javax.swing.AbstractCellEditor;
    import javax.swing.JFrame;
    import javax.swing.JCheckBox;
    import javax.swing.JInternalFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    import javax.swing.*;

public class Test extends RealizedIMainFrame{

    private JPanel buttonPanel=new JPanel();
    private JButton button = new JButton("Reload");
//-----------------------------------------------------------------------------------------
    public class JCHECKBOXCellEditorRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener {

        private JCheckBox CheckBox;

        public JCHECKBOXCellEditorRenderer() {
            this.CheckBox = new JCheckBox();
            CheckBox.addActionListener(actionListener);
            CheckBox.setOpaque(false);

        }
        private ActionListener actionListener = new ActionListener() {
         @Override public void actionPerformed(ActionEvent actionEvent) {
                     if(CheckBox.isSelected()){
                table.setRowHeight(table.getSelectedRow(), 50);
                     table.setFillsViewportHeight(true);
                     table.setSelectionBackground(Color.RED);
                     }
                     else{
                         table.setRowHeight(table.getSelectedRow(), 20);
                     }

                }
                 };
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            CheckBox.setSelected(Boolean.TRUE.equals(value));

            return CheckBox;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            CheckBox.setSelected(Boolean.TRUE.equals(value));
            return CheckBox;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            stopCellEditing();
        }

        @Override
        public Object getCellEditorValue() {
            return CheckBox.isSelected();
        }

    }
//-----------------------------------------------------------------------------------------


    private class MyObjectManager {
        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
        private List<MyObject> objects = new ArrayList<Test.MyObject>();
        private List<MyObject> objects2 = new ArrayList<Test.MyObject>();
        public void addObject(MyObject object) {
            objects.add(object);
            object.setManager(this);
            propertyChangeSupport.firePropertyChange("objects", null, object);
        }
        public void addObject2(MyObject object) {
            objects2.add(object);
            object.setManager(this);
            propertyChangeSupport.firePropertyChange("objects2", null, object);
        }
        public List<MyObject> getObjects() {
            return objects;
        }
        public List<MyObject> getObjects2() {
            return objects2;
        }
        public void setAsSelected(MyObject myObject) {
            for (MyObject o : objects) {
                o.setSelected(myObject == o);
            }
        }
        public void setAsSelected2(MyObject myObject) {
            for (MyObject o : objects2) {
                o.setSelected(myObject == o);
            }
        }
    }

    private class MyObject {
        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

        private MyObjectManager manager;

        private String value;

        private boolean selected;

        public MyObject(String value) {
            this.value = value;
        }

        public PropertyChangeSupport getPropertyChangeSupport() {
            return propertyChangeSupport;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
            propertyChangeSupport.firePropertyChange("value", null, value);
        }

        public MyObjectManager getManager() {
            return manager;
        }

        public void setManager(MyObjectManager manager) {
            this.manager = manager;
            propertyChangeSupport.firePropertyChange("manager", null, manager);
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            if (this.selected != selected) {
                this.selected = selected;
                if (selected) {
                    manager.setAsSelected(this);
                }
                propertyChangeSupport.firePropertyChange("selected", !selected, selected);
            }
        }

    }

    private class ReloadActionListener implements ActionListener {
            @Override public void actionPerformed(ActionEvent e) {
                //MyObjectManager manager = new MyObjectManager();
                   //MyTableModel m=new MyTableModel(manager);
                //table.setModel(m);
                ((MyTableModel) table.getModel()).fireTableDataChanged();
                table.revalidate();
                table.repaint();
                table.updateUI();


            }

             }
    Test(String s) {
        super();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            try {
                con = DriverManager.getConnection(url, login, password);
            } catch (SQLException e) {

                e.printStackTrace();
            }

            f.setBounds(0, 10, 300, 350);

            f.setVisible(true);
        MyObjectManager manager = new MyObjectManager();
        for (int i = 0; i < GetLastID(); i++) {
            MyObject object = new MyObject(GetQuestion().get(i));

            manager.addObject(object);
        }
        for (int i = 0; i < GetLastID(); i++) {
            MyObject object = new MyObject(GetAdvise().get(i));

            manager.addObject2(object);
        }
        table = new JTable(new MyTableModel(manager));
        table.setRowHeight(20);
        TableColumn column = table.getColumnModel().getColumn(1);
        column.setCellEditor(new JCHECKBOXCellEditorRenderer());//Redaktor
        column.setCellRenderer(new JCHECKBOXCellEditorRenderer());//visualizator


        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(table), BorderLayout.CENTER);
        f.pack();
        f.setVisible(true);
        f.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            ActionListener actionListener2 = new ReloadActionListener();
            button.addActionListener(actionListener2);
            button.setVisible(true);
            buttonPanel.add(button);

    }
//-----------------------------------------------------------------------------------------
    public class MyTableModel extends AbstractTableModel implements PropertyChangeListener {

        private final MyObjectManager manager;

        public MyTableModel(MyObjectManager manager) {
            super();
            this.manager = manager;
            manager.propertyChangeSupport.addPropertyChangeListener(this);
            for (MyObject object : manager.getObjects()) {
                object.getPropertyChangeSupport().addPropertyChangeListener(this);
            }
            for (MyObject object2 : manager.getObjects2()) {
                object2.getPropertyChangeSupport().addPropertyChangeListener(this);
            }

        }

        //private MyTableModel() {super(); }

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getSource() == manager) {

                if (evt.getPropertyName().equals("objects")) {
                    ((MyObject) evt.getNewValue()).getPropertyChangeSupport().addPropertyChangeListener(this);
                }
                fireTableDataChanged();
            } else if (evt.getSource() instanceof MyObject) {
                int index = manager.getObjects().indexOf(evt.getSource());
                fireTableRowsUpdated(index, index);
            }
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        /*public int getColumn() {
            return 3;
        }*/
        @Override
        public int getRowCount() {
            return manager.getObjects().size();
        }

        public MyObject getValueAt(int row) {
            return manager.getObjects().get(row);
        }
        public MyObject getValueAt2(int row) {
            return manager.getObjects2().get(row);
        }
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 0:
                return getValueAt(rowIndex).getValue();
            case 1:
                return getValueAt(rowIndex).isSelected();
            case 2:
                return getValueAt2(rowIndex).getValue();
            }
            return null;
        }

        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex) {
            if (columnIndex == 1) {
                getValueAt(rowIndex).setSelected(Boolean.TRUE.equals(value));
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 1;
        }

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
            case 0:
                return String.class;
            case 1:
                return Boolean.class;
            case 2: 
                return String.class;
            }
            return Object.class;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
            case 0:
                return "Questions";
            case 1:
                return "Selected";
            case 2:
                return "Advises";
            }
            return null;
        }

    }

   /* public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().initUI();
            }
        });
    }*/
}

0 个答案:

没有答案