gpt4 book ai didi

java - 如何用基本表格填充 JScrollPane

转载 作者:行者123 更新时间:2023-12-01 12:37:48 25 4
gpt4 key购买 nike

我的计划是创建一个“基本”表格模板,每次玩家创建新的播放列表时都会填充 JScrollPane。

Netbeans GUI 编辑器创建如下代码: 私有(private)无效 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);

我有我的模板: 公共(public)类 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

当我想要调用此表"template"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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com