我有JTable,通过AbstractTableModel显示来自arraylist的数据。
我想要做的是当我在JTable中的行上单击(左键单击)时,我想显示一个新窗口,在哪里可以查看和编辑存储在arraylist中的行中的数据。
我已经编写了MouseListener,但它什么也没做。
有main,table和tablemodel类。
public class MainWindow extends JFrame {
private Controller controller;
private Prihlasovanie prihlasovanie;
private TabulkaSkladnik tabulkaskladnik;
private Okno okno;
public MainWindow() {
super("PROJEKT OOP");
setVisible(true);
setSize(750, 508);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
setMinimumSize(new Dimension(800,500));
controller = new Controller();
tabulkaskladnik = new TabulkaSkladnik();
prihlasovanie = new Prihlasovanie();
tabulkaskladnik.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Okno okno = new Okno();
okno.setVisible(true);
}
});
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
getContentPane().add(tabbedPane, BorderLayout.NORTH);
tabbedPane.addTab("Prihlasovanie", null , prihlasovanie , null);
tabbedPane.addTab("Skladnik", null , tabulkaskladnik , null);
tabulkaskladnik.setData(controller.getObjednavky());
}
}
public class TabulkaSkladnik extends JTable {
private JTable tabulka;
private SkladnikTableModel tabulkaModel;
public TabulkaSkladnik() {
tabulkaModel = new SkladnikTableModel();
tabulka = new JTable(tabulkaModel);
setLayout(new BorderLayout());
add(new JScrollPane(tabulka),BorderLayout.CENTER);
}
public void setData(List<Objednavka> databaza) {
tabulkaModel.setData(databaza);
}
public void refresh() {
tabulkaModel.fireTableDataChanged();
}
}
public class SkladnikTableModel extends AbstractTableModel {
private List<Objednavka> databaza;
private String[] colNames = {"ID", "Nazov", "Popis", "Meno", "Priezvisko"};
public SkladnikTableModel() {
}
public void setData(List<Objednavka> databaza) {
this.databaza = databaza;
}
public String getColumnName(int column) {
// TODO Auto-generated method stub
return colNames[column];
}
public Class getColumnClass(int col) {
return getValueAt(0,col).getClass();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public int getRowCount() {
return databaza.size();
}
@Override
public Object getValueAt(int riadok, int stlpec) {
Objednavka objednavka = databaza.get(riadok);
switch(stlpec) {
case 0: return objednavka.getId();
case 1: return objednavka.getNazov();
case 2: return objednavka.getOpis();
case 3: return objednavka.getMeno();
case 4: return objednavka.getPriezvisko();
}
return null;
}
}
感谢您的帮助! :)