I am trying to show some data on the JTables:
private JTable table1 = new JTable() {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
private JScrollPane secTabFirst() {
Object [][] data = {{"90"},{"1700"},{"60"},{"0.7"}, {"Globular"}, {"Sinusoidal"}};
String[] columnNames = {"Parameters"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table1.setModel(model);
table1.setTableHeader(null);
table1.setShowHorizontalLines(true);
table1.setShowVerticalLines(true);
//table1.setPreferredSize(new Dimension(150,240));
//table1.setMaximumSize(new Dimension(150,240));
JScrollPane tb1 = new JScrollPane(table1);
tb1.setVisible(true);
return tb1;
}
public static void main(String args[]){
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(secTabFirst());
}
The JTables are shown like these:
But the upper and left lines of the table are invisible. Also, it displays 2 dark-boxes after placing the tables in JScrollPane.
How can I make the lines visible and the dark-boxes disappear?
Thanks,
MB
I tried to replicate your issue without any success, a JTable
will never show the top and left borders by default, but this is not an issue as its usually expected to be rendered inside a JScrollPane
. As @HovercraftFullOfEels mentioned try giving us an Short, Self Contained, Correct (Compilable), Example
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class TestApp {
public TestApp() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TestApp::new);
}
private void initComponents() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
JTable table = new JTable() {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
Object[][] data = {{"90"}, {"1700"}, {"60"}, {"0.7"}, {"Globular"}, {"Sinusoidal"}};
String[] columnNames = {"Parameters"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table.setModel(model);
table.setTableHeader(null);
JScrollPane pane = new JScrollPane(table); // borders for top and left will be rendered thanks to JScrollPanes borders
panel.add(pane);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Update:
You can however set a Border
yourself which will ensure the top and left border lines are drawn:
table.setBorder(new LineBorder(Color.GRAY, 1));
Which produces (JTable
is not in a JScrollPane
in the below screenshot to show the borders without interference):