Home Answers Viewqa Swing-AWT Java swing in NetBeans

 
 


swebert
Java swing in NetBeans
2 Answer(s)      3 years and 2 months ago
Posted in : Swing AWT

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 Pages:
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.... i will specify a swing code for JTable using NETBEANS so would you tell me... table like this is it possible: if it is can u give me code in NetBeans
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
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
Looking for sample project in java using netbeans
Looking for sample project in java using netbeans  Hi all, i am novice in developing desktop application in java using netbeans. can anyone pls help.../example/java/swing/addeditanddeleteemployee_inf.shtml
swing
swing  Write a java swing program to delete a selected record from a table
netbeans library
netbeans library  Hi could someone build a program in netbeans.../Java-Beginners/19557-Java-null-pointer-exception.html http://www.roseindia.net/answers/viewqa/Java-Beginners/7688-write-java-code-discount-bookstore.html
netbeans library
netbeans library  Hi could someone build a program in netbeans.../JavaScriptQuestions/14353-java-code.html http://www.roseindia.net/answers/viewqa/Java.../answers/viewqa/Java-Beginners/7688-write-java-code-discount-bookstore.html
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
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Java swing - Java Beginners
Java swing  how to set the background picture for a panel in java swing .i m using Netbeans IDE.  Hi Friend, Try the following code: import java.awt.*; import java.awt.image.*; import java.io.*; import
Sitemap Java Swing Tutorial
-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 is java swing
Netbeans GUI Ribbon
Netbeans GUI Ribbon  how to create ribbon task in java GUI using netbeans
Look and Feel - Swing AWT
Look and Feel  i m using netbeans for creating a swing GUI...: http://www.roseindia.net/java/example/java/swing/GettingAndSettingLAF.shtml http://www.roseindia.net/java/example/java/swing/DefaultLookAndFeel.shtml Hope
java swing (jtable)
java swing (jtable)  hii..how to get values of a particular record in jtable from ms access database using java swing in netbeans..?? please help..its urgent..   Here is an example that retrieves the particular record
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.
netbeans with mysql and tomcat
netbeans with mysql and tomcat  Hi. I have to run a JSP or JAVA file in netbeans with mysql database and tomcat server 7.0. But while the page loading itself it say error. ClassNotFoundException: com.mysql.jdbc.Driver. How
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
NetBeans
NetBeans  why Netbeans IDE is not commonly used Today by most of the companies
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
NetBeans IDE
NetBeans IDE         The NetBeans IDE, product of Sun Microsystems, is a free, open-source integrated development environment written entirely in Java for software developers
java -netbeans help - Java Beginners
java -netbeans help   a simple program in netbeans ide to add two numbers and display the result as ADDITION OF TWO NUMBERS IS:70 in the textarea.../mam, i developed frontend with netbeans with buttons and textarea.. am executing
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
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
java swing-action on checkbox selection
java swing-action on checkbox selection  I am working in netbeans and mysql.On selecting a check box, the data from database must be retrieved. i need the action preformance of check box...could nybody?....if could...!thanks
java swing
java swing  how to connect database with in grid view in java swing   Hi Friend, Please visit the following link: Grid view in java swing Thanks
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
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
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 - 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
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
netbeans ddl and dml sql statements - Java Beginners
netbeans ddl and dml sql statements  Kindly send me the ddl,dml and tcl commands while in netbeans. wilson
JFrame Components Printing - Swing AWT
link: http://www.roseindia.net/java/example/java/swing/Print.shtml Hope...JFrame Components Printing  hi sir i am doing a project so i am working in netbeans i have to print a JFrame it contains Labels and few
Tree and a desktoppane - Swing AWT
Tree and a desktoppane  Hi , Iam kind of new to Java... on top, a tree (separate java class outside using JTree and the corresponding... as they are in the same class. Iam using netbeans to develop. Please help. Thanks
java heap space & netbeans - Java Beginners
java heap space & netbeans  Java heap space". I'm using netbeans 5.1.1 and jdk1.5.0_07 version.. can any body say how can i know the amount of java heap space while i run...java heap space & netbeans Hey,all!! I've got a problem
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
coding problem in netbeans - Java Beginners
coding problem in netbeans  hi, i have just started programming in netbeans,i'm facing a problem in a java application i create a jframe class and jbutton and jtextfeild inside it now i have created another class c1
netbeans coding prob - IDE Questions
netbeans coding prob  hi, i have just started programming in netbeans,i'm facing a problem in a java application i create a jframe class... ME HOW TO DO IT IN NETBEANS. THANK YOU  Hi Friend, Please visit
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 - 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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.