Share on Google+Share on Google+

samir
Java question
1 Answer(s)      7 years ago
Posted in : Swing AWT

Ads

View Answers

September 22, 2009 at 12:32 PM


Hi Friend,

Try the following code:

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

class DisplayTableAction{
int counter1=0,counter2=0,counter3=0,counter4=0;
int counter=0;
public static void main(String arg[]){
DisplayTableAction action=new DisplayTableAction();
}
DisplayTableAction(){
ListSelectionModel listSelectionModel;
final Vector columnNames = new Vector();
final Vector data = new Vector();
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register";, "root", "root");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from item");
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();
st.close();
}
catch(Exception e) {}
JFrame tab=new JFrame();
final JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane( table );
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){

Object obj1=null,obj2=null,obj3=null;
String q="";
int i=table.getSelectedRow();
if(i==0){
counter1++;
q=Integer.toString(counter1);
}
if(i==1){
counter2++;
q=Integer.toString(counter2);
}
if(i==2){
counter3++;
q=Integer.toString(counter3);
}
if(i==3){
counter4++;
q=Integer.toString(counter4);
}
obj1 = GetData(table, i, 0);
obj2 = GetData(table, i, 1);
obj3 = GetData(table, i, 2);
String data[][] = {{obj1.toString(),obj2.toString(),obj3.toString(),q}};
String col[] = {"Item_Code","Item_Name","Item_Price","Quantity"};
JTable t=new JTable(data, col);
JScrollPane p=new JScrollPane(t);
JFrame f=new JFrame();
f.add(p);
f.setVisible(true);
f.setSize(300,100);
}
});
tab.add( scrollPane );
tab.setVisible(true);
tab.setSize(350,150);
}
public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
}
Thanks









Related Tutorials/Questions & Answers:
Question in Java ??
Question in Java ??   Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number squrfoot
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Advertisements
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question on java
Question on java  why java is not purely oop?   java is not purely OOP because 1.) Multiple Inheritance is not supported. 2.) It uses primitive data type. 3.) static method can be called without creating instances
Question Java>?
Question Java>?  **ï??· Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts. ï??· Enter data for all accounts (account number,name, balance). ï??· Add
java question
java question   Create a class called Complex to perform arithmetic operations with complex numbers. 1- Use double variables to represent the fields of the class. 2- Provide a no-argument constructor with default values in case
java question :)
java question :)  write java program to use vector in ArrayList with add, remove,sort   import java.util.Vector; public class... Element MUAYAD"); vc.add("Vector Element JAVA"); vc.add("Vector Element
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
java question
java question  find the 127 twin pair in java
java question
java question  the purpose of defining multiple constructor in java
java question
java question  what is the difference between path and classpath in java
java question
java question  How to find the longest and shortest line from the file in case of java and java program should read the filename from the keyboard
java question
java question  wats dynamic dispatching
java question
java question  how to write a java program that, given a friend name , display the phone number and the email address for that friend
java question
java question  Please, give me sample program for insert Audio(sound) on Frame in swing in java
java question
java question  anyone provide the code to call c++ function from java   Here you will find details how to implement the c++ into the java. http://www.javaworld.com/javatips/jw-javatip23.html
Java Question
Java Question  can u give me one expample in which conditions we... in java includes only abstract methods i.e. methods have signatures only (or we... in java defines that it must be implemented by any class to make use of it. One
java question
java question  what are the exactly J2EE concepts.. struts and hybernates coming under which catagiry
java question
java question  input any number and check if sum of alternate digit is palindrome
java question
java question  how to create date and time based pgm in java... in db table if date and time crossed automatically update in the db and inform to login user as pop ups   http://www.roseindia.net/jsp/loginstatus.shtml
java question
java question  i wrote to java code to get data populated in the Revision field of table InventDim 2.Failing to successfully do the above, i tried to use the ConfigId field of table InventDim for the same purpose, by labeling
java question
java question  sir, can you give me the code for online banking using netbeans and mysql in j2ee with its database connectivity
java question
java question  sir, can you give me the code for online banking using netbeans and mysql in j2ee with its database connectivity
java question
java question  when we use the Abstract class and interface?   Please visit the following link: Abstract class and Interface
java question
java question  Can any one send me the source code to get confirmation after the successful registration to our mobile
java question
java question  I am converting a .net website into java one. Can you help me how to code things step by step. project name - src -java -main -webapp - common - jsp - css - images - javascripts I
Java question
Java question  Write a program that converts a (C to F Converter from 0 - 20) and writes the output to a file instead of the screen. Open the file in Notepad to confirm the output
java question
java question  i have a string like "My name is arvind.i live in bangalore.i study in college.".the problem is that i hav to break this string into three seperate lines
java Question
java Question  write a program that reads an integer and check whether it is even. for example, if your input is 25, the should be: is 25 an even number? false if your input is 2000, the should be: is 2000 an even number?true
Tutorials   
Java Spring Hibernate Struts Training Change JPanel background color from mouse entered/exit event JTable Pagination Jcombobox JAVA SWING how can i display a pdf file in a jtextarea Crop Image Code in Applet Crop Image Code in Applet Image Manipulation in Swing AWT Applet Ho to get virtual key code. how to show set of images continously using JScrollpane in java how to show set of images continously using JScrollpane in java How to connect on screen keyboard to system. How to connect on screen keyboard to system. query java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings Download. how to open one Jframe from main method call Urgent requirement display shapes i am getting an error in adding a menu item to a menu. I am trying to develop a Image Sliding application but unfortunately its not working... Can any one help me in this Sliding window protocol how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider Jbutton in JTable cells program code java textfield problem swing retrieve data from database in java swing form using prev n next buttons retrieve data from database in java swing form using prev n next buttons How to close a frame on action event of a button from a different class ? searching the data swings for webnms Jpanel allignment pass data from java frame to file code for traffic control system simulation Bank Management System how to maKE Jcombox editable after saveing value init. i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled. i havei) a jtable with few cloumns and few rows andii) one button. when place the cursor in any of the table's column cell. i need to enable the button.by default the button should be disabled. PDF Comparator i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox. issue on jcombobox

Ads

 
Advertisement null

Ads