Display table performing action on another table


 

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.

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();
}
  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.addElementmd.getColumnName(i) );
}
while (rs.next()){
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElementrs.getObject(i) );
}
data.addElementrow );
}
rs.close();
st.close();
}
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();
      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.addscrollPane );
  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);
  
 } 

Ads