我正在实现一个JFrame类,该类必须查看表并对其进行处理。该表有一个AbstractTableModel,并且我已经在该表上定义了要在JFrame类中使用的方法。但是我无法做到这一点。
这里的代码:

必须查看JTable的JFrame类

public class CreaOrario extends JFrame {


public CreaOrario(int num) {
    initialize(num);
}

/**
 * Initialize the contents of the frame.
 */
private void initialize(final int semestre) {
    this.setSize(1150,650);
    this.setTitle("Creazione dell'Orario");
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    TabellaOrario orarioEditabile = new TabellaOrario(semestre);
    this.getContentPane().add(orarioEditabile, BorderLayout.CENTER);

    final JTable table = orarioEditabile.getTable();
    //now I'd like to do: table.getLesson;
}
}


必须在JFrame中添加并且包含表的JPanel类:

public class TabellaOrario extends JPanel
{
private JTable table;

public TabellaOrario (int semestre)
{

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    table = new JTable(new MyTableModel());
    table.setFillsViewportHeight(true);
    table.setPreferredScrollableViewportSize(new Dimension(510, 350));
    JScrollPane jps = new JScrollPane(table);
    add(jps);
    add(new JScrollPane(table));
    table.setCellSelectionEnabled(true);
    table.setRowHeight(40);

}

public JTable getTable() {
    return table;
}
public class MyTableModel extends AbstractTableModel
{


private String[] columns = {"","Lunedì","Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"};


private Object [][] 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] = aValue;
            fireTableCellUpdated(row, col);

            }

     }
        public String[] getLesson (int row, int column)
        {
            String value = (String) table.getValueAt(row, column);
            int start = value.indexOf("<b>");
            int end = value.indexOf("</b>");
            String insegnamento = value.substring(start + "<b>".length(), end);

            start = value.lastIndexOf("<b>");
            end = value.lastIndexOf("</b>");
            String aula = value.substring(start + "<b>".length(), end);

            return new String[] {insegnamento,aula};

        }
}
}


MyTableModel类中,我定义了要在getLesson()类中使用的方法CreaOrario

但是,如果在MyTableModel中我写table.getLesson(),则显示消息错误,提示The method getLesson() is undefined for the type JTable
为什么? table实际上是一个TabellaOrario对象,并具有MyTableModel作为模型。 getLesson()是为MyTableModel定义的,因此我希望table.getLesson()可以工作。

最佳答案

这是因为table被声明为JTable,而getTable()也会返回JTable。您只需要类型转换,因为getLesson()JTable中不存在。

您需要像这样调用getLesson():

String[] lessons = ((TabellaOrario.MyTableModel)table.getModel()).getLesson();

关于java - 无法对相同类的对象使用方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18812032/

10-09 05:15