简体   繁体   中英

Java - 2 dimensional array in JOptionPane how to show it all at once

I have a two dimensional array, which i want to show via a JOptionPane. So far it is showing me 1 row at a time. But i would like it to show all 8 rows at once.

It is also showing brackets and comma's in the JOptionPane once i run the code. Is there some way to get rid of those brackets and comma's?

This is my code so far, im just started learning Java.

package indzendopgave2;
import javax.swing.JOptionPane;

import java.util.Arrays;

public class Inzend2 {

public static void main(String[] args) {
    //Creating array
    int[][] blastTable = new int[][]{
            {32,31,30,29,28,27,26,25},
            {24,23,22,21,20,19,18,17},
            {16,15,14,13,12,11,10,9},
            {8,7,6,5,4,3,2,1},
            {0,-1,-2,-3,-4,-5,-6,-7},
            {-8,-9,-10,-11,-12,-13,-14,-15},
            {-16,-17,-18,-19,-20,-21,-22,-23},
            {-24,-25,-26,-27,-28,-29,-30,-31}
    };

    printArray(blastTable);
}



//Method to print two dimensional array in a JOptionPane
public static void printArray(int[][] num1){

    for(int x=0; x<num1.length; x++){
        String output = Arrays.toString(num1[x]);
            JOptionPane.showMessageDialog(null, output, "Uitvoer",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

If you want to show the whole array all at once, you need not use a loop at all.

   String output = Arrays.deepToString(num1);
   JOptionPane.showMessageDialog(null, output, "Uitvoer",
                    JOptionPane.INFORMATION_MESSAGE);

If you want to remove the , and [] , you have to parse the array to a string and format it as you like.

This will also do

  public static void printArray(int[][] num1) {
    String output = "";
    for (int x = 0; x < num1.length; x++) {
        output += Arrays.toString(num1[x]) + "\n";
    }
    JOptionPane.showMessageDialog(null, output, "Uitvoer",
            JOptionPane.INFORMATION_MESSAGE);
}

Use the best tool for the job, and for displaying tabular data in Swing, this means creating a JTable. I'd create a DefaultTableModel object with your data, put the model into a JTable, and then display the JTable in your JOptionPane. Solved.

在此处输入图片说明

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.util.Arrays;

public class Inzend2 {

   public static void main(String[] args) {
      // Creating array
      int[][] blastTable = new int[][] { { 32, 31, 30, 29, 28, 27, 26, 25 },
            { 24, 23, 22, 21, 20, 19, 18, 17 },
            { 16, 15, 14, 13, 12, 11, 10, 9 }, { 8, 7, 6, 5, 4, 3, 2, 1 },
            { 0, -1, -2, -3, -4, -5, -6, -7 },
            { -8, -9, -10, -11, -12, -13, -14, -15 },
            { -16, -17, -18, -19, -20, -21, -22, -23 },
            { -24, -25, -26, -27, -28, -29, -30, -31 } };

      printArray(blastTable);
   }

   // Method to print two dimensional array in a JOptionPane
   public static void printArray(int[][] num1) {
      String[] columnNames = { "A", "B", "C", "D", "E", "F", "G", "H" };

      // table models expect reference type data, and so
      // int array must be changed to an Integer array
      Integer[][] data = new Integer[num1.length][num1[0].length];
      for (int i = 0; i < data.length; i++) {
         for (int j = 0; j < data[i].length; j++) {
            data[i][j] = num1[i][j];
         }
      }
      DefaultTableModel model = new DefaultTableModel(data, columnNames);
      JTable table = new JTable(model);
      JScrollPane scrollPane = new JScrollPane(table);

      JOptionPane.showMessageDialog(null, scrollPane, "Uitvoer",
            JOptionPane.INFORMATION_MESSAGE);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM