Como habilito barra de rolagem em JLabel?

5 respostas
G

Como posso habilitar a barra de rolagem em um JLabel???

Tenho o seguinte...

public void criaJanela() throws FileNotFoundException{
		JPanel panel = new JPanel();
		JLabel labelTexto = new JLabel();
		//Scroll

		
		labelTexto.setText(mostraArquivo());
		panel.add(labelTexto);
		//panel.add(sp);
		
		getContentPane().add(panel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        pack();
        setVisible(true);
		
	}

o metodo mostraArquivo retorna um arquivo texto qq....

5 Respostas

dudaskank

vc deve utilizar o JScrollPane do swing… coloque ele no panel e o label no JScrollPane que vc criar.

espero que ajude :slight_smile:

G

Não funcionou... eu já havia tentado...

public class SwingFile extends JFrame {

	String texto = "<html>";
	JFrame frame;
	
	SwingFile()
    {
        super("Arquivo X");
    }
	
	
	public String mostraArquivo() throws FileNotFoundException {
		File file = new File("system.log");
		Scanner sc = new Scanner(file);
		
		
		while (sc.hasNextLine()) {
			
			texto = texto + sc.nextLine() + "<br>";
			
		}
		return texto + "</html>";
		
	}
	
	public void criaJanela() throws FileNotFoundException{
		JPanel panel = new JPanel();
		JLabel labelTexto = new JLabel();
		ScrollPane sp = new ScrollPane();

		
		labelTexto.setText(mostraArquivo());
		panel.add(labelTexto);
		panel.add(sp);
		
		
		getContentPane().add(panel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        pack();
        setVisible(true);
		
	}

		
}
S

1- Você não colocou o label no scrollpane.
2- Quando for colar um código, cola só a parte relevante dele.

G

Já tentei isso… dá erro!

S

Foi JScrollPane? E qual o erro?

Criado 20 de março de 2008
Ultima resposta 20 de mar. de 2008
Respostas 5
Participantes 3