1

我有一个对象 A,它实例化另一个对象 B。我想知道是否可以使用 B 中的指令修改 A。在我的情况下,我有一个打开的时间表(它的代码在“对象 A”下)(按InsertLesson.setVisible(true);)一个窗口,让用户使用课程编译其单元格。此时,窗口(InsertLesson“对象B”下的代码)获得用户选择的课程,但无法在表中写入该选择。我能怎么做?

这里的代码

对象 A:

public class TablePanel extends JPanel
{
private JTable table;
public Tabella()
{
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
    table = new JTable(new MyTableModel());
    table.setFillsViewportHeight(true);     
    table.setPreferredScrollableViewportSize(new Dimension(500, 100));
    JScrollPane jps = new JScrollPane(table);
    add(jps);
    add(new JScrollPane(table));
    table.setCellSelectionEnabled(true);
    table.addMouseListener(
        new MouseAdapter(){
        public void mouseClicked(MouseEvent e) {
            int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
            int col = table.columnAtPoint(new Point(e.getX(), e.getY()));

            if (col>0) {
            if (e.getClickCount() > 1) {
            if (row == 5 | row == 6) 
                {
                    JOptionPane.showMessageDialog(null, "Impossible to set lesson.");

                    return;
                }
            else {
                table.getColumnName(col);
                String day = table.getColumnName(col);
                String hour = (String) table.getValueAt(row, 0);
                InsertLesson cell = new InsertLesson(day, hour);
                cel.setVisible(true);

                 }
                }
              }
            }
    }
    );
}
private class MyTableModel extends AbstractTableModel {

private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

private String[][] data = {{"8:30 - 9:30","","","","","",""},
        {"9:30 - 10:30","","","","","",""},
        {"10:30 - 11:30","","","","","",""},
        {"11:30 - 12:30","","","","","",""},
        {"12:30 - 13:30","","","","","",""},
        {"13:30 - 14:30","","","","","",""},
        {"14:30 - 15:30","","","","","",""},
        {"15:30 - 16:30","","","","","",""},
        {"16:30 - 17:30","","","","","",""}};

    public int getColumnCount() {
        return columns.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columns[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }
}   

对象 B(必须修改 A):

public InsertLesson (String day, String hour)
        { 
            initialize(day, hour);
        }

        private void initialize(String day, String hour) {


            this.setSize(600,200);
            this.setTitle("Insert Lesson");
            this.setLocationRelativeTo(null);

            String[] lessons = {"math", "english", "art"};
            String [] classrooms = {"class1", "class2"};    

            JPanel centralPnl = new JPanel();
            this.getContentPane().add(centralPnl, BorderLayout.CENTER);


            final JComboBox classBox = new JComboBox(classrooms );
            centralPnl.add(classBox);

            final JComboBox lessonsBox = new JComboBox(lessons);
            centralPnl.add(lessonsBox);

            JPanel southPnl = new JPanel();
            this.getContentPane().add(southPnl, BorderLayout.SOUTH);

            JButton insLessonBtn = new JButton("Insert Lesson");
            southPnl.add(insLessonBtn);

            lessonsBox.addItemListener(new ItemListener()
            {
                @Override
                public void itemStateChanged(ItemEvent e) {

                    if (e.getStateChange() == ItemEvent.SELECTED)
                    {
                        selectedLesson = lessonsBox.getSelectedItem().toString();

                    }
                }
            });

            classBox.addItemListener(new ItemListener(){
                @Override
                public void itemStateChanged(ItemEvent e) {

                    if (e.getStateChange() == ItemEvent.SELECTED)
                    {
                        selectedClass = classBox.getSelectedItem().toString();

                    }


                }


            });

            class  MouseSpy implements MouseListener
            {
                    public void mouseClicked(MouseEvent e)
                    {
                        JOptionPane.showMessageDialog(null,"Do something for modify table with\n"
                                + "values of selectedLesson and selectedClass");
                    }
                    public void mousePressed(MouseEvent e) {}
                    public void mouseReleased(MouseEvent e) {}
                    public void mouseEntered(MouseEvent e) {}
                    public void mouseExited(MouseEvent e) {}
            }

            MouseListener listener = new MouseSpy();
            insLessonBtn.addMouseListener(listener);

        }
        }
}
4

1 回答 1

2

要更新A中的表,B必须调用ofsetValueAt()上的方法。或者,向您添加一个执行更新的方法。这里可以看到一个典型的实现。如果这没有帮助,请编辑您的问题以包含一个显示您遇到的问题的sscceTableModelATableModelsetValueAt()

附录:我想在用户按下…按钮后更新表格…。

作为使用 的具体示例TableModelUpdate下面的按钮会在每次按下时更新表格的模型。将 的setValueAt ()实现与上面引用的进行比较。按钮的actionPerformed()方法在封闭范围内访问对 的final引用TableModel,但您可以将您的引用TableModel作为参数传递给 的构造函数InsertLesson

附录:你会为我写吗?

不,但我将概述该方法,假设一个类InsertLesson

TableModel model = new MyTableModel();
JTable table = new JTable(model);
InsertLesson cell = new InsertLesson(day, hour, model);
…
class InsertLesson {

    TableModel model;

    public InsertLesson(String day, String hour, TableModel model) {
        this.model = model;
        …
    }
    …
}

表格图像

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 * @see http://stackoverflow.com/a/18764073/230513
 */
public class Test {

    private static class MyTableModel extends AbstractTableModel {

        private String[] columns = {
            "Time", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        private String[][] data = {
            {"8:30 - 9:30", "", "", "", "", ""},
            {"9:30 - 10:30", "", "", "", "", ""},
            {"10:30 - 11:30", "", "", "", "", ""},
            {"11:30 - 12:30", "", "", "", "", ""},
            {"12:30 - 13:30", "", "", "", "", ""},
            {"13:30 - 14:30", "", "", "", "", ""},
            {"14:30 - 15:30", "", "", "", "", ""},
            {"15:30 - 16:30", "", "", "", "", ""},
            {"16:30 - 17:30", "", "", "", "", ""}};

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

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

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

        @Override
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        @Override
        public void setValueAt(Object aValue, int row, int col) {
            data[row][col] = (String) aValue;
            fireTableCellUpdated(row, col);
        }
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final TableModel model = new MyTableModel();
        f.add(new JScrollPane(new JTable(model) {
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(600, 128);
            }
        }));
        f.add(new JButton(new AbstractAction("Update") {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.setValueAt(String.valueOf(e.getWhen() % 1000000), 1, 1);
            }
        }), BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
于 2013-09-12T12:18:21.843 回答