0

I have a database that populates a jList so that every column of each row is displayed as 1 jList item. I have set the vertical scrollbar as needed and horizontal never which worked, however

  1. It is not "line wrapping", the line of text is going too far horizontally so it is not all visible.

  2. I would like the columns to be vertically listed such as:

COLUMN1: column1 info
COLUMN2: column1 info
COLUMN3: column1 info
COLUMN4: column1 info

instead, as I said it is horizontal such as:

COLUMN1: column1 info COLUMN2: column2 info COLUMN3: column3 info

For this I have tried the .append("\n") and the .append(System.getProperty("line.separator"));, neither worked.

What is the best way to go about fixing one or both of these issues?

Thanks!

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    DefaultListModel model = new DefaultListModel();
    jList1.setModel(model);
    final String SPACE = " ";
    StringBuilder sBuilder = new StringBuilder();
    String sqlQuery = "select Column1, Column2, Column3, Column4, Column5 from APP.DATA123 "
            + "where (Column1 = ?) AND (Column2 = ?) AND (Column3= ?) OR (Column2 = ?) AND (Column3 = ?)";


        String abc = jTextField2.getText();
        String cba = (String)jComboBox1.getSelectedItem();
        String cab = (String)jComboBox2.getSelectedItem();
        String data = "jdbc:derby://localhost:1527/sample";
    try (
          Connection conn = DriverManager.getConnection(
          data, "app", "app");
          PreparedStatement st = conn.prepareStatement(sqlQuery))   { 

      Class.forName("org.apache.derby.jdbc.ClientDriver");
      st.setString(1, abc);
      st.setString(2, cba);
      st.setString(3, cab);       
      st.setString(4, cba);
      st.setString(5, cab);
      ResultSet rec = st.executeQuery();


        while (rec.next())  {
         sBuilder.setLength(0);
        sBuilder.append(rec.getString("Column1")).append(SPACE)
                 .append(rec.getString("Column2")).append(SPACE)
                 .append(rec.getString("Column3")).append(SPACE)
                 .append(rec.getString("Column4")).append(SPACE)
                 .append(rec.getString("Column5")).append(SPACE);

         model.addElement(sBuilder.toString());         
        }
      st.close();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Evan12
  • 19
  • 7
  • 1
    "Offtopic:" you might want to look into "Clean code" by Robert Martin. Your code could benefit from that. **A lot**. You are doing way too many things within that poor method. Seriously: horrible style. – GhostCat Sep 29 '16 at 19:06
  • I will look into it if I have time, give me a break, I am not a software person I am a pharmacist and learned java in 1 month LoL. I read "Sam's Teach Yourself Java in 21 days". – Evan12 Sep 29 '16 at 20:07

2 Answers2

1

It is not "line wrapping", the line of text is going too far horizontally so it is not all visible. ...For this I have tried the .append("\n") and the .append(System.getProperty("line.separator"));, neither worked.

JList uses a JLabel as its default renderer, which means new lines will not be rendered. You could go about rendering the content with html, but given...

would like the columns to be vertically listed such as

...you might consider using a JTable.

Community
  • 1
  • 1
copeg
  • 8,290
  • 19
  • 28
1

and horizontal never

If you line is too large then you don't want to use never. You want to let the JList determine when scrolling is required.

I would like the columns to be vertically listed such as:

Don't append the data.

Instead you need to add 5 separate items to the JList. Then each item will appear on a new row.

camickr
  • 321,443
  • 19
  • 166
  • 288