Jlist não cria barra de rolagem

4 respostas
E

Sou iniciante no java e estou tentando fazer o exercício 13 do curso de Gustavo Guanabara, usando o eclipse. Estou acompanhando o programa que ele faz, o dele a lista quanto ultrapassa o número máximo de visualização cria automaticamente a barra de rolagem. Já tentei vários procedimentos, mas não consegui. Agradeço por qualquer ajuda,

public class Exercicio13 {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
		public void run() {
			try {
				Exercicio13 window = new Exercicio13();
				window.frame.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	});
}

/**
 * Create the application.
 */
public Exercicio13() {
	initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
	frame = new JFrame();
	frame.setBounds(100, 100, 450, 300);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(null);
	
	JLabel lblNewLabel = new JLabel("Inicio");
	lblNewLabel.setBounds(24, 11, 46, 14);
	frame.getContentPane().add(lblNewLabel);
	
	JLabel lblNewLabel_1 = new JLabel("Fim");
	lblNewLabel_1.setBounds(24, 60, 46, 14);
	frame.getContentPane().add(lblNewLabel_1);
	
	JLabel lblNewLabel_2 = new JLabel("Passo");
	lblNewLabel_2.setBounds(24, 109, 46, 14);
	frame.getContentPane().add(lblNewLabel_2);
	
	JLabel lblInicio = new JLabel("0");
	JLabel lblFim = new JLabel("6");
	JLabel lblPasso = new JLabel("1");
			
	JList lstCont = new JList(); 
	new JScrollPane(lstCont);
	
	JSlider sliInicio = new JSlider();
	sliInicio.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent e) {
			int i = sliInicio.getValue();
			lblInicio.setText(Integer.toString(i));
			
		}
	});
			
	sliInicio.setValue(0);
	sliInicio.setMaximum(5);
	sliInicio.setBounds(78, 11, 200, 26);
	frame.getContentPane().add(sliInicio);
	
	JSlider sliFim = new JSlider();
	sliFim.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent e) {
		int f = sliFim.getValue();
		lblFim.setText(Integer.toString(f));
		}
	});
	sliFim.setMinimum(6);
	sliFim.setValue(6);
	sliFim.setBounds(80, 60, 200, 26);
	frame.getContentPane().add(sliFim);
	
	JSlider sliPasso = new JSlider();
	sliPasso.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent e) {
			int p = sliPasso.getValue();
			lblPasso.setText(Integer.toString(p));
		}
	});
	sliPasso.setMaximum(4);
	sliPasso.setMinimum(1);
	sliPasso.setValue(1);
	sliPasso.setBounds(78, 109, 200, 26);
	frame.getContentPane().add(sliPasso);
	
	
	lblInicio.setBounds(288, 11, 46, 14);
	frame.getContentPane().add(lblInicio);
	
	
	lblFim.setBounds(288, 60, 46, 14);
	frame.getContentPane().add(lblFim);
	
	
	lblPasso.setBounds(288, 109, 46, 14);
	frame.getContentPane().add(lblPasso);
	
	JButton btnCont = new JButton("Contar");
	btnCont.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			int i = sliInicio.getValue();
			int f = sliFim.getValue();
			int p = sliPasso.getValue();
			
			DefaultListModel lista = new DefaultListModel();
			
			for (int c = i; c<=f; c += p) {
				lista.addElement(c);					
			}
			
			lstCont.setModel(lista);
			
		}
	});
	btnCont.setBounds(24, 170, 89, 23);
	frame.getContentPane().add(btnCont);
	
	
	lstCont.setBounds(252, 146, 83, 86);
	frame.getContentPane().add(lstCont);
}

}

4 Respostas

D

Tem que adicionar o ScrollPane com o List:

// frame.getContentPane().add(lstCont);
frame.getContentPane().add(new JScrollPane(lstCont));
E

Ocorreu que não criou a lista… :sob:

D

Acho que faltou definir o tamanho e posição do ScrollPane:

// lstCont.setBounds(252, 146, 83, 86);
// frame.getContentPane().add(lstCont);

JScrollPane lstContScrollPane = new JScrollPane(lstCont)
lstContScrollPane.setBounds(252, 146, 83, 86);
frame.getContentPane().add(lstContScrollPane);
E

Agora deu certo!!!
Muito obrigado!!!

Criado 1 de maio de 2020
Ultima resposta 1 de mai. de 2020
Respostas 4
Participantes 2