我的计划是创建一个“基本”表模板,该模板将在玩家每次创建新的播放列表时填充JScrollPane。
Netbeans GUI编辑器将如下创建代码:
私人无效initComponents(){
playlistsScrollPane = new javax.swing.JScrollPane();
playlists = new javax.swing.JList();
musicFileTableScrollPane = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jTextField1 = new javax.swing.JTextField();
NewPlaylistButton = new javax.swing.JButton();
DeleteSelectedPlaylistButton = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
playlists.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
playlistsScrollPane.setViewportView(playlists);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
musicFileTableScrollPane.setViewportView(jTable1);
我有我的模板:
公共类BasicTableGUI扩展了JTable {
JTable table;
DefaultTableModel musicTableModel;
public BasicTableGUI() {
String[] columns = {"Artist", "Title", "Album", "Track Number", "Length"};
musicTableModel = new DefaultTableModel(columns, 15);
table = new javax.swing.JTable();
table.setModel(musicTableModel);
if (table.isEnabled()) {
System.out.println("yess");
} else {
System.out.println("nsssss");
}
table.setTransferHandler(new MusicTableHandler(musicTableModel));
table.setDragEnabled(true);
table.setDropMode(javax.swing.DropMode.ON_OR_INSERT_ROWS);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setFillsViewportHeight(true);
table.setVisible(true);
}
}
我尝试从netbeans的GUI代码中调用它,如下所示:
...
playlists.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
playlistsScrollPane.setViewportView(playlists);
musicFileTableScrollPane.setViewportView(newBasicGUI());
我收到此错误:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException:
Uncompilable source code Erroneous sym type:
javax.swing.JScrollPane.setViewportView
在需要时正确调用此表“模板”,BasicGUI的正确方法是什么?是否正确创建了BasicGUI类?
最佳答案
您永远不应将JTable子类化。您应该创建一个扩展AbstractTableModel的类并实现数据调用方法。然后代替这个:
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
你会
jTable1.setModel(new MyBasicTableModel());
然后,您的MyBasicTableModel类将保存将在表中显示的所有数据。您可能需要添加其他方法,以便您的程序能够根据需要更新数据。您还必须在获取数据更新时通知侦听器,以便可视地更新表。
您可以在http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data中找到更多信息。
关于java - 如何用基本表填充JScrollPane,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25414060/