Looking round Stack exchange I can’t find a good answer to this. Most of the code in similar questions I’ve seen look massive and bloated where an error could easily creep in; I can’t see if I’m making the same mistake or not.
I only have two classes (and an interface), my probelm being is that my JTable is blank:

Here is my code:
Launch the program
public class Launcher {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
IViewManager.Util.getInstance();
}
});
}
}
Start the ViewManager frame
public class ViewManager implements IViewManager {
JFrame frame = null;
JTabbedPane myListTabs = null;
ComicsListPane myComicsListPane = null;
public ViewManager(){
//Create and set up the window.
frame = new JFrame("My List Agregator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the Tabbed pane for lists.
myListTabs = new JTabbedPane();
myComicsListPane = new ComicsListPane();
myListTabs.add(myComicsListPane);
myListTabs.setTitleAt(myListTabs.getTabCount()-1, "title");
frame.getContentPane().add(myListTabs);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
The Comics List Pane
public class ComicsListPane extends JPanel {
/**
*
*/
private static final long serialVersionUID = -5207104867199042105L;
JTable myComicsTable = null;
public ComicsListPane() {
//Create the column names and data
String[] columnNames = {"Comic Title",
"Volume",
"Edition",
"Purchased"};
Object[][] data = {
{"The Amazing Spider-man", new Integer(3),
new Integer(679), new Boolean(false)},
{"The Amazing Spider-man", new Integer(3),
new Integer(680), new Boolean(false)}
};
//Create the table
myComicsTable = new JTable(data, columnNames);
myComicsTable.setPreferredScrollableViewportSize(new Dimension(750, 110));
myComicsTable.setFillsViewportHeight(true);
myComicsTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(myComicsTable);
scrollPane.add(myComicsTable);
scrollPane.setPreferredSize(new Dimension(450, 110));
this.add(scrollPane, BorderLayout.CENTER);
}
}
I can’t see where I’ve gone wrong, mostly I’ve copied form the Java Doc Tutorials.
Could someone help point out the error, to a java novice?
It looks like your problem is when you create the
JScrollPane.The second line here is not needed – you’ve already constructed the
JScrollPanewith theJTable, there’s no need to then add it to theJScrollPane. Indeed – this is using theadd()method fromContainer. Exactly why it’s causing the behaviour you are seeing is not clear to me without looking at the javadocs/source in more detail.As an aside, when you’re working with GUIs (and indeed, generally) it’s best to narrow your code down to the smallest chunks. i.e. a method to create the
JTable– can that be displayed? Then, a method to wrap it in aJScrollPane– does display as you would wish? etc. etc.