Java swing in NetBeans

Java swing in NetBeans

thanks a lot sir for everything you answered for my last questions now sir i just have another 3 questions that is


Q 1.
i will specify a swing code for JTable using NETBEANS so would you tell me is it correct code for JTable and is there other way of specifying it.

code:
String c=Combo.getSelectedItem().toString();
String sql;
if(c.equals("PAID"))
{
sql="SELECT * FROM Fees_Struc, Student, Fees_Pay WHERE Fees_Struc.Hostel=Student.Hostel And Student.Roll=Fees_Pay.Roll And Fees_Pay.Status=?";
}
else
{
sql="SELECT * FROM Fees_Struc, Student, Fees_Pay WHERE Fees_Struc.Hostel=Student.Hostel And Student.Roll=Fees_Pay.Roll And Fees_Pay.Status=?";
}


try
{
int row=0,columns=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Stu","admin","admin");
try
{
PreparedStatement stm=con.prepareStatement(sql);
stm.setString(1, c);
ResultSet res=stm.executeQuery();
while(res.next())
{
row=res.getRow();

}
PreparedStatement stm1=con.prepareStatement(sql);
stm1.setString(1, c);
res=stm1.executeQuery();
ResultSetMetaData md = res.getMetaData();
columns = md.getColumnCount();
Object r[][]=new Object[row][columns] ;
int rindex=0;
while(res.next())
{
r[rindex][0]=res.getInt("Roll");
r[rindex][1]=res.getString("Name");
r[rindex][2]=res.getString("Add");
r[rindex][3]=res.getInt("Age");
r[rindex][4]=res.getString("Course");
r[rindex][5]=res.getString("Class");
r[rindex][6]=res.getString("Sex");
r[rindex][7]=res.getString("Hostel");
r[rindex][8]=res.getInt("Year");
r[rindex][9]=res.getString("Month");
r[rindex][10]=res.getInt("Paid");
r[rindex][11]=res.getInt("Bal");
r[rindex][12]=res.getString("Status");
rindex++;
}
jTable1.setModel(new javax.swing.table.DefaultTableModel(r, new String [] {"Roll No", "Name","Address","Age","Course","Class","Sex","Hostel","Year","Month","Paid","Bal","Status" }
));

res.close();
stm.close();
stm1.close();
con.close();
}



catch(SQLException ex)
{
javax.swing.JOptionPane.showMessageDialog(null,"Cannot display","Blank",javax.swing.JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e)
{
javax.swing.JOptionPane.showMessageDialog(null,"Cannot display","Blank",javax.swing.JOptionPane.ERROR_MESSAGE);
}
}

Q 2.
i want to transfer it to excel for printing so can u plz specify code for this above table.

Q 3.

i want to have another table like this is it possible: if it is can u give me code in NetBeans

Table Header Table Fields
_____________________________________________________
| ROll NO | Data from Database |
|______________|______________________________________|
| Name | Data from Database |
|______________|______________________________________|
| Address | Data from Database |
|______________|______________________________________|
| Age | Data from Database |
|______________|______________________________________|
| Course | Data from Database |
|______________|______________________________________|

sorry for specifying like this.....
View Answers

March 18, 2010 at 3:33 PM

Hi Friend,

1)Try the following :

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

public class JTableDatabase {
public static void main(String[] args) {
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel p=new JPanel();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
String sql = "Select * from data";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
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();
}
catch(Exception e) {
System.out.println( e );
}
JTable table = new JTable(data, columnNames);
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(250);
}
JScrollPane scrollPane = new JScrollPane( table );
p.add( scrollPane );
JFrame f=new JFrame();
f.add(p);
f.setSize(600,400);
f.setVisible(true);
}
}

March 18, 2010 at 4:32 PM

continue..

2)Try the following:

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

class Form extends JFrame{
ResultSet rs;
Form(){
String data[][] = {{"",""}};
String col[] = {"Name","Address"};
final DefaultTableModel model = new DefaultTableModel(data,col);
final JTable table = new JTable(model);
JScrollPane pane=new JScrollPane(table);
JButton button=new JButton("Export");
JPanel panel=new JPanel();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
final HSSFWorkbook wb = new HSSFWorkbook();
final HSSFSheet sheet = wb.createSheet("Excel Sheet");
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test";, "root", "root");
Statement st = con.createStatement();
rs= st.executeQuery("Select * from data");
while(rs.next()){
model.insertRow(table.getRowCount(),new Object[]{rs.getString("name"),rs.getString("address")});
}
model.removeRow(0);
panel1.add(pane);
panel2.add(button);
panel.add(panel1);
panel.add(panel2);
add(panel);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
try{
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("Name");
rowhead.createCell((short) 1).setCellValue("Address");
int index=1;
int count=table.getRowCount();
for(int i=0;i<count;i++){
Object obj1 = GetData(table, i, 0);
Object obj2 = GetData(table, i, 1);

HSSFRow row = sheet.createRow((short)index);
row.createCell((short) 0).setCellValue(obj1.toString());
row.createCell((short) 1).setCellValue(obj2.toString());
index++;
}
FileOutputStream fileOut = new FileOutputStream("c:\\Hello.xls");
wb.write(fileOut);
fileOut.close();
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start C:\\Hello.xls");
}
catch(Exception ex){}
}
});
}
catch(Exception e){}

}
public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
}
class JTableToExcel{
public static void main(String arg[]) {
try
{
Form frame=new Form();
frame.setSize(450,200);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}

Please clarify your third problem.

Thanks









Related Tutorials/Questions & Answers:
java swing in netbeans
java swing in netbeans  how can create sub menu in java swing using netbeans?   Hi Friend, Try the following code: import javax.swing.... backwardMenuItem = new JMenuItem("Java"); newmenu.add(backwardMenuItem
Java swing in NetBeans - Swing AWT
Java swing in NetBeans   thanks a lot sir for everything you answered for my last questions now sir i just have another 3 questions that is Q 1. i will specify a swing code for JTable using NETBEANS so would you tell me
Advertisements
jdbc and swing problem in netbeans
jdbc and swing problem in netbeans  i reteived the table from database in a jdbc program. next i want to do is place the table as it is in a jpanel.. i am using netbeans IDE can u tel me how to do that one?? urgent
jdbc and swing problem in netbeans
jdbc and swing problem in netbeans  i reteived the table from database in a jdbc program. next i want to do is place the table as it is in a jpanel.. i am using netbeans IDE can u tel me how to do that one?? urgent
Designing of textfield arrays in Netbeans IDE - Swing AWT
Designing of textfield arrays in Netbeans IDE  Respected sir, Sir I want to create an array of Jtextfield in Jframe Form in Swing GUI in NetBeans IDE.... How can i do this.........???? I have a code which
java netbeans
java netbeans  i am making project in core java using Netbeans. Regarding my project i want to know that How to fetch the data from ms-access... using netbeans
java netbeans
java netbeans  i am making project in core java using Netbeans. Regarding my project i want to know that How to fetch the data from ms-access... using netbeans
java swing - Swing AWT
java swing  how i can insert multiple cive me exampleolumn and row in one JList in swing?plz g  Hi Friend, Please clarify your question. Thanks
Java swing
Java swing  what are the root classes of all classes in swing
Java swing
Java swing  Does Swing contains any heavy weight component
java swing
java swing  view the book details using swing
java swing - Swing AWT
java swing  how to add image in JPanel in Swing?  Hi Friend, Try the following code: import java.awt.*; import java.awt.image....: http://www.roseindia.net/java/example/java/swing/ Thanks
java swing - Swing AWT
java swing   how i can insert in JFrame in swing?  Hi Friend, Try the following code: import java.awt.*; import javax.swing.*; import java.awt.event.*; class FormDemo extends JFrame { JButton ADD; JPanel
java swing - Swing AWT
java swing   Iam developing a java web browser.Actually my code works fine ie. i can load a web page without proxy.But in my place i have only proxy... a proxy or how to make my java web browser to listen to proxy setting??? please help
java swing
java swing  what is java swing   Swing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java Foundation Classes), which is an API for providing a graphical user interface for Java
java swing
java swing  how to connect database with in grid view in java swing   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 Grid view in java swing Thanks
java swing
java swing  add two integer variables and display the sum of them using java swing
Java swing
Java swing  Write a java swing program to calculate the age from given date of birth
java swing.
java swing.  Hi How SetBounds is used in java programs.The values in the setBounds refer to what? ie for example setBounds(30,30,30,30) and in that the four 30's refer to what
JAVA SWING
JAVA SWING  Hi.... Iam doing project in java...and my front end in swing ..our project is like billing software... then what are the topics i want cover? then how to design? pls help me
java swing - Swing AWT
java swing  how to save data in sql 2005 while insert in textfield  Hi Friend, Try the following code: import java.sql.*; import javax.swing.*; import javax.swing.border.*; import java.awt.*; import
Java swing
are displayed in the table..I need the source code in java swing...Java swing  If i am login to open my account the textfield,textarea and button are displayed. if i am entering the time of the textfield
Java swing
to the database using java swing...Java swing  I create one table. That table contains task ID and Task Name. When I click the task ID one more table will be open and that table
java - Swing AWT
What is Java Swing AWT  What is Java Swing AWT
swing - Java Beginners
Swing application testing  What are the conventions for testing a Java swing application
NetBeans
NetBeans  why Netbeans IDE is not commonly used Today by most of the companies
java login form using netbeans
java login form using netbeans  how to connect an access database to a login form using netbeans
Java swing
Java swing  how to create simple addition program using java swing?   import java.awt.*; import javax.swing.*; import java.awt.event.*; class SumOfNumbers extends JFrame { SumOfNumbers(){ JLabel lab1=new
NetBeans
NetBeans  Hi, I am Kundan I have made a project on NetBeans. And now I want to know that how can i run my project without NetBeans IDE,on other PC. Please help me it's important
NetBeans
NetBeans  Hi, I am Kundan I have made a project on NetBeans. And now I want to know that how can i run my project without NetBeans IDE,on other PC. Please help me it's important
buttons in netbeans,java
buttons in netbeans,java  respected sir, i would like to known how to make coding on a button while working on netbeans, so that a new window will open after clicking on that button? thank you   import java.awt.
Java swing
Java swing  when i enter the time into the textbox and activities into the textarea the datas saved into the database.the java swing code for the above item   import java.sql.*; import java.awt.*; import javax.swing.
netbeans
netbeans  In netbeans, there are choices of books with their price. you check the book you wanted then click the purchase.the output should be the book with the price then you will get the total price of the book you purchase.how
java swing
java swing  what is code for diplay on java swing internal frame form MYSQL DB pls send   Here is a code of creating form on jinternalframe and connect to mysql. import java.io.*; import java.sql.*; import java.awt.
Sitemap Java Swing Tutorial
Parameter | JPA One-to-One Relationship | JPA-QL Queries Java Swing Tutorial Section Java Swing Introduction | Java 2D API | Data Transfer in Java Swing | Internationalization in Java Swing | Localization | What
netbeans
netbeans  guysss m nt able to opemn ma netbeans ide.. no error msg s coming... wn i installed yesterday one msg came stating about some run time error... sm one plzzzzzz hlp me
Java Programming using Netbeans - IDE Questions
visit the following link: http://www.roseindia.net/java/example/java/swing/add...Java Programming using Netbeans  Hello Dear sir, i got one scenario... & address using netbeans jframe then i have to store these data into my package so
SWING
SWING  A JAVA CODE OF MOVING TRAIN IN SWING
Java swing code
Java swing code  can any one send me the java swing code for the following: "A confirmation message after the successful registration of login form
Java swing code
Java swing code  can any one send me the java swing code for the following: "A confirmation message after the successful registration of login form
Java swing code
Java swing code  can any one send me the java swing code for the following: "A confirmation message after the successful registration of login form
Java swing code
Java swing code  can any one send me the java swing code for the following: "A confirmation message after the successful registration of login form
Java swing code
Java swing code  can any one send me the java swing code for the following: "A confirmation message after the successful registration of login form
Java swing code
Java swing code  can any one send me the java swing code for the following: "A confirmation message after the successful registration of login form
java swing and CSS
java swing and CSS  can css be used in java swing desktop application in different forms for better styles?plzz help
java,jdbc,netbeans
java,jdbc,netbeans  can you tell me the program which read multiple dbf files and then insert those dbf files data in msaccess automatically
Java swing
Java swing  How to combine two java files
java swing
java swing  meaning of out.flush
Java swing
Java swing  What method is used to specify a container's layout
Java swing
Java swing  What methods are used to get and set the text label displayed by a Button object

Ads