Saturday, December 01, 2007

How to show data in database with a JTable

This Java Swing tip illustrates a method of showing data in database in a JTable. JTable act as a output for the database. The developer may use this to display a part of data (for example data generated form a query) to the user in a table. The table gives user an impression of database.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class DatabaseTest extends JFrame {

JTextField hostField;
JTextField queryField;
QueryTableModel qtm;

public DatabaseTest() {
super("Database Test Frame");
setSize(350, 200);

qtm = new QueryTableModel();
JTable table = new JTable(qtm);
JScrollPane scrollpane = new JScrollPane(table);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 2));
p1.add(new JLabel("Enter the Host URL: "));
p1.add(hostField = new JTextField());
p1.add(new JLabel("Enter your query: "));
p1.add(queryField = new JTextField());
p1.add(new JLabel("Click here to send: "));

JButton jb = new JButton("Search");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
} );
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(scrollpane, BorderLayout.CENTER);

public static void main(String args[]) {
DatabaseTest tt = new DatabaseTest();

No comments: