A few notes for reference:
I've incorporated @Reimeus' suggestion, but I'm not really sure what you want.
I'm sure the border is merely illustrative, but note that the API authors, "recommend that you put the component in a
JPanel
and set the border on theJPanel
." In particular, the green border obscures part of the first table row.Note that the gridlines belong to the UI delegate, as shown here; if this is critical, plan to test on each target L&F.
Unless you have a specific reason to wait on the initial thread, use
invokeLater()
.If the
JTable
dominates your initial appearance, overriding theScrollable
interface methodgetPreferredScrollableViewportSize()
is a reasonable compromise of the general guidelines here.Use
pack()
to leverage a component's preferred size.Make
setVisible()
last.
Addendum: Based on your update, TableGridProb2
, I added a colored border to an enclosing panel to see better. Indeed, com.apple.laf.AquaTableUI
leaves a one-pixel left margin, presumably for the selection rectangle. You might try this with your chosen L&F.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ColorUIResource;
public class TableGridProb2 {
public static void main(String[] args) {
UIManager.put("Table.gridColor", new ColorUIResource(Color.gray));
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Table Grid Prob2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane sp = new JScrollPane(new JTable(20, 5));
JPanel p = new JPanel(new GridLayout());
p.setBorder(new LineBorder(Color.green, 4));
p.add(sp);
frame.getContentPane().add(p);
frame.pack();
frame.setVisible(true);
}
});
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ColorUIResource;
public class TableGridProb {
public static void main(String[] args) {
UIManager.put("Table.gridColor", new ColorUIResource(Color.gray));
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Table Grid Prob");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel leftPanel = new JPanel(new BorderLayout());
JTree jt = new JTree();
jt.expandRow(1);
leftPanel.add(jt, BorderLayout.WEST);
JTable table = new JTable(20, 1) {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(300, 200);
}
};
table.setBorder(new LineBorder(Color.green, 4));
table.setTableHeader(null);
leftPanel.add(new JScrollPane(table), BorderLayout.CENTER);
JSplitPane splitPane = new JSplitPane();
splitPane.setLeftComponent(leftPanel);
frame.add(splitPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}