How to create a scroll bar?

0

Well I have a problem, I have 2 methods, one that counts the lines of a .txt file and another method that creates an array of the file lines so that I can print it in a JOptionpane.messagedialog

1st method

//metodo que devuelve el numero de lineas del archivo case 6
public int mostrarfarmacia(InputStream is) throws IOException{
    int n=0;
    InputStreamReader ir = new InputStreamReader(is);
    BufferedReader bs = new BufferedReader(ir);
    String linea = bs.readLine();
    while(linea!=null) {
        linea = bs.readLine();
        n++;
    }
    bs.close();
    return n;
}

2nd method

//metodo que imprime el archivo faramcia case 6
public void mostrarfarmacia1(InputStream is, int n) throws IOException{
    int i = 0;
    InputStreamReader ir = new InputStreamReader(is);
    BufferedReader bs = new BufferedReader(ir);
    String[] lineas = new String[n];
    String linea = bs.readLine();
    while(linea!=null) {
        lineas[i] = linea;
        linea = bs.readLine();
        i++;
    }
    bs.close();
    JOptionPane.showMessageDialog(null, lineas);
}

My problem is that when printing the array with the contents of the file, if the file is long, when printing it in the JOptionpane.showmessagedialog it prints a part and the rest prints it out of the screen, which makes it impossible to visualize it, I would like to know if there is a way to be able to visualize everything, or I would have to use other functions than the JOptionpane.showmessagedialog

    
asked by lalo hernandez 23.05.2018 в 08:30
source

1 answer

0

In a JOptionPane you can use a jlabel to show the text to mark the width and length of the window you can do with html and css

JOptionPane.showMessageDialog(
    this, 
    "<html><body><p style='width: 200px;'>"+"mensaje a imprimir en la Jlabel"+"</p></body></html>",  // Contenido + Css
    "Este es el título",   // Título
    JOptionPane.ERROR_MESSAGE // Tipo deJOptionPane
); 

If you need more space or you do not like the length and width of the screen that you occupy you can use a JscrollPane and inside a Jlabel that is the same but having above the Jlabel the JscrollPane

More information on html in components: Here

and here is an example of using CSS in Swing: Here

Additionally, if you use java 1.7 or higher I recommend using the try with resources so that java manages the issue of closing the resources used and you do not have to be using the .close ()

    
answered by 23.05.2018 в 10:27