Bookmark Topic Watch Topic
  • New Topic

How to change jTable data by jButton event  RSS feed

 
Bikash Megale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi everyone,

In my Swing GUI program, there are 5 jTables each having data from different Database table Data.

I want to change jTable and it's data by jButton event.


This code works,

public JTable getJTableStoreReport_monthly() {
if (jTableStoreReport_monthly == null) {

String categoryRangeBegin = "100";//categoryRangeBegin;
String categoryRangeEnd = "300";//categoryRangeEnd;
String dateRangeBegin = "2009-04";
String dateRangeEnd = "2009-06";

TableModelStoreReport_monthly storeReportTableModel_monthly;
storeReportTableModel_monthly =
new TableModelStoreReport_monthly(categoryRangeBegin, categoryRangeEnd, dateRangeBegin, dateRangeEnd);
jTableStoreReport_monthly = new JTable(storeReportTableModel_monthly);

jTableStoreReport_monthly.setSelectionForeground(Color.white);
jTableStoreReport_monthly.setSelectionBackground(Color.blue);
jTableStoreReport_monthly.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

// My addition
for (int i=0; i<jTableStoreReport_monthly.getColumnCount(); i++) {
TableColumn column = jTableStoreReport_monthly.getColumnModel().getColumn(i);
if (i==0) column.setPreferredWidth(50);
if (i==1) column.setPreferredWidth(150);
if (i==2) column.setPreferredWidth(60);
if (i==3) column.setPreferredWidth(70);
if (i==4) column.setPreferredWidth(60);
}

jTableStoreReport_monthly.setAutoCreateRowSorter(true);
jTableStoreReport_monthly.setVisible(true);


}
return jTableStoreReport_monthly;
}



But, if I want to control the categoryRangeBegin or dateRangeBegin inside jButton actionPerformed() block, the same code as following does not work

String categoryRangeBegin = "100";//categoryRangeBegin;
String categoryRangeEnd = "300";//categoryRangeEnd;
String dateRangeBegin = "2009-04";
String dateRangeEnd = "2009-06";

TableModelStoreReport_monthly storeReportTableModel_monthly;
storeReportTableModel_monthly =
new TableModelStoreReport_monthly(categoryRangeBegin, categoryRangeEnd, dateRangeBegin, dateRangeEnd);
jTableStoreReport_monthly = new JTable(storeReportTableModel_monthly);

jTableStoreReport_monthly.setSelectionForeground(Color.white);
jTableStoreReport_monthly.setSelectionBackground(Color.blue);
jTableStoreReport_monthly.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

// My addition
for (int i=0; i><jTableStoreReport_monthly.getColumnCount(); i++) {
TableColumn column = jTableStoreReport_monthly.getColumnModel().getColumn(i);
if (i==0) column.setPreferredWidth(50);
if (i==1) column.setPreferredWidth(150);
if (i==2) column.setPreferredWidth(60);
if (i==3) column.setPreferredWidth(70);
if (i==4) column.setPreferredWidth(60);
}

jTableStoreReport_monthly.setAutoCreateRowSorter(true);
jTableStoreReport_monthly.setVisible(true);




>
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please http://faq.javaranch.com/java/UseCodeTags
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Duplicated now: http://www.coderanch.com/t/452824/Swing-AWT-SWT-JFace/java/change-jTable-data-by-jButton
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!