SoCo
SoCo

Reputation: 1954

Java - JTextArea into JList does not show the wanted text

Ok guys here is the deal, I am trying to populate a JList's rows using a JtextArea that contains a string value, but when I run my program all that is shown in the JList's rows are the JTextArea's properties(?) instead of the string I am passing. So, can you please take a look at my code and point out what i am missing?

Here is the Code:

public void DoConnect() {
    try{
             String host="jdbc:derby://localhost:1527/Project";
             String username="Adminn";
             String pass="password";
             Connection con = DriverManager.getConnection( host, username, pass );
             System.out.println("connected");

             Statement stmt = con.createStatement( );
             String SQL = "SELECT * FROM TEAMS";
             ResultSet rs=stmt.executeQuery(SQL);

             while(rs.next()){

             String s = rs.getString("NAME");

             JTextArea ta = new JTextArea();
             ta.setText(s);
             listModel.addElement(ta);
             }
             jList1.setModel(listModel);
             jButton1.addActionListener ((ActionEvent e) -> {
                listModel.removeAllElements();
                DoConnect();
             });

        }
        catch ( SQLException err ){
             System.out.println( err.getMessage( ) );
        }
}

And here is the output I am getting:

enter image description here

Upvotes: 0

Views: 524

Answers (2)

Voqus
Voqus

Reputation: 159

I believe you need JTable and not JTextArea for that purpose, correct me if i'm wrong.
For example, check this image

If you want to go with the JTable solution, there is a library called rs2xml.jar which makes your life easier to populate JTable with the data you wanted.

Usage:

Statement stmt = con.createStatement();
String SQL = "SELECT * FROM TEAMS";
ResultSet rs = stmt.executeQuery(SQL);
table.setModel(DbUtils.resultSetToTableModel(rs)); //this line requires the rs2xml.jar

Credits to: http://learnerarena.com for the picture.
Sorry, i could not comment due to low reputation.

Upvotes: 1

camickr
camickr

Reputation: 324197

The default renderer of a JList simply displays the toString() implementation of the object that you add to the model.

Don't add a JTextArea to the ListModel. Add the String to the ListModel

Also, why are you using "select * from Teams" when you only want a single column of data. Be more explicit with your SQL and make the query more efficient.

Edit:

Because the string I am trying to display has multiple lines.

Then you need to create a custom renderer and use the JTextArea as the renderer. In any case you would only ever add the String text to the ListModel. Read the section from the Swing tutorial on Writing a Custom Renderer.

Or a second option is to display the text as HTML. A JLabel will render the HTML in multiple line:

listModel.addElement("<html>1<br>2<br>3<br></html>");

Upvotes: 1

Related Questions