Search code examples
javaarraysswingjoptionpane

Display accommodated array with JOptionPane


I have this code and it runs just fine. The problem is that when I try to display it using showMessageDialog it shows everything in a messy way. Any advice on how to display it properly? Thanks.

String tablaColeccion;
tablaColeccion ="";
for (int i = 0; i < informeMatrices.getVector().length; i++){
    if (informeMatrices.getVector()[i] != null){
        tablaColeccion += informeMatrices.getVector()[i] + " ";
    }
}
tablaColeccion += "\n";

for (int i = 0; i < informeMatrices.getMatriz().length; i++) {
    for(int u = 0; u < informeMatrices.getMatriz().length; u++){
        if (informeMatrices.getMatriz()[i][u] != null){
            tablaColeccion += informeMatrices.getMatriz()[i][u] + " ";
        }
    }
    tablaColeccion += "\n"; 
}
tablaColeccion += "\n\n";

JOptionPane.showMessageDialog(null,tablaColeccion,"Coleccion Completa", JOptionPane.INFORMATION_MESSAGE);

I would like to display it as separated columns. Any help would be awesome.


Solution

  • I found this way to do it. It is pretty simple and easy to implement. This will accomodate everything into columns and rows just by simply using HTML.

    StringBuilder sb = new StringBuilder( 128);
    
            sb.append("<html><table><tr>");
    
            for (int i = 0; i < informeMatrices.getVector().length; i++)
            {
                if (informeMatrices.getVector()[i] != null)
                {
    
                    sb.append("<td>" + informeMatrices.getVector()[i] + "</td>");
                }
            }
    
            for (int i = 0; i < informeMatrices.getMatriz().length; i++) 
            {
                sb.append("<tr>");
    
                for(int u = 0; u < informeMatrices.getMatriz().length; u++)
                {
                    if (informeMatrices.getMatriz()[i][u] != null)
                    {                       
                        sb.append("<td>").append(informeMatrices.getMatriz()[i][u]).append("</td>");
                    }
    
                }
    
                sb.append("</tr>");
    
            }
    
            sb.append("</table></html>");
    
            JOptionPane.showMessageDialog(null,sb.toString(),"Coleccion Completa",
                    JOptionPane.INFORMATION_MESSAGE);