将instancer对象修改为instanced对象

时间:2013-09-12 11:45:44

标签: java swing object jtable

我有一个对象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);

        }
        }
}

1 个答案:

答案 0 :(得分:2)

要更新 A 中的表格, B 必须调用setValueAt() TableModel上的方法A。或者,向执行更新的TableModel添加方法。可以看到setValueAt()的典型实现here。如果这样做无效,请修改您的问题以包含展示您遇到的问题的sscce

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

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

附录:你会为我写这些吗?

不,但我会概述这种方法,假设课程为TableModel

InsertLesson

table image

InsertLesson