-2

I am trying to display an arraylist in a gui. However I am getting some troubles. I need to check if my game is legal, if it is not legal then it calls getProblems which displays the arraylist. I tried to call getProblems directly in the GUI class however it will show the array as empty. (Since it's not checking if it's legal). I also tried to call isLegal then getProblems but you cannot do this in a JOptionPane. Any tips on how I can call it accross?

GetProblems class

 protected List < String > getProblems() {
     return displayOutput;
 }

IsLegal Class

public boolean isLegal() {
  boolean legality;

  if (checkRowConstraints().isEmpty()) {
    legality = true;

  } else {
    getProblems();
    legality = false;

  }
  return legalCheck;
}

GUI:

 public void actionPerformed(ActionEvent e) {
     if (!(puzzle.isLegal())) {
       JOptionPane.showMessageDialog(FutoshikiFrame.this,
         puzzle.getProblems(),
         "You made a mistake!",
         JOptionPane.INFORMATION_MESSAGE);

Here is difference between Actual display of GUI result and result i'm trying to get.

Actual display of GUI to result and result i'm trying to get

Further Problem found: I need to return the arraylist then empty it. Fixed

  • I'm surprised that even compiles, because you are attempting to return a `String` when you've specified your return type as `List` in `getProblems()`. – TEK May 08 '16 at 15:53
  • @TEK Adjusted, when I pasted my code into stackoverflow i forgot to remove that top line (Was formatting the array so it just prints the characters inside the square brackets only, eg: "Hi" Instead of "[Hi]") – LogicGates May 08 '16 at 15:57

1 Answers1

2

Presently, there is an implicit call to the List's toString() method when you call puzzle.getProblems() within the JOptionPane. So rather than get the contents of the List, which is what you want, you're getting whatever toString() is giving you.

You're not going to get the contents of the List unless you iterate over it first.

You could try something like this. (Note, this is untested code. For demo purposes.)

String formattedString = "";
//let's iterate over our List and build a formatted string for output
for(String element : puzzle.getProblems())
{
    formattedString += element;
}

Then you can output this formatted String

 public void actionPerformed(ActionEvent e) {
     if (!(puzzle.isLegal())) {
       JOptionPane.showMessageDialog(FutoshikiFrame.this,
         formattedString,
         "You made a mistake!",
         JOptionPane.INFORMATION_MESSAGE);
TEK
  • 1,265
  • 1
  • 15
  • 30