Solved

swing application

Posted on 2002-06-28
5
171 Views
Last Modified: 2010-03-31


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
Comment
Question by:stanleyhuen
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
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
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
Just look in the api help at the JTable constructors :

JTable(Object[][], Object[])
and
JTable(Vector, Vector)
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now