NotRightMeow
NotRightMeow

Reputation: 29

JTable only shows the right border of the table

I have a JTable and I want to make a tic tac toe themed table. I have managed to get rid of three borders without really knowing how...

Here's my code:

public class Table extends JPanel {

    public static void main( String[] args ) {

        JFrame jFrame = new JFrame();
        JTable table = new JTable(3, 3);

        table.setBounds(90, 20, 200, 200);
        for(int i = 0; i < 3; i++) {

            table.setRowHeight(i, 67);        

        }
        table.setBackground(Color.black);
        table.setGridColor(Color.white);
        table.setForeground(Color.white);

        // content pane so I can set the background
        jFrame.getContentPane().add(table);
        jFrame.getContentPane().setBackground(Color.black);
        jFrame.setSize(400, 400);
        jFrame.setLayout(null);
        jFrame.setVisible(true);

    }

}

I also have some stuff that initializes the table more like setting values but I don't think that will matter but I can add it if you want. So how could I get the right border of the table to disappear too?

When I compile this code, the top, left and bottom borders are not visible

Upvotes: 0

Views: 114

Answers (1)

George Z.
George Z.

Reputation: 6808

You can usesetBorder method with a LineBorder in order to fill it:

table.setGridColor(Color.white);
table.setBorder(BorderFactory.createLineBorder(table.getGridColor()));

By the way, setLayout(null) and setBounds is a bad practice that is going to give you hard time in the future (plus it is not user-friendly, since you cannot resize your frame and won't work in different screen sizes). Try to use LayoutManagers and let them do the work for you.

Also, take a look at initial threads. All swing applications should run on their own thread and not in the main thread.

Upvotes: 1

Related Questions