[JavaSwing] JList in Java – JList in Java
Content
Create and capture events in JList
Additions element in JList
JList allows to display a list of objects and groups. In this lesson we will learn the basics about it.
Create and capture events in JList
We will create a program that contains 1 JList including color, click to select a color, the background color of the JFrame will change the color name.
package nguyenvanquan7826.JList; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class JListDemo extends JFrame { private int width = 200; private int height = 250; // create array string color to show in list String[] colorName = { "blue", "green", "red" }; // create array color Color[] backGroundColor = { Color.blue, Color.green, Color.red }; JList<String> myJList; // create JList width array color public JListDemo() { // set layout for contenPane getContentPane().setLayout(new FlowLayout()); // add list color add(createJList()); // set display setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(width, height); setLocationRelativeTo(null); setVisible(true); } private JList createJList() { // create list myJList = new JList<String>(colorName); // add listen to get action when select item in JList myJList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { changeBackGround(); } }); return myJList; } // change background JFrame private void changeBackGround() { getContentPane().setBackground( backGroundColor[myJList.getSelectedIndex()]); } public static void main(String[] args) { new JListDemo(); } }
* The constructor method of JList:
– JList(): create an empty JList
– JList(And[] listData): create 1 JList with an array of data
– JList(ListModel dataModel): JList to create a model has
– JList(Vector listData): JList with data created in vector
* Catching Event when selecting an item in the list we add methods addListSelectionListener for JList. Then will override methods value changed to perform the desired. Using methods getSelectedIndex for selected locations in JList
* Attention in the above example because we use directly JFrame should want to change the background color of the frame we need setLayout for JFrame and when performing background instead need to make call getContentPane().setBackground.
Additions element in JList
The next example below will accomplish more in less element JList. JFrame containing 1 JPanel (main panel), This panel is the main pharmaceutical setLayout BorderLayout and contains objects in the picture below:
When Selecting from the JList and then press Delete or Add to delete the selected element or elements in addition to the JList JTextFiel.
package nguyenvanquan7826.JList; import java.awt.BorderLayout; import java.awt.GridLayout; 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.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class JListAddOrRemoveItem extends JFrame implements ActionListener { private int width = 200; private int height = 250; private JTextField tfAdd; private JList<String> myList; public JListAddOrRemoveItem() { // add main panel add(createMainPanel()); // display setTitle("Add - remove item in JList"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(width, height); setLocationRelativeTo(null); setVisible(true); } private JPanel createMainPanel() { // create main panel JPanel panel = new JPanel(new BorderLayout()); // set border empty for main panel panel.setBorder(new EmptyBorder(0, 10, 10, 10)); // add content panel.add(createDeletePanel(), BorderLayout.PAGE_START); panel.add(createAddPanel(), BorderLayout.PAGE_END); panel.add(createList(), BorderLayout.CENTER); return panel; } // delete panel in top of frame private JPanel createDeletePanel() { JPanel panel = new JPanel(); panel.add(createButton("Delete")); return panel; } // add panel in bottom of frame private JPanel createAddPanel() { JPanel panel = new JPanel(new GridLayout(0, 1)); panel.add(tfAdd = new JTextField()); panel.add(createButton("Add")); return panel; } // create Jlist private JList<String> createList() { // create defaultListModel DefaultListModel<String> model = new DefaultListModel<>(); // add element to model model.addElement("Java"); model.addElement("C#"); // set model to JList myList = new JList<String>(model); return myList; } // create a button private JButton createButton(String btnName) { JButton btn = new JButton(btnName); btn.addActionListener(this); return btn; } private void deleteItem() { // get model of Jlist DefaultListModel<String> model = (DefaultListModel<String>) myList .getModel(); // delete item is selected if (!model.isEmpty() && myList.getSelectedIndex() >= 0) { model.remove(myList.getSelectedIndex()); } // set model for JList myList.setModel(model); } private void addItem() { String item = tfAdd.getText().trim(); if (item.equals("")) { JOptionPane.showMessageDialog(null, "Pleas enter item add to Jlist", "Error", JOptionPane.ERROR_MESSAGE); } else { DefaultListModel model = (DefaultListModel) myList.getModel(); model.addElement(item); myList.setModel(model); tfAdd.setText(""); } } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "Delete") { deleteItem(); return; } if (e.getActionCommand() == "Add") { addItem(); } } public static void main(String[] args) { new JListAddOrRemoveItem(); } }
In this example JList is initialized with 1 model is DefaultListModel, That is 1 model that Java built for us, it is very easy to use. The additions to the JList mandatory element we have done through the JList model can not directly be (Unless every time you add or remove users 1 list (or array) molecules and then re-create the JList). Details of these steps are clearly annotate in the code, you keep track of which.
Read more: use JList, class JList, custom JList
có cách nào để setEnable(false) 1 item cụ thể trong jlist không admin
Mình cũng chưa thử. 🙂
chào ad, cho mình hỏi trong jlist khi muốn click vào một item có thể gọi frame khác làm ntn ạ! CEO: have 1 list phòng ban khi click dô một item bất kì thì sẽ gọi đc tới frame nhân viên
I also have not tried this one. Lâu rồi cũng không động lại nó. ^^ Bạn thử tìm google xem nhé.
admin có code tính cộng trừ nhân chia sử dụng JList không? cho em xin với