问大家个问题 我用对象数组构造了一个JLIST,有没有方法在这个对象数组后添加其他的对象数组

解决方案 »

  1.   

    好像没找到这样的方法。。不过我觉得应该可以将2个数组合并,然后调用
    JList的setListData方法重新构造
      

  2.   

    我是连SQLSERVER的时候进行查询,所以返回的记录数不确定,但是字段数数确定的,然后要求将返回的RESULTSET显示要JLIST上,要求格式是比如:
    1
    张三

    262
    李四

    273
    王五

    28
      

  3.   

    好像
    DefaultListModel这种数组类型有提供这样的添加方法 你的JList就用DefaultListModel构造
    JList jlist=new JList();
    DefaultListModel storeResult=new DefaultListModel();
    jlist.setModel(storeResult);
    storeResult.addElement(object obj)
    这里的obj可以指定为你查询结果的数组。我没有试过,你试试这样应该可以。用DefaultListModel你更新数据后JList中的数据也会同步更新,可以不用repaint();
      

  4.   

    恩,成功了,可是怎么给JList加上滚动条呢?
      

  5.   

    package system;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.sql.*;
    import java.io.*;
    /**
     * <p>Title: 信息系统</p>
     * <p>Description: 信息系统的查询和输出</p>
     * <p>Copyright: Copyright (c) 2007</p>
     * <p>Company: ACCP</p>
     * @author 刘海斌
     * @version 1.0
     */public class SystemFrame extends JFrame {
      JPanel contentPane;
      JScrollPane listScrollPane;
      JList jList1 = new JList();
      JTextField jTextField1 = new JTextField();
      JButton jButton1 = new JButton();
      JButton jButton2 = new JButton();
      ArrayList[] list;
      JLabel jLabel1 = new JLabel();
      JComboBox jComboBox1 = new JComboBox();
      StringBuffer sb = new StringBuffer();
      String[] str;  //Construct the frame
      public SystemFrame() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception  {
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(null);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        jTextField1.setText("");
        jTextField1.setBounds(new Rectangle(150, 226, 75, 30));
        jButton1.setBounds(new Rectangle(243, 234, 56, 24));
        jButton1.setText("查询");
        jButton1.addActionListener(new SystemFrame_jButton1_actionAdapter(this));
        jButton2.setBounds(new Rectangle(316, 234, 59, 24));
        jButton2.setText("导出");
        jButton2.addActionListener(new SystemFrame_jButton2_actionAdapter(this));
        jLabel1.setText("包含");
        jLabel1.setBounds(new Rectangle(115, 226, 27, 30));
        jComboBox1.setBounds(new Rectangle(28, 226, 79, 30));
        listScrollPane = new JScrollPane(jList1);
        listScrollPane.setBounds(new Rectangle(20, 11, 360, 200));
        contentPane.add(jTextField1, null);
        contentPane.add(jButton2, null);
        contentPane.add(jComboBox1, null);
        contentPane.add(jButton1, null);
        contentPane.add(jLabel1, null);
        contentPane.add(listScrollPane,null);
        String[] str = new String[]{"请选择","au_lname","au_fname","address","city"};
        addItem(jComboBox1,str);
      }
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  void jButton1_actionPerformed(ActionEvent e) {
        String str = (String)jComboBox1.getSelectedItem();
        String txt = jTextField1.getText();
        String sql = "select * from authors where "+ str +" like '%"+ txt +"%'";
        DateBase db = new DateBase("todb","sa","sa");
        ResultSet rs = db.executeQuery(sql);
        this.show(rs);
      }  private void addItem(JComboBox jcb,String[] str) {
        for (int i = 0; i < str.length; i++) {
          jcb.addItem(str[i]);
        }
      }  /*private void show(ResultSet rs) {
        try {
          ResultSetMetaData rsmd = rs.getMetaData();
          int count = rsmd.getColumnCount();
          ArrayList[] tmp = new ArrayList[count];
          for (int i = 0; i < count; i++) {
                tmp[i] = new ArrayList();
            }
          while (rs.next()) {
            for(int i=0;i<count;i++) {
              tmp[i].add(rs.getObject(i+1));
            }
          }
          putData(tmp);
          jList1.setListData(list);
        }
        catch (SQLException ex) {
          ex.printStackTrace();
        }
      }  private void putData(ArrayList[] tmp) {
        list = new ArrayList[tmp[0].size()] ;
        for(int i=0;i<list.length;i++) {
          list[i] = new ArrayList();
          for(int j=0;j<tmp.length;j++) {
            list[i].add(tmp[j].get(i));
          }
        }
      }*/
      private void show(ResultSet rs) {
        DefaultListModel storeResult=new DefaultListModel();
        jList1.setModel(storeResult);
        try {
          ResultSetMetaData rsmd = rs.getMetaData();
          int count = rsmd.getColumnCount();
          String[] tmp = new String[count];
          for (int i = 0; i <tmp.length; i++) {
                tmp[i] = new String();
            }
          while(rs.next()) {
            for(int i=0;i<tmp.length;i++) {
              tmp[i] = rs.getString(i+1)+"\n";
              sb.append(tmp[i]);
              storeResult.addElement(tmp[i]);
            }
          }
          System.out.println(sb);
        }
        catch (SQLException ex) {
          ex.printStackTrace();
        }
      }  void jButton2_actionPerformed(ActionEvent e) {
        File f = new File("d:/1.txt");
        try {
          if(!f.exists()) {
            f.createNewFile();
           }
          FileWriter fw = new FileWriter(f);
          BufferedWriter bw = new BufferedWriter(fw);
          str = sb.toString().split("\n");
          for(int i=0;i<str.length;i++) {
            bw.write(str[i]);
            bw.newLine();
          }
          bw.flush();
          bw.close();
          fw.close();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
      }
    }class SystemFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
      SystemFrame adaptee;  SystemFrame_jButton1_actionAdapter(SystemFrame adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
      }
    }class SystemFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
      SystemFrame adaptee;  SystemFrame_jButton2_actionAdapter(SystemFrame adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
      }
    }
      

  6.   

    package system;import java.sql.*;/**
     * <p>Title: 信息系统</p>
     * <p>Description: 信息系统的查询和输出</p>
     * <p>Copyright: Copyright (c) 2007</p>
     * <p>Company: ACCP</p>
     * @author not attributable
     * @version 1.0
     */public class DateBase {
      private String db;
      private String uname;
      private String pwd;
      private Connection conn;
      private Statement stat;
      DateBase(String db,String uname,String pwd) {
        this.db = db;
        this.uname = uname;
        this.pwd = pwd;
      }  public ResultSet executeQuery(String sql) {
        try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException ex) {
          ex.printStackTrace();
          return null;
        }
        try {
          conn = DriverManager.getConnection("jdbc:odbc:" + this.db, uname,pwd);
          if(conn==null) {
             return null;
          } else {
            stat = conn.createStatement();
            return stat.executeQuery(sql);
          }
        }
        catch (SQLException ex1) {
          ex1.printStackTrace();
          return null;
        }  }
    }