JList成为JTextArea

时间:2012-04-22 14:47:47

标签: java swing jlist jtextarea

好的,这是我在JAVA中的第一个项目(银行应用程序),我想要的是显示从数据库中获取的结果并将其设置为JTextArea。但是,我希望银行职员能够点击JTextArea中的一行并执行某些操作(一旦我在JTextArea中获得结果,我将负责);这就是我使用JList的原因。我已经到了能够在不使用JList的情况下在JTextArea中显示结果的观点。我的问题是:

  • 如果可能,我如何将JList放入JTextArea?

以下是我的代码的一部分:

// function that will search for a record in the database
private void searchRecord(){


    try {

        Connection connect = null;

        Class.forName("com.mysql.jdbc.Driver");

        String url = "url";
        String user = "user";
        String password ="password";

        connect = DriverManager.getConnection(url,user,password);

        Statement SQLStatement = connect.createStatement();
        String select = "SELECT * FROM customerinfo WHERE LastName LIKE '"+ txtSearch.getText().trim() +"%'";
        ResultSet rows = SQLStatement.executeQuery(select);

        while(rows.next()){

            textArea.setLineWrap(true);

            String first = rows.getString("FirstName");
            String last = rows.getString("LastName");
            String middleInitial = rows.getString("MiddleInitial");
            String[] row = {first, last, middleInitial};
            list = new JList(row);
            list.setVisibleRowCount(5);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            //textArea.append(row[1].toString() + ", " + row[0].toString() + " " + row[2].toString() + ".\n");
            textArea.append(list values goes here + "\n"); // here is where I'm stuck!!!
            //System.out.println(rows.getString("LastName"));
        }

先谢谢!!!

1 个答案:

答案 0 :(得分:0)

//Dont put list to JTextArea.You can put data into directly

String first = rows.getString("FirstName");
String last = rows.getString("LastName");
String middleInitial = rows.getString("MiddleInitial");


JTextArea txaDetails=new JTextArea();
txaDetails.append("\n"+first +last +middleInitial +"\n");
txaDetails.setCaretPosition(txaDetails.getDocument().getLength());