Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

swing application



I am writting an swing application.
After querying from db, I have to display the result in the following format:

Field 1  Field2  Field3
xxx      yyyy    zzz
aaa      bbbb    c
ddd      ff      gg

...


Do i need to use JTable?
if so, how to use JTable?
0
stanleyhuen
Asked:
stanleyhuen
  • 2
  • 2
1 Solution
 
Venci75Commented:
you can display it as HTML - use JEditorPane with content type "text/html":

      StringBuffer buff = new StringBuffer();
      buff.append("<Table>");
      buff.append("<TR>");
      buff.append("<TD>");
      buff.append("Field 1");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("Field 2");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("Field 3");
      buff.append("</TD>");
      buff.append("</TR>");

      buff.append("<TR>");
      buff.append("<TD>");
      buff.append("xxx");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("yyy");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("zzz");
      buff.append("</TD>");
      buff.append("</TR>");

      buff.append("<TR>");
      buff.append("<TD>");
      buff.append("aaa");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("bbbb");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("c");
      buff.append("</TD>");
      buff.append("</TR>");

      buff.append("<TR>");
      buff.append("<TD>");
      buff.append("ddd");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("ff");
      buff.append("</TD>");
      buff.append("<TD>");
      buff.append("gg");
      buff.append("</TD>");
      buff.append("</TR>");

      buff.append("</Table>");
      jEditorPane1.setContentType("text/html");
      jEditorPane1.setText(buff.toString());
0
 
objectsCommented:
Use a JTable
Heres an example:

Object[] columns = { "Field1", "Field2", "Field3" };
Object[][] data = {
  { "xxx", "yyy", "zzz" },
  { "aaa", "bbbb", "c" },
  { "ddd", "ff", "gg"}};

JTable table = new JTable(data, columns);
frame.getContentPane().add(new JScrollPane(table));
0
 
OviCommented:
Just look in the api help at the JTable constructors :

JTable(Object[][], Object[])
and
JTable(Vector, Vector)
0
 
OviCommented:
I believe the second constructor is more realiable for you since the size of a Vector can grow dinamically, while of an array not (you don't know exactly in a resultSet the number of rows/columns)
0
 
objectsCommented:
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now