JListTest.java
上传用户:xinkaifrp
上传日期:2021-05-08
资源大小:372k
文件大小:3k
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * JListTest.java
- *
- * Created on 2009-6-26, 11:20:27
- */
- package test;
- import javax.swing.DefaultListModel;
- import javax.swing.ListModel;
- /**
- *
- * @author Administrator
- */
- public class JListTest extends javax.swing.JFrame {
- /** Creates new form JListTest */
- public JListTest() {
- initComponents();
- }
- public void setValue(){
- DefaultListModel model=new DefaultListModelImpl();
- model.addElement("hello");
- model.addElement("hi");
- jList1.setModel(model);
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- jList1 = new javax.swing.JList();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jList1.setModel(new javax.swing.AbstractListModel() {
- String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
- public int getSize() { return strings.length; }
- public Object getElementAt(int i) { return strings[i]; }
- });
- jScrollPane1.setViewportView(jList1);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(45, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(45, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- JListTest t=new JListTest();
- t.setValue();
- t.setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JList jList1;
- private javax.swing.JScrollPane jScrollPane1;
- // End of variables declaration//GEN-END:variables
- private static class DefaultListModelImpl extends DefaultListModel {
- public DefaultListModelImpl() {
- }
- }
- }