Reputation: 846
My plan is to create a "basic" table template that will fill the JScrollPane every time the player creates a new playlist.
Netbeans GUI editor creates the code like so: private void 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);
I have my template: public class BasicTableGUI extends 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);
}
}
and I attempt to call it from netbeans' GUI code like so:
...
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());
I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException:
Uncompilable source code Erroneous sym type:
javax.swing.JScrollPane.setViewportView
What is the correct way to call this table "template", BasicGUI when I want to, and have I created the BasicGUI class correctly?
Upvotes: 0
Views: 278
Reputation: 1420
You should never sub class JTable. You should create a class that extends AbstractTableModel and implement the data call methods. Then instead of this:
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"
}
));
you would
jTable1.setModel(new MyBasicTableModel());
Your MyBasicTableModel class will then hold all the data that will be displayed in the table. You may need to add extra methods so your program will be able to update the the data as needed. You also have to notify listeners when you get data updates so the table will be updated visually.
You can find more information at http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data
Upvotes: 1