0

I have 2 classes, WordSearch.java is where all the main code is held and WordSearchSwing.java is where I wish to create a JTable to display the 2D array "letters". The JTable doesn't display any content (just 12 blank cells) when the following code is run. I cannot figure out why it will not work, any help would be much appreciated.

WordSearch Class:

import java.util.ArrayList;

public class WordSearch {

    public static String[][] letters = new String[12][12];
    static ArrayList<String> words = new ArrayList<String>();

    public static void main(String[] args) {


        //populating the arrayList with Strings, some which are in the wordsearch and some which aren't
        words.add("ghost");     words.add("knife");     words.add("patch");     words.add("ghoul");     words.add("apple");
        words.add("boo");       words.add("spider");    words.add("pumpkin");   words.add("house");     words.add("trick");
        words.add("cat");       words.add("candy");     words.add("orange");    words.add("rat");       words.add("crow");
        words.add("blood");     words.add("bats");      words.add("neck");      words.add("mask");      words.add("witch");
        words.add("party");     words.add("broom");     words.add("fangs");     words.add("mummy"); 

        //A 2D array is used to make the wordsearch
        //Here each coordinate is assigned a letter
        letters[0][0] = "o";    letters[1][0] = "e";    letters[2][0] = "o";    letters[3][0] = "e";    letters[4][0] = "b";    letters[5][0] = "o";
        letters[0][1] = "h";    letters[1][1] = "n";    letters[2][1] = "i";    letters[3][1] = "l";    letters[4][1] = "t";    letters[5][1] = "r";
        letters[0][2] = "b";    letters[1][2] = "c";    letters[2][2] = "o";    letters[3][2] = "d";    letters[4][2] = "y";    letters[5][2] = "a";
        letters[0][3] = "d";    letters[1][3] = "o";    letters[2][3] = "t";    letters[3][3] = "m";    letters[4][3] = "d";    letters[5][3] = "n";
        letters[0][4] = "d";    letters[1][4] = "h";    letters[2][4] = "m";    letters[3][4] = "a";    letters[4][4] = "c";    letters[5][4] = "g";
        letters[0][5] = "p";    letters[1][5] = "u";    letters[2][5] = "o";    letters[3][5] = "a";    letters[4][5] = "p";    letters[5][5] = "e";
        letters[0][6] = "m";    letters[1][6] = "u";    letters[2][6] = "n";    letters[3][6] = "u";    letters[4][6] = "t";    letters[5][6] = "r";
        letters[0][7] = "m";    letters[1][7] = "d";    letters[2][7] = "m";    letters[3][7] = "k";    letters[4][7] = "s";    letters[5][7] = "e";
        letters[0][8] = "y";    letters[1][8] = "d";    letters[2][8] = "c";    letters[3][8] = "p";    letters[4][8] = "h";    letters[5][8] = "e";
        letters[0][9] = "w";    letters[1][9] = "e";    letters[2][9] = "i";    letters[3][9] = "e";    letters[4][9] = "k";    letters[5][9] = "r";
        letters[0][10] = "n";   letters[1][10] = "y";   letters[2][10] = "n";   letters[3][10] = "e";   letters[4][10] = "o";   letters[5][10] = "i";
        letters[0][11] = "c";   letters[1][11] = "t";   letters[2][11] = "f";   letters[3][11] = "h";   letters[4][11] = "o";   letters[5][11] = "w";

        letters[6][0] = "b";    letters[7][0] = "o";    letters[8][0] = "o";    letters[9][0] = "l";    letters[10][0] = "c";   letters[11][0] = "h";
        letters[6][1] = "o";    letters[7][1] = "b";    letters[8][1] = "h";    letters[9][1] = "t";    letters[10][1] = "a";   letters[11][1] = "s";
        letters[6][2] = "g";    letters[7][2] = "h";    letters[8][2] = "o";    letters[9][2] = "s";    letters[10][2] = "t";   letters[11][2] = "g";
        letters[6][3] = "r";    letters[7][3] = "b";    letters[8][3] = "a";    letters[9][3] = "t";    letters[10][3] = "s";   letters[11][3] = "n";
        letters[6][4] = "e";    letters[7][4] = "o";    letters[8][4] = "f";    letters[9][4] = "u";    letters[10][4] = "t";   letters[11][4] = "a";
        letters[6][5] = "d";    letters[7][5] = "e";    letters[8][5] = "f";    letters[9][5] = "t";    letters[10][5] = "m";   letters[11][5] = "f";
        letters[6][6] = "i";    letters[7][6] = "c";    letters[8][6] = "k";    letters[9][6] = "o";    letters[10][6] = "p";   letters[11][6] = "m";
        letters[6][7] = "p";    letters[7][7] = "h";    letters[8][7] = "o";    letters[9][7] = "e";    letters[10][7] = "a";   letters[11][7] = "a";
        letters[6][8] = "s";    letters[7][8] = "r";    letters[8][8] = "c";    letters[9][8] = "l";    letters[10][8] = "r";   letters[11][8] = "s";
        letters[6][9] = "b";    letters[7][9] = "r";    letters[8][9] = "d";    letters[9][9] = "p";    letters[10][9] = "t";   letters[11][9] = "k";
        letters[6][10] = "o";   letters[7][10] = "o";   letters[8][10] = "e";   letters[9][10] = "p";   letters[10][10] = "y";  letters[11][10] = "e";
        letters[6][11] = "n";   letters[7][11] = "l";   letters[8][11] = "u";   letters[9][11] = "a";   letters[10][11] = "a";  letters[11][11] = "s";


        //searchAcross();
        //searchDown();
    }

    //A method used to search across the grid for words
    public static void searchAcross(){
        for (int i =0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                for (int k = 0; k < words.size(); k++){
                    if (words.get(k).contains(letters[i][j]) && (words.get(k).contains(letters[i][j+1])) && (words.get(k).contains(letters[i][j+2]))){
                        System.out.println(words.get(k));
                        }
                    }
                }
            }
        }

    // A method used to search down the grid for words
    public static void searchDown(){
        for (int j =0; j < 12; j++) {
            for (int i = 0; i < 12; i++) {
                for (int k = 0; k < words.size(); k++){
                    if (words.get(k).contains(letters[j][i]) && (words.get(k).contains(letters[j][i+1])) && (words.get(k).contains(letters[j][i+2]))){
                        System.out.println(words.get(k));
                        }
                    }
                }
            }
    }
}

WordSearchSwing Class:

import javax.swing.*;
public class WordSearchSwing {

    private JPanel contentPane;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WordSearchSwing frame = new WordSearchSwing();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public WordSearchSwing() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        Object[] title = {"Happy Halloween"};
        table = new JTable(WordSearch.letters, title);
        contentPane.add(table, BorderLayout.CENTER);
    }

}
5
  • Dear mother of god ! please tell me this initialization phase was automatically generated Commented Nov 7, 2012 at 15:09
  • yes I am using window builder Commented Nov 7, 2012 at 15:13
  • haha yeah seems like a nightmare to do it manually (been a long time since I coded with Swing) Commented Nov 7, 2012 at 15:14
  • How exactly it doesn't work? Commented Nov 7, 2012 at 15:16
  • It doesnt give me an error but when I actually run it it just gives 12 blank cells in the JTable. Commented Nov 7, 2012 at 15:39

3 Answers 3

3

To expand on @Hakan's answer:

When you run your code, it'll start by running one (and only one) main method. So if you see the GUI, it ran the main method of WordSearchSwing. This means that the main method of WordSearch was not run.

The easiest (but definitely not the best) way to fix this is to add WordSearch.main(new String[]{}); before you create the JTable.

What it looks like you're trying to use for WordSearch in this instance is a Singleton pattern. This would be a much better solution than the above.

Sign up to request clarification or add additional context in comments.

Comments

2

WordSearch.letters is initialized in WordSearch.main(...) method and WordSearch.main() method is never called.

Note: There are a number of improvement opportunities in this code, but I have answered the question only.

Comments

2

The fast track to get it running would be to

1) Change in WordSearch from

public static void main(String[] args) {

to static initialization:

static  {
    //populating the arrayList with Strings, some which are in the wordsearch and some which aren't
    words.add("ghost");     words.add("knife");     words.add("patch");     words.add("ghoul");     words.add("apple");

Next in WordSearchSwing you need to change the code to

    Object[] title = {"First","2nd","r3d","4th","5th","6th"};
    table = new JTable(WordSearch.letters, title);
    JScrollPane scrollPane = new JScrollPane(table);
    contentPane.add(scrollPane, BorderLayout.CENTER);

to see the first 6 columns, add more as you like, and to also display the header.

You would then get something like this:

Running code

1 Comment

Mind blown. +1 for teaching me something new (static initialization).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.