Originally posted by Peter Phung:
the table is obviously bigger than the viewable area, but there are no scrollbars.
JScollPane = ViewPort + ScrollBars
If you use JScrollPane, the scrollbars are included. If you could parse the resultset into the arrays as required in the code example below, using JTable is quite convenient:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class
Java {
public static void main(String args[]) {
String tableName = "Coffee Price List";
Object[] columnNames =
{"Brand", "Supplier", "Price", "Origin"};
Object[][] tableData = {
{"Arabia", new Float(57910000), new Float(14.80), "south" },
{"Colombian", null, new Float(13.90), "north" },
{"Java", new Float(108200000), new Float(12.55), "east" },
{"Arabia", new Float(57910000), new Float(14.80), "south" },
{"Colombian", null, new Float(13.90), "north" },
{"Arabia", new Float(57910000), new Float(14.80), "south" },
{"Colombian", null, new Float(13.90), "north" },
{"Java", new Float(108200000), new Float(12.55), "east" },
{"Amaretto", new Float(149600000), new Float(12.73), "east" },
{"MoonLight", new Float(384400), new Float(13.46), "west"},
{"Almond", new Float(227940000), new Float(16.94), "midwest" },
{"Amaretto", new Float(149600000), new Float(12.73), "east" },
{"Java", new Float(108200000), new Float(12.55), "east" },
{"Amaretto", new Float(149600000), new Float(12.73), "east" },
{"MoonLight", new Float(384400), new Float(13.46), "west"},
{"Almond", new Float(227940000), new Float(16.94), "midwest" },
{"Mint", new Float(9378), new Float(12.52), "southeast" },
{"Chocolate", new Float(23459), new Float(12.6), "northwest" },
{"MoonLight", new Float(384400), new Float(13.46), "west"},
{"Almond", new Float(227940000), new Float(16.94), "midwest" },
{"Mint", new Float(9378), new Float(12.52), "southeast" },
{"Chocolate", new Float(23459), new Float(12.6), "northwest" },
{"Mint", new Float(9378), new Float(12.52), "southeast" },
{"Chocolate", new Float(23459), new Float(12.6), "northwest" },
};
new Display(tableName, columnNames, tableData );
}
}
class Display {
JFrame window = new JFrame();
String title;
String text = "";
public Display(String tableName, Object[] columnNames, Object[][] tableData) {
JTable table = new JTable(tableData, columnNames);
JScrollPane scrollableTable = new JScrollPane(table);
JFrame mainWindow = new JFrame(tableName);
mainWindow.setContentPane(scrollableTable);
mainWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
mainWindow.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = mainWindow.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frameSize.height = 35 * tableData.length;
if (frameSize.height > 300) frameSize.height = 300;
mainWindow.setSize(frameSize.width, frameSize.height);
mainWindow.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
mainWindow.setVisible(true);
}
}
[ April 19, 2002: Message edited by: Doanh Nguyen ]
[ April 19, 2002: Message edited by: Doanh Nguyen ]