Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jtable cell auto update  RSS feed

 
abhijeet nagare
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys....
i am new to java
i am developing a billing system of a shop....
and am using jtable to strore the data..

in jtable i use combox of product and services
i want help from you that how to get cost value of selected product or services automaticaly which is also stored in combobox....
please help...
code and gui is here




class table extends DefaultTableModel
{
JTable t;
JScrollPane jsp;
String col1[],col2[],col3[];
public String tickt;
JComboBox comboBox,comboBox1,comboBox2;

table()
{
comboBox = new JComboBox();
comboBox1 = new JComboBox();
comboBox2 = new JComboBox();

String col[]={"Employee"," Services","Product ","tax","cost"};

Object[][] data = {
{"", "" ,"",false,}};


DefaultTableModel model = new DefaultTableModel(data, col);
t = new JTable(model) {
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return Boolean.class;

default:
return String.class;

}
}
};



for(int i=0;i<5;i++)
{
t.getColumnModel().getColumn(i).setPreferredWidth(150);
t.setRowHeight(30);
}



try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbcracle:thin:@//localhost:1521", "system", "system");
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);;
String q="Select * From Employeedetails where status='1'";

ResultSet rs=st.executeQuery(q);

ResultSetMetaData m=rs.getMetaData();

int c=m.getColumnCount();


int cnt=0,cnt1=0,cnt2=0;
String col1[]=new String[c];
comboBox.addItem("");
while(rs.next())
{

cnt++;

comboBox.addItem(col1[cnt]=rs.getString(2));

}

String q1="select * from productdetails where status='1' ";
ResultSet rs1=st.executeQuery(q1);
ResultSetMetaData m1=rs1.getMetaData();
int c1=m1.getColumnCount();

String col2[]=new String[c1];
comboBox1.addItem("");

while(rs1.next())
{


comboBox1.addItem(rs1.getString(2));
cnt1++;

}


String q2="select * from servicedetails where status='1'";
ResultSet rs2=st.executeQuery(q2);
comboBox2.addItem("");
while(rs2.next())
{

cnt2++;

comboBox2.addItem(rs2.getString(2));
cnt2++;


}


String q3="SELECT MAX(ticktn) AS Highest_no FROM ticktno";
ResultSet rs3=st.executeQuery(q3);
while(rs3.next())
{
int ti=rs3.getInt(1)+1;
tickt="" + ti;
System.out.println(rs3.getInt(1));
}
}
catch (Exception e)
{

e.printStackTrace();

}


TableColumn employ = t.getColumnModel().getColumn(0);
employ.setCellEditor(new DefaultCellEditor(comboBox));
TableColumn product = t.getColumnModel().getColumn(1);
product.setCellEditor(new DefaultCellEditor(comboBox2));
TableColumn services1 = t.getColumnModel().getColumn(2);
services1.setCellEditor(new DefaultCellEditor(comboBox1));


int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
t.setToolTipText("table info");
t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

jsp=new JScrollPane(t,v,h);
jsp.setBackground(Color.GRAY);
jsp.setBounds(0, 200, 780, 150);

}

}
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. move everything, every code about JComponents, TableCellEditors and JDBC out of/from class table extends DefaultTableModel {

2. there would be only public Class getColumnClass(int column) { and Columns identifiers, better could be to declare String col[]={"Employee"," Services","Product ","tax","cost"}; as local variable

3. remove useless code line Object[][] data = {{"", "" ,"",false,}};

4. rename class table extends DefaultTableModel { to class MyTableModel extends DefaultTableModel { (see more, search for Java Naming Conventions)

5. important could be if JDBC will be invoked only once time (on apps start_up) or not

6. use if_needed instead of int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;

7. put JScrollPane to JFrame (BorderLayout in API) or JPanel (FlowLayout in API) by using standard Layout Manager, to set initial (and reasonable) PreferredSize table.setPreferredScrollableViewportSize(new Dimension(int, int)); as to call JFrame.pack() before JFrame.setVisible(true)

8. all value from JTables view is stored in XxxTableModel, then to use simple code line Table.getValueAt(int row, int column) or better from DefaultTableModel.getValueAt(int row, int column), notice for XxxTableModel, be sure that JTables view isn't sorted, filtered or Columns reordered, otherwise you would be need to convert index from view to model

EDIT (I forgot answer to your question:-))

9. override getValueAt, put there proper calculations, if passed, then output should be setValueAt

10. change String value (getColumnClass) in DefaultTableModel, JComboBox (DeafultComboBoxModel) in the case that contains number value to Double/Integer
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!