Java - JList, alguien tiene un ejemplo ?

 
Vista:
sin imagen de perfil
Val: 111
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

JList, alguien tiene un ejemplo ?

Publicado por Gonzalo (112 intervenciones) el 06/08/2015 20:09:55
encontre este ejemplo en este libro

Core Java™ Volume II–Advanced Features, Eighth Edition

funciona muy bien

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
 
 
public class AddingListenerToDocument extends JPanel
{
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				JFrame frame = new ListFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}
 
class ListFrame extends JFrame
{
	public ListFrame()
	{
		setTitle("ListTest");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		String[] words =
			{
				"quick", "brown", "hungry", "wild", "silent", "huge", "private",
				"abstract", "static", "final"
			};
 
		wordList = new JList(words);
		wordList.setVisibleRowCount(5);
		JScrollPane scrollPane = new JScrollPane(wordList);
 
		listPanel = new JPanel();
		listPanel.add(scrollPane);
		wordList.addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent event)
			{
				Object[] values = wordList.getSelectedValues();
				StringBuilder text = new StringBuilder(prefix);
				for(int i = 0; i < values.length; i++)
				{
					String word = (String) values[i];
					text.append(word);
					text.append(" ");
				}
				text.append(suffix);
				label.setText(text.toString());
			}
		});
 
	 buttonPanel = new JPanel();
	 group = new ButtonGroup();
	 makeButton("Vertical", JList.VERTICAL);
	 makeButton("Vertical Wrap", JList.VERTICAL_WRAP);
	 makeButton("Horizontal Wrap", JList.HORIZONTAL_WRAP);
 
	 add(listPanel, BorderLayout.NORTH);
	 label = new JLabel(prefix + suffix);
	 add(label, BorderLayout.CENTER);
	 add(buttonPanel, BorderLayout.SOUTH);
 }
 
 /**
 * Makes a radio button to set the layout orientation.
 * @param label the button label
 * @param orientation the orientation for the list
 */
 private void makeButton(String label, final int orientation)
 {
	 JRadioButton button = new JRadioButton(label);
	 buttonPanel.add(button);
	 if (group.getButtonCount() == 0) button.setSelected(true);
	 group.add(button);
 
	 button.addActionListener(new ActionListener()
	 {
		 public void actionPerformed(ActionEvent event)
		 {
			 wordList.setLayoutOrientation(orientation);
			 listPanel.revalidate();
			}
		});
	}
 
	 private static final int DEFAULT_WIDTH = 400;
	 private static final int DEFAULT_HEIGHT = 300;
	 private JPanel listPanel;
	 private JList wordList;
	 private JLabel label;
	 private JPanel buttonPanel;
	 private ButtonGroup group;
	 private String prefix = "The ";
	 private String suffix = "fox jumps over the lazy dog.";
}

pero trabaja con una lista fija, yo necesito poder quitar, agregar o inclusive borrar y empezar de nuevo, en otras palabras, necesito poder cambiar los elementos o borrarlos si asi se necesita y que muestre los cambios en ese momento.

no necesito modificar el list para que se muestre por columnas, solo necesito el procedimiento para quitar y agregar elementos.

la opcion como la estoy manejando no borra la lista, los elementos se quiedan siempre visibles aunque los borre, lo cual no es correcto.

alguien tiene un ejemplo sencillo? yo lo ajustaria para lo que necesito.

gracias, salu2.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

JList, alguien tiene un ejemplo ?

Publicado por jack (3 intervenciones) el 08/08/2015 06:14:55
Hola.
Lista dinamica.
list-combobox-1400120 Subido en subir imagenes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class ListViewSample extends Application {
 
    public static final ObservableList names =
        FXCollections.observableArrayList();
    public static final ObservableList data =
        FXCollections.observableArrayList();
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("List Simple");
 
        final ListView listView = new ListView(data);
        listView.setPrefSize(200, 250);
        listView.setEditable(true);
 
        names.addAll(
             "Adam", "Alex", "Alfredo", "Alberto",
             "Brenda", "Carlos", "Devora", "Don Jose",
             "Luis", "Martha", "Rosa", "Rudolfo",
             "Tony", "Poncho", "Selena", "Casimiro"
        );
 
        for (int i = 0; i < 18; i++) {
            data.add("anonimo");
        }
 
        listView.setItems(data);
        listView.setCellFactory(ComboBoxListCell.forListView(names));
 
        StackPane root = new StackPane();
        root.getChildren().add(listView);
        primaryStage.setScene(new Scene(root, 200, 250));
        primaryStage.show();
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar