jtable question
i have a button that when pressed will get data from a database and put them in a table and then add this table to a panel...the problem is that this table does not show until i click the mouse button randomly somewhere in this panel...i tried using the repaint() but that did not work....
im also facing problems in making the table's cells not editable and by making the horizontal scroll bar show
here is part of my code
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.print.PrinterException;
import java.sql.*;
import java.text.MessageFormat;
import java.util.Vector;
publicclass GUIextends JFrameimplements ActionListener, WindowListener{
public GUI(){
initializeMenuBar();
initializeGUI();
}
publicvoid initializeGUI(){
addWindowListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setTitle("Medawwar DVD rental center");
setSize(1000, 700);
setLocation(20, 20);
JTabbedPane tp =new JTabbedPane(JTabbedPane.LEFT);
getContentPane().add(tp);
tp.addTab("Customer List",new CustomerListGUI());
}
publicvoid initializeMenuBar(){
JMenuBar menuBar =new JMenuBar();
JMenu fileMenu =new JMenu("File");
JMenuItem exitItem =new JMenuItem("Exit");
exitItem.setActionCommand("exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
}
publicstaticvoid main(String[] args){
GUI g =new GUI();
g.setVisible(true);
}
/**
* Invoked when an action occurs.
*/
publicvoid actionPerformed(ActionEvent e){
if (e.getActionCommand().equalsIgnoreCase("exit"))
System.exit(0);
}
/**
* Invoked the first time a window is made visible.
*/
publicvoid windowOpened(WindowEvent e){
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when the user attempts to close the window
* from the window's system menu.
*/
publicvoid windowClosing(WindowEvent e){
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a window has been closed as the result
* of calling dispose on the window.
*/
publicvoid windowClosed(WindowEvent e){
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a window is changed from a normal to a
* minimized state. For many platforms, a minimized window
* is displayed as the icon specified in the window's
* iconImage property.
*
* @see java.awt.Frame#setIconImage
*/
publicvoid windowIconified(WindowEvent e){
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a window is changed from a minimized
* to a normal state.
*/
publicvoid windowDeiconified(WindowEvent e){
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when the Window is set to be the active Window. Only a Frame or
* a Dialog can be the active Window. The native windowing system may
* denote the active Window or its children with special decorations, such
* as a highlighted title bar. The active Window is always either the
* focused Window, or the first Frame or Dialog that is an owner of the
* focused Window.
*/
publicvoid windowActivated(WindowEvent e){
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a Window is no longer the active Window. Only a Frame or a
* Dialog can be the active Window. The native windowing system may denote
* the active Window or its children with special decorations, such as a
* highlighted title bar. The active Window is always either the focused
* Window, or the first Frame or Dialog that is an owner of the focused
* Window.
*/
publicvoid windowDeactivated(WindowEvent e){
//To change body of implemented methods use File | Settings | File Templates.
}
}
class CustomerListGUIextends JPanelimplements ActionListener, GUIConstants{
privatefinalint FONT_SIZE = 15;
private Font f =new Font("Times New Roman", Font.BOLD, FONT_SIZE);
private Font borderFont =new Font("Times New Roman", Font.BOLD, FONT_SIZE + 5);
private JPanel p =new JPanel();
private JButton gen =new JButton("Generate");
private JButton reset =new JButton("reset");
private JButton print =new JButton("print");
JTable table =null;
public CustomerListGUI(){
initializeGUI();
setupComponents();
setupButtons();
setupFonts();
setupTable();
}
publicvoid setupFonts(){
gen.setFont(f);
reset.setFont(f);
}
publicvoid setupComponents(){
Border b = BorderFactory.createLoweredBevelBorder();
this.setBorder(BorderFactory.createTitledBorder(b
,"Customer List"
, TitledBorder.DEFAULT_JUSTIFICATION
, TitledBorder.DEFAULT_POSITION
, borderFont
, Color.blue));
p.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
}
publicvoid initializeGUI(){
setLayout(new BorderLayout());
add(p, BorderLayout.NORTH);
p.add(gen);
p.add(print);
p.add(reset);
}
publicvoid setupButtons(){
gen.addActionListener(this);
gen.setActionCommand("generate");
reset.addActionListener(this);
reset.setActionCommand("reset");
print.addActionListener(this);
print.setActionCommand("print");
}
publicvoid setupTable(){
}
publicvoid resetFields(){
}
public Box addComponent(Component c1, Component c2,int spacing){
returnnull;//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when an action occurs.
*/
publicvoid actionPerformed(ActionEvent e){
if (e.getActionCommand().equalsIgnoreCase("reset")){
resetFields();
}
if (e.getActionCommand().equalsIgnoreCase("print")){
MessageFormat header =new MessageFormat("Customer List");
MessageFormat footer =new MessageFormat("{0}");
PrintRequestAttributeSet attr =new HashPrintRequestAttributeSet();
attr.add(MediaSizeName.ISO_A4);
attr.add(OrientationRequested.LANDSCAPE);
try{
table.print(JTable.PrintMode.FIT_WIDTH, header, footer, true, attr,true);
}catch (PrinterException e1){
JOptionPane.showMessageDialog(null,"error printing","ERROR", JOptionPane.ERROR_MESSAGE);
}
}
if (e.getActionCommand().equalsIgnoreCase("generate")){
try{
Vector columnName =new Vector();
Vector data =new Vector();
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/dvd","root","swordfrogy");
String sql ="SELECT fname, phone, address, note FROM customer";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
columnName.addElement("Name");
columnName.addElement("Phone");
columnName.addElement("Address");
columnName.addElement("Note");
// Get row data
while (rs.next()){
Vector row =new Vector(columns);
for (int i = 1; i <= columns; i++){
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
stmt.close();
JTable table =new JTable(data, columnName){
public Class getColumnClass(int column){
for (int row = 0; row < getRowCount(); row++){
Object o = getValueAt(row, column);
if (o !=null){
return o.getClass();
}
}
return Object.class;
}
};
//table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
table.getTableHeader().setReorderingAllowed(false);
JScrollPane sp =new JScrollPane(table
,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp, BorderLayout.CENTER);
}catch (SQLException e1){
e1.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
}catch (IllegalAccessException e1){
e1.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
}catch (ClassNotFoundException e1){
e1.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
}catch (InstantiationException e1){
e1.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
}
}
}
}
interface GUIConstants{
publicvoid setupFonts();
publicvoid setupComponents();
publicvoid initializeGUI();
publicvoid setupButtons();
publicvoid setupTable();
publicvoid resetFields();
public Box addComponent(Component c1, Component c2,int spacing);
}
please help
thanks in advance

