0

Hello I am making a JTable in Java and I have weird problem. I can only see the background color.

This is the Method that is responsible for the creation.

public void createCustomerList() {

    table = new JTable();
    tableModel = new DefaultTableModel();

    tableModel.addColumn("Naam");
    tableModel.addColumn("Achternaam");
    tableModel.addColumn("Klant-ID");

    table.setSize(954, 686);
    table.setLocation(100, 78);
    table.setBackground(Color.white);
    table.setForeground(Color.black);
    table.setGridColor(Color.black);
    table.setRowHeight(30);
    table.setFont(new Font("Tahoma", Font.PLAIN, 36));

    JScrollPane scrollPane = new JScrollPane(table);
    table.setModel(tableModel);
    this.add(scrollPane);
    this.add(table);

}

and in my constructor I call this method. but I only get the background color ( when changing it to black or red or something else it changes ) but I don't see columns and the grid etc. Where could the problem be?

Reshad
  • 2,570
  • 8
  • 45
  • 86
  • 1
    The default grid color on Mac is white, as discussed [here](http://stackoverflow.com/a/13779735/230513). – trashgod Oct 31 '13 at 20:41

1 Answers1

2

By adding the table to the container, you've removed it from the scroll pane. Start by removing the last add statement and make sure you are using an appropriate layout manager.

As pointed about by TrashGod, some look and feels may choose to set the grid color to the same color as the tables background color

Updated with working example

When you add a JTable (or any other component for that matter) to a JScrollPane, the component falls under the control of the scroll pane, you no longer have (a great deal of) control over the layout of the component.

You need to then focus on laying out the JScrollPane appropriately...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class QuickTable {

    public static void main(String[] args) {
        new QuickTable();
    }

    public QuickTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTable table = new JTable();
                DefaultTableModel tableModel = new DefaultTableModel();

                tableModel.addColumn("Naam");
                tableModel.addColumn("Achternaam");
                tableModel.addColumn("Klant-ID");

                for (int index = 0; index < 10; index++) {

                    String value = Integer.toString(index);
                    tableModel.addRow(new Object[]{value, value, value});

                }

                table.setModel(tableModel);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • What layout manager do you recommend? I use setLayOut(null) at the moment so I can assign the location of some items myself manually. The grid color I have set that to black as u can see in my code. – Reshad Oct 31 '13 at 20:47
  • Typically I would normally place the scroll pane within a container that is using a `BorderLayout`, but I also use `GridBagLayout` setting the scroll pane's constraints in such a way to make it fill it's available space within the cell – MadProgrammer Oct 31 '13 at 22:24
  • Unfortunately setting the layout to border layout shows the table. I tried it with another one and with a few of the layout managers it does show the table but the rest of my view is screwed up.. – Reshad Oct 31 '13 at 23:08
  • 1
    What scare's me, is you seem to be trying to place the and size the table yourself, but it's not the table that you need to worried about, because it's within the control of the scroll pane, it's the scrollpane – MadProgrammer Oct 31 '13 at 23:12
  • Aah yes it is.. so now I did a setBounds on the scroll pane and it works mate! thanks – Reshad Oct 31 '13 at 23:28