• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print in the printer a result of JTable?  RSS feed

 
Jan Michael Soan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello;
Can anyone there guide me how to print a query or text in my JTable, print with printpreview much better. I'm currently using jdk 1.3.
This are the codes for my JTable query :
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Report extends JFrame {
private Connection connection;
private JTable table;
public Report()
{
super("Timeport . . .");
String url = "jdbc dbc:Employees";

try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connection = DriverManager.getConnection( url );
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
}
getTable();
JPanel p1 = new JPanel();
JButton b1 = new JButton("Accept");
JButton b2 = new JButton("Cancel");
p1.add(b1);
p1.add(b2);
p1.setLayout( new FlowLayout() );
this.getContentPane().add( p1, BorderLayout.SOUTH );
Dimension sd = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setLocation(sd.width / 2 - 465 / 2,
sd.height / 2 - 390 / 2);
setResizable(false);
setSize( 465, 390 );
show();
}
private void getTable()
{
Statement statement;
ResultSet resultSet;

try {
String query = "SELECT " +
"IdNumber, Lastname, Firstname, SSSNo, Gender," +
"Status, Address, JobTitle, DaysWorked, Tax, SSS, Loan," +
"Medicare, OthersDed, BasicPay, OvertimePay, HolidayPay, OthersSal," +
"NetPay" + " FROM Records";
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
displayResultSet( resultSet );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private void displayResultSet( ResultSet rs )
throws SQLException
{
boolean moreRecords = rs.next();
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"ResultSet contained no records" );
setTitle( "No records to display" );
return;
}
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
ResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
// ------ Here's the JTable in my Program ------- //
table = new JTable( rows, columnHeads );
table.setPreferredScrollableViewportSize(new Dimension(600, 100));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JDesktopPane d1 = new JDesktopPane();
d1.putClientProperty("JDesktopPane.dragMode", "outline");
JInternalFrame f1 = new JInternalFrame("Report . . .",false,false,false,true);
f1.setSize(458, 300);
f1.setVisible(true);
Container c = f1.getContentPane();
c.add( new JScrollPane(table) );
d1.add( f1, BorderLayout.CENTER );
this.getContentPane().add(d1, BorderLayout.CENTER);

validate();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException
{
Vector currentRow = new Vector();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
currentRow.addElement( rs.getString( i ) );
break;

case Types.INTEGER:
currentRow.addElement(
new Long( rs.getLong( i ) ) );
break;

default:
System.out.println( "Type was: " +
rsmd.getColumnTypeName( i ) );
}
return currentRow;
}
public void shutDown()
{
try {
connection.close();
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to disconnect" );
sqlex.printStackTrace();
}
}

// Main method
public static void main( String args[] )
{
Report app = new Report();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
Thanks : God Bless to everyone.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not cross post. Continue the conversation here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!