简体   繁体   English

Java-进入JList的JTextArea不显示所需文本

[英]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. 好的,这是交易,我正在尝试使用包含字符串值的JtextArea填充JList的行,但是当我运行程序时,JList的行中显示的所有内容都是JTextArea的properties(?)而不是字符串I正在过去。 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: 这是我得到的输出:

在此处输入图片说明

The default renderer of a JList simply displays the toString() implementation of the object that you add to the model. JList的默认渲染器仅显示添加到模型中的对象的toString()实现。

Don't add a JTextArea to the ListModel. 不要将JTextArea添加到ListModel中。 Add the String to the ListModel String添加到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. 使用SQL更明确,使查询更高效。

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. 然后,您需要创建一个自定义渲染器,并将JTextArea用作渲染器。 In any case you would only ever add the String text to the ListModel. 无论如何,您只能将String文本添加到ListModel中。 Read the section from the Swing tutorial on Writing a Custom Renderer . 阅读Swing教程中有关编写自定义渲染器的部分

Or a second option is to display the text as HTML. 或者第二种选择是将文本显示为HTML。 A JLabel will render the HTML in multiple line: JLabel将在多行中呈现HTML:

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

I believe you need JTable and not JTextArea for that purpose, correct me if i'm wrong. 我相信您需要JTable而不是JTextArea,如果我错了,请纠正我。
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. 如果要使用JTable解决方案,则有一个名为rs2xml.jar的库,该库使您可以轻松地用所需的数据填充JTable。

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. 图片来源: http//learnerarena.com
Sorry, i could not comment due to low reputation. 抱歉,由于声誉低下,我无法发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM