Porque o list abaixo tem que ser final no metodo valueChanged? o eclipse pede para ser final.
package com.tutoriais.tuto;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Gui {
JTextArea area;
public void janela (){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
area = new JTextArea(10,10);
JScrollPane scroll = new JScrollPane(area);
JLabel texto = new JLabel("Titulo");
String [] nomes ={"Renan","Willamy","Bezerra","Renan","Willamy","Bezerra"};
JList list = new JList(nomes);
list.setVisibleRowCount(4);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
if(!list.getValueIsAdjusting()){ // por que este este list tem que ser final
area.append((String)list.getSelectedValue()+"\n");
}
}
});
JScrollPane scrollList = new JScrollPane(list);
JLabel texto2 = new JLabel("Titulo2");
JLabel texto3 = new JLabel("Titulo3");
panel2.add(scroll);
panel.setLayout(new BorderLayout());
panel.add(new BorderLayout().CENTER,panel2);
panel.add(new BorderLayout().NORTH,texto);
panel.add(new BorderLayout().WEST,texto2);
panel2.add(new BorderLayout().EAST,scrollList);
panel2.setBackground(Color.blue.cyan);
panel.setBackground(Color.blue.cyan);
JButton botao1 = new JButton("Click me");
frame.getContentPane().add(new BorderLayout().CENTER,panel);
frame.getContentPane().add(new BorderLayout().SOUTH,botao1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
Gui g = new Gui();
g.janela();
}
}