Hobolics
Hobolics

Reputation: 11

Creating bold cell border lines JTable

I'm trying to create a Sudoku game and I am having trouble creating the bold lines to break it into 3 x 3 blocks. My recent attempt was to impose an image on top of the table using JLabel. The problem is the label completely covers the JTable. I goofed around with setting the opacity of the label and the table with no luck. Here are some images to show what I'm aiming for:

The current look:

enter image description here

The goal:

enter image description here

If you could advise me as to a method I can use to create these lines, it would be greatly appreciated. No direct answers needed, just a point in the right direction.

Upvotes: 1

Views: 1147

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168845

For any board game I'd tend to use buttons in a grid layout (or a group of grid layouts) like in this mine sweeper game or this chess board.

For the borders in this GUI though, I'd use a 3 x 3 group of nine grid layouts, each of which has a LineBorder. By default the border would go around all four sides of the panel it is displayed in, but where they meet the border would be double width, thereby coming close to recreating the second image.

E.G.

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;

public class Soduko {

    private JComponent ui = null;

    Soduko() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridLayout(3,3));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        ArrayList<Integer> values = new ArrayList<>();
        for (int ii=0; ii<34; ii++) {
            values.add(0);
        }
        Random r = new Random();
        for (int ii=34; ii<81; ii++) {
            values.add(r.nextInt(9)+1);
        }
        Collections.shuffle(values);
        int count=0;
        for (int ii=0; ii<9; ii++) {
            JPanel p = new JPanel(new GridLayout(3, 3));
            p.setBorder(new LineBorder(Color.BLACK, 2));
            ui.add(p);
            for (int jj=0; jj<9; jj++) {
                int v = values.get(count++).intValue();
                String s = v>0 ? "" + v : "";
                p.add(new JButton(s));
            }
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Soduko o = new Soduko();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 2

camickr
camickr

Reputation: 324207

Check out Table Row Rendering.

It shows how to provide a custom Border for each cell in a row.

So you would need to modify the code to provide multiple different Borders depending on the cell being rendered.

Upvotes: 2

Related Questions