Evan12 Evan12 - 2 months ago 8
SQL Question

Line Wrapping for Jlist/next line for stringbuilder

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

Answer

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.

Comments