Search code examples
javaswingjlistjtextarea

JList into a JTextArea


Okay, this is my first project(bank application) in JAVA, and all I want is to display the results taken from a database and set it into a JTextArea. However, I want the bank clerk to be able to click on a line in the JTextArea and do certain actions(I will take care of that once I get the results in the JTextArea); that's why I'm using a JList. I have gotten to the point that I was able to show the results in the JTextArea without using the JList. My question is:

  • If possible, how can I put a JList into a JTextArea?

Here is part of my code:

// 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"));
        }

Thanks in advanced!!!


Solution

  • //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());