Como colocar uma barra de scroll no JTextArea?

2 respostas
B

Ola a todos.

Alguem poderia me explicar como eu posso ativar a scroll do meu JTextArea? eu fiz o codigo abaixo em uma visual class.

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;


public class CadastroPessoa extends JFrame {

	//private List<Pessoas> lista;
	
	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JButton botIncluir = null;

	private JButton botRemover = null;

	private JButton botBuscar = null;

	private JButton botListar = null;

	private JButton botSair = null;

	private JLabel labelNome = null;

	private JLabel labelIdade = null;

	private JTextField textoNome = null;

	private JTextField textoIdade = null;

	private JTextArea areaLista = null;

	private JButton botLimpar = null;

	/**
	 * This is the default constructor
	 */
	public CadastroPessoa() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(400, 300);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			labelIdade = new JLabel();
			labelIdade.setBounds(new Rectangle(151, 15, 107, 16));
			labelIdade.setText("Idade");
			labelNome = new JLabel();
			labelNome.setBounds(new Rectangle(15, 14, 107, 18));
			labelNome.setText("Nome");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getBotIncluir(), null);
			jContentPane.add(getBotRemover(), null);
			jContentPane.add(getBotBuscar(), null);
			jContentPane.add(getBotListar(), null);
			jContentPane.add(getBotSair(), null);
			jContentPane.add(labelNome, null);
			jContentPane.add(labelIdade, null);
			jContentPane.add(getTextoNome(), null);
			jContentPane.add(getTextoIdade(), null);
			jContentPane.add(getAreaLista(), null);
			jContentPane.add(getBotLimpar(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes botIncluir	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBotIncluir() {
		if (botIncluir == null) {
			botIncluir = new JButton();
			botIncluir.setBounds(new Rectangle(271, 21, 107, 33));
			botIncluir.setText("Incluir");

		}
		return botIncluir;
	}

	/**
	 * This method initializes botRemover	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBotRemover() {
		if (botRemover == null) {
			botRemover = new JButton();
			botRemover.setBounds(new Rectangle(270, 59, 106, 33));
			botRemover.setText("Remover");
		}
		return botRemover;
	}

	/**
	 * This method initializes botBuscar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBotBuscar() {
		if (botBuscar == null) {
			botBuscar = new JButton();
			botBuscar.setBounds(new Rectangle(269, 105, 107, 32));
			botBuscar.setText("Buscar");
		}
		return botBuscar;
	}

	/**
	 * This method initializes botListar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBotListar() {
		if (botListar == null) {
			botListar = new JButton();
			botListar.setBounds(new Rectangle(270, 150, 106, 32));
			botListar.setText("Listar");
		}
		return botListar;
	}

	/**
	 * This method initializes botSair	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBotSair() {
		if (botSair == null) {
			botSair = new JButton();
			botSair.setBounds(new Rectangle(271, 218, 107, 34));
			botSair.setText("Sair");
		}
		return botSair;
	}

	/**
	 * This method initializes textoNome	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTextoNome() {
		if (textoNome == null) {
			textoNome = new JTextField();
			textoNome.setBounds(new Rectangle(16, 37, 108, 19));
		}
		return textoNome;
	}

	/**
	 * This method initializes textoIdade	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTextoIdade() {
		if (textoIdade == null) {
			textoIdade = new JTextField();
			textoIdade.setBounds(new Rectangle(149, 36, 109, 20));
		}
		return textoIdade;
	}

	/**
	 * This method initializes areaLista	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getAreaLista() {
		if (areaLista == null) {
			areaLista = new JTextArea();
			areaLista.setBounds(new Rectangle(17, 64, 240, 181));
		}
		return areaLista;
	}
	
	private JButton getBotLimpar() {
		if (botLimpar == null) {
			botLimpar = new JButton();
			botLimpar.setBounds(new Rectangle(271, 189, 106, 23));
			botLimpar.setText("Limpar");
		}
		return botLimpar;
	}
	
}

2 Respostas

M
JScrollPane scroll = new JScrollPane(myTextArea);
myContainer.add(scroll);

:smiley:

B

Mathias Obrigado…mas a ideia seria usar o JScrollBar como ficaria?

private JScrollBar getScrollS() {
		if (scrollS == null) {
			scrollS = new JScrollBar();
			scrollS.setBounds(new Rectangle(237, 66, 21, 179));
		}
		return scrollS;
	}
Criado 26 de outubro de 2007
Ultima resposta 26 de out. de 2007
Respostas 2
Participantes 2