Reputation: 2720
I'm trying to put JTable
inside JScrollPane
. JScrollPane
is inside JPanel
which is the part of JTabbedPane
.
Here is the code:
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel pnlMaxMinAvg1 = new JPanel();
scrollPane.setViewportView(pnlMaxMinAvg1);
tabbedPane.addTab("MaxMinAvg1", null, pnlMaxMinAvg1, null);
pnlMaxMinAvg1.setLayout(new BorderLayout(0,0));
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {{"V_L1_N_RMS_AVG", null}
},
new String[] {
"Variable", "Value"
}
));
table.setBounds(178, 11, 200,300);
scrollPane.add(table);
When I run this, table
is not displayed. If I set:
pnlMaxMinAvg1.add(table);
then table
is visible.
Why?
Upvotes: 1
Views: 512
Reputation: 205875
Your JPanel
, pnlMaxMinAvg1
, is empty at the time you make it the viewport view of scrollPane
. Adding the table
to pnlMaxMinAvg1
gives the scroll pane some non-empty content to display. Moreover, avoid setBounds()
and let the layout do the work. In the example below, the scroll pane's view is set to the table explicitly, as in your fragment, but you can specify it in the scroll pane's constructor.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
* @see https://stackoverflow.com/q/46669145/230513
*/
public class TableTest {
private void display() {
JFrame f = new JFrame("TableTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][]{{"V_L1_N_RMS_AVG", null}},
new String[]{"Variable", "Value"}
));
JScrollPane jsp = new JScrollPane();
jsp.setViewportView(table);
JTabbedPane jtp = new JTabbedPane();
jtp.add("TableTest", jsp);
f.add(jtp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new TableTest()::display);
}
}
In this variation, I've added N
rows of data and overridden the Scrollable
method, getPreferredScrollableViewportSize()
, as shown here. Resize the enclosing window to see how it affects the scrollbar.
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
* @see https://stackoverflow.com/q/46669145/230513
*/
public class TableTest {
private static final int N = 16;
private void display() {
JFrame f = new JFrame("TableTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultTableModel model = new DefaultTableModel(
null, new String[]{"Variable", "Value"}
);
JTable table = new JTable(model) {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(256, getRowHeight() * N / 2);
}
};
for (int i = 0; i < N; i++) {
model.addRow(new Object[]{"V_L1_N_RMS_AVG" + i, String.valueOf(i)});
}
JScrollPane jsp = new JScrollPane();
jsp.setViewportView(table);
JTabbedPane jtp = new JTabbedPane();
jtp.add("TableTest", jsp);
f.add(jtp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new TableTest()::display);
}
}
Upvotes: 1