Last active
June 7, 2017 19:30
-
-
Save Blasanka/e1acca1221feaeb1d032820339e1bf4d to your computer and use it in GitHub Desktop.
This simple program I have created for you to see how to get all the values in the JList. everythig explained by the comments so that you can understand easily. Also if you want to get selected items in JList you find my gist. If you have any problems, comment below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
B Liyanage asanka - code snippets for you. | |
twitter - @bliyanageasanka | |
*/ | |
import java.awt.BorderLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.DefaultListModel; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JList; | |
import javax.swing.JScrollPane; | |
import javax.swing.WindowConstants; | |
public class Jlist { | |
//declare components. | |
JFrame frame; | |
JList<String> list; | |
JButton button; | |
//constructor | |
public Jlist(){ | |
// call the initialize method to create and show GUI | |
init(); | |
} | |
public void init(){ | |
//initialize main frame | |
frame = new JFrame("Sample"); | |
//frame size | |
frame.setSize(300, 300); | |
//layout - to put component in places. Here I used BorderLayout but there is lot. | |
frame.setLayout(new BorderLayout(30, 30)); | |
//create custom model | |
DefaultListModel<String> model = new DefaultListModel<>(); | |
model.addElement("A"); | |
model.addElement("B"); | |
model.addElement("C"); | |
model.addElement("D"); | |
model.addElement("E"); | |
//initialize the JList | |
list = new JList<>(); | |
//set the custom model that we created before | |
list.setModel(model); | |
//initialize scroll pane and add list in to it, so if list have lost of element you can scroll through. | |
JScrollPane pane = new JScrollPane(list); | |
pane.setViewportView(list); | |
//initialize the button | |
button = new JButton("Get Values"); | |
//add button click listener | |
button.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent evt) { | |
//call the method to get all values from the list | |
buttonActionPerformed(); | |
} | |
}); | |
frame.add(pane, BorderLayout.NORTH); | |
frame.add(button, BorderLayout.CENTER); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
//get the all values from this function | |
public void buttonActionPerformed(){ | |
for(int i = 0; i< list.getModel().getSize();i++){ | |
System.out.println(list.getModel().getElementAt(i)); | |
} | |
} | |
//main method | |
public static void main(String[] args) { | |
//instantiate frame window | |
new Jlist(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment