Share on Google+Share on Google+

Display table performing action on another table

In this section, we are going to display a table that will perform an action on another table.

Display table performing action on another table

In this section, we have created two tables. The data in table 1 have shown as a result of the database. We have retrieved the values from the database and display it on the table  1 containing the columns Item_code, Item_name, Item_Price. On clicking any of the table row, another table will be displayed showing the data of that row with one additional column 'Quantity'. The value for this column should increase by 1, if you have already clicked that row.

Here is the 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();
    ListSelectionModel listSelectionModel;
    final Vector columnNames = new Vector();
        final Vector data = new Vector();
    Connection con = null;
      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.addElementmd.getColumnName(i) );
while ({
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElementrs.getObject(i) );
data.addElementrow );
catch(Exception e) {}
JFrame tab=new JFrame();
final JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPanetable );
table.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
      Object obj1=null,obj2=null,obj3=null;
     String q="";
      int i=table.getSelectedRow();
      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();
  tab.addscrollPane );
  public Object GetData(JTable table, int row_index, int col_index){
    return table.getModel().getValueAt(row_index, col_index);



Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.