I don't understand where is the problem. The JTable is embedded in a JScrollPanel which is embedded in a JPanel. The table is not displaying. Any help appreciated. I probably missed to add some elements. Checked thoroughly but cannot find anything. This is just the constructor:
public TableIssues() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 894, 597);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel patientsPanel = new JPanel();
patientsPanel.setBounds(6, 152, 882, 417);
patientsPanel.setLayout(null);
String[] patientsColumns = {
"one",
"two",
"three"};
String[][] tableInput={{"first","second","third"},{"first","second","third"}};
patientsTable = new JTable(tableInput,patientsColumns);
JScrollPane scroll = new JScrollPane();
scroll.setBounds(0, 0, 882, 363);
scroll.setLayout(null);
scroll.setViewportView(patientsTable);
patientsPanel.add(scroll);
JButton addPatietsButton = new JButton("Add");
addPatietsButton.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
addPatietsButton.setBounds(356, 375, 211, 36);
patientsPanel.add(addPatietsButton);
contentPane.add(patientsPanel);
}
NEVER do this:
scroll.setLayout(null);
You ruin the JScrollPane's layout, and so it completely loses its functionality, thereby shooting yourself in the foot. Remove that line.
While null layouts and setBounds()
might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
For example, this GUI
is created by this code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TableIssues2 extends JPanel {
private static final int GAP = 5;
private static final String[] COL_NAMES = {"One", "Two", "Three"};
private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0);
private JTable patientsTable = new JTable(model);
public TableIssues2() {
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, GAP));
buttonPanel.add(new JButton("Add"));
buttonPanel.add(new JButton("Remove"));
buttonPanel.add(new JButton("Exit"));
JPanel bottomPanel = new JPanel();
bottomPanel.add(buttonPanel);
for (int i = 0; i < 5; i++) {
model.addRow(new String[]{"First", "Second", "Third"});
}
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(new JScrollPane(patientsTable), BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
TableIssues2 mainPanel = new TableIssues2();
JFrame frame = new JFrame("TableIssues2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}