CloudStrife209
asked on
getting a object returned from a row selection on a JTable
I've built a jTable and added records to it from an custom object that i've built which all seems to work fine.
Here is the thing i want to show some values in the table not all of them, but when the user selects the row they want i want to get the full object back and display in a series of text, checkboxes and such the rest of the values. now when i have the whole list showing i can use the selection index and match it up to the original collection but when i add a filter the selection index doesn't match the original collection any more.
The first example is the setup of the table
the second is the Abstractdatamodel that i'm using
and the third is the listener and record change code i've got
Thanks
Here is the thing i want to show some values in the table not all of them, but when the user selects the row they want i want to get the full object back and display in a series of text, checkboxes and such the rest of the values. now when i have the whole list showing i can use the selection index and match it up to the original collection but when i add a filter the selection index doesn't match the original collection any more.
The first example is the setup of the table
the second is the Abstractdatamodel that i'm using
and the third is the listener and record change code i've got
Thanks
OccupationTableModel model = new OccupationTableModel(occupationList);
occupationTable = new JTable(model);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
occupationTable.setRowSorter(sorter);
package dataTypes;
import javax.swing.table.AbstractTableModel;
@SuppressWarnings("serial")
public class OccupationTableModel extends AbstractTableModel {
Occupation[] occupations;
public OccupationTableModel(Occupation[] occipationList) {
occupations = occipationList;
}
public int getColumnCount() {
return 5;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "ANZIC";
case 1:
return "Business Discription";
case 2:
return "General Acceptence";
default:
return "Unknown";
}
}
@Override
public int getRowCount() {
return occupations.length;
}
@Override
public Object getValueAt(int row, int col) {
switch (col) {
case 0:
return occupations[row].getANZIC();
case 1:
return occupations[row].getbusinessDiscription();
case 2:
return occupations[row].getgeneralAcceptence();
default:
return " - ";
}
}
public Object getObjectAt(int row) {
Occupation aOcuppation = occupations[row];
return aOcuppation;
}
}
ListSelectionModel rowSM = occupationTable.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return; // if you don't want to handle intermediate
// selections
ListSelectionModel rowSM = (ListSelectionModel) e.getSource();
int selectedIndex = rowSM.getMinSelectionIndex();
// do something with selected index
//System.out.print(selectedIndex + " ");
//System.out.println(occupationList[selectedIndex].getbusinessDiscription());
recordChange(selectedIndex);
}
});
private void recordChange(int selectedIndex) {
//This will be called when a new record is selected from the table.
NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setMaximumFractionDigits(0);
txtOccupationName.setText(occupationList[selectedIndex].getbusinessDiscription());
txtGeneralAcceptence.setText(occupationList[selectedIndex].getgeneralAcceptence());
txtPolicyWording.setText(occupationList[selectedIndex].getpolicyWording());
chkLiabilityAllowed.setSelected((boolean) occupationList[selectedIndex].getliabilityAllowed());
if (occupationList[selectedIndex].getfireSIBIC()==0) {
txtFireSI.setText("N/A");
} else {
txtFireSI.setText(nf.format(occupationList[selectedIndex].getfireSIBIC()));
}
if (occupationList[selectedIndex].getliabilitySIBIC()==0) {
txtLiabilitySI.setText("N/A");
} else {
txtLiabilitySI.setText(nf.format(occupationList[selectedIndex].getliabilitySIBIC()));
}
txtSurveyHazard.setText(Byte.toString(occupationList[selectedIndex].getSurveyHazard()));
txtIndustry.setText(occupationList[selectedIndex].getIndustry());
}
Well, when you do the filter you can maintain HashMap which will map the new numbers to the original ones
And if you filter your objects and have a new list of the remaining objects - in order to show the remaining fields - why do you need
to have original index ? First you show some fileds of the object then you show the remianing fields of the same object when the user selects it - don't see
why that would be a problem
to have original index ? First you show some fileds of the object then you show the remianing fields of the same object when the user selects it - don't see
why that would be a problem
ASKER
"First you show some fileds of the object then you show the remianing fields of the same object when the user selects it"
that would be my problem as i'm not doing that right, only way i could figure out how to only show some fields and show the others when the user selected it was to match up the records to the original collection.
that would be my problem as i'm not doing that right, only way i could figure out how to only show some fields and show the others when the user selected it was to match up the records to the original collection.
Why is that a problem?
manitain ArrayList of your objects and put that ArrayList as the basis of your TableModel
then you filter your ArrayList to a shorter ArrayList but the idea remains the same - tthe new ArrayList will be the basis
of the TableModel
If you could paste some code that would be slef-standing and I could compile it - I couyld try
to understand it in a more concrete way - otherwise it is difficult to understand extensive
blocks of code by sightreading
ASKER
i'm not sure if this is a problem but its not an arraylist it's simply an array of the object
package dataTypes;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.sql.*;
import system.SQLConnection;
public class Occupation {
private String ANZIC;
private String businessDiscription;
private String generalAcceptence;
private String policyWording;
private int fireSIBIC;
private int liabilitySIBIC;
private boolean liabilityAllowed;
private byte surveyHazard;
private boolean BISreferral;
private String primaryActivity;
private String industry;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
this);
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public Occupation(String ANZIC, String businessDiscription,
String generalAcceptence, String policyWording, int fireSIBIC,
int liabilitySIBIC, boolean liabilityAllowed, byte surveyHazard,
boolean BISreferral, String primaryActivity) {
super();
this.ANZIC = ANZIC;
this.businessDiscription = businessDiscription;
this.generalAcceptence = generalAcceptence;
this.policyWording = policyWording;
this.fireSIBIC = fireSIBIC;
this.liabilitySIBIC = liabilitySIBIC;
this.liabilityAllowed = liabilityAllowed;
this.surveyHazard = surveyHazard;
this.BISreferral = BISreferral;
this.primaryActivity = primaryActivity;
}
public Occupation() {
super();
this.ANZIC = "";
this.businessDiscription = "";
this.generalAcceptence = "";
this.policyWording = "";
this.fireSIBIC = 0;
this.liabilitySIBIC = 0;
this.liabilityAllowed = false;
this.surveyHazard = 0;
this.BISreferral = false;
this.primaryActivity = "";
}
public void setANZIC(String ANZIC) {
propertyChangeSupport.firePropertyChange("ANZIC", this.ANZIC,
this.ANZIC = ANZIC);
}
public void setbusinessDiscription(String businessDiscription) {
propertyChangeSupport.firePropertyChange("businessDiscription",
this.businessDiscription,
this.businessDiscription = businessDiscription);
}
public void setgeneralAcceptence(String generalAcceptence) {
propertyChangeSupport.firePropertyChange("generalAcceptence",
this.generalAcceptence,
this.generalAcceptence = generalAcceptence);
}
public void setpolicyWording(String policyWording) {
propertyChangeSupport.firePropertyChange("policyWording",
this.policyWording, this.policyWording = policyWording);
}
public void setfireSIBIC(int fireSIBIC) {
propertyChangeSupport.firePropertyChange("fireSIBIC", this.fireSIBIC,
this.fireSIBIC = fireSIBIC);
}
public void setliabilitySIBIC(int liabilitySIBIC) {
propertyChangeSupport.firePropertyChange("liabilitySIBIC",
this.liabilitySIBIC, this.liabilitySIBIC = liabilitySIBIC);
}
public void setliabilityAllowed(boolean liabilityAllowed) {
propertyChangeSupport
.firePropertyChange("liabilityAllowed", this.liabilityAllowed,
this.liabilityAllowed = liabilityAllowed);
}
public void setsurveyHazard(byte surveyHazard) {
propertyChangeSupport.firePropertyChange("surveyHazard",
this.surveyHazard, this.surveyHazard = surveyHazard);
}
public void setBISreferral(boolean BISreferral) {
propertyChangeSupport.firePropertyChange("BISreferral",
this.BISreferral, this.BISreferral = BISreferral);
}
public void setprimaryActivity(String primaryActivity) {
propertyChangeSupport.firePropertyChange("primaryActivity",
this.primaryActivity, this.primaryActivity = primaryActivity);
}
public void setIndustry(String industry) {
propertyChangeSupport.firePropertyChange("industry", this.industry,
this.industry = industry);
}
public String getANZIC() {
return ANZIC;
}
public String getbusinessDiscription() {
return businessDiscription;
}
public String getgeneralAcceptence() {
return generalAcceptence;
}
public String getpolicyWording() {
return policyWording;
}
public int getfireSIBIC() {
return fireSIBIC;
}
public int getliabilitySIBIC() {
return liabilitySIBIC;
}
public boolean getliabilityAllowed() {
return liabilityAllowed;
}
public byte getSurveyHazard() {
return surveyHazard;
}
public boolean getBISreferral() {
return BISreferral;
}
public String getPrimaryActivity() {
return primaryActivity;
}
public String getIndustry() {
return industry;
}
public static Occupation[] loadOccupationList() {
int i = 0;
Occupation[] occupationList;
Connection conn = SQLConnection.getConnection();
if (conn != null) {
try {
Statement st = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// Change the SQL statement below to change the collected data
ResultSet rs = st.executeQuery("SELECT * FROM tblBusinessList");
occupationList = new Occupation[SQLConnection.getRowCount(rs)];
rs.first();
// Put the data into the array need to check the next after the
// addition
do {
occupationList[i] = new Occupation();
occupationList[i].setANZIC(rs.getString("ANZICCode"));
occupationList[i].setbusinessDiscription(rs.getString("BusDiscription"));
occupationList[i].setgeneralAcceptence(rs.getString("GeneralAcceptence"));
occupationList[i].setpolicyWording(rs.getString("PolicyWording"));
occupationList[i].setfireSIBIC(rs.getInt("FireSI"));
occupationList[i].setliabilitySIBIC(rs.getInt("LiabilitySI"));
occupationList[i].setliabilityAllowed(rs.getBoolean("LiabilityAllowed"));
occupationList[i].setsurveyHazard(rs.getByte("SurveyHazard"));
occupationList[i].setBISreferral(rs.getBoolean("ReferToBIS"));
occupationList[i].setprimaryActivity(rs.getString("PrimaryActivity"));
occupationList[i].setIndustry(rs.getString("Industry"));
i++;
} while (rs.next());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
occupationList = null;
}
} else {
occupationList = null;
}
return occupationList;
}
}
In general ArrayList' s are more convenient than arrays, but in this respect it should not be that different -
just create TableModel around the array of your objects.
When you filter this array to another array - set this new array at the basis of TableModel
(say make the method setArray.. or something)
Your Occupation class is good, but still i cannot compile the whole thing,
but try to do it this way - I see no reason why you cannot do it the way I mentioned
just create TableModel around the array of your objects.
When you filter this array to another array - set this new array at the basis of TableModel
(say make the method setArray.. or something)
Your Occupation class is good, but still i cannot compile the whole thing,
but try to do it this way - I see no reason why you cannot do it the way I mentioned
What is the primary key (compound or otherwise) of Occupation? You could use that
ASKER
Primary key for occupation is ANZIC
ASKER
i needed the following line of code to do the conversion for me
table.convertRowIndexToMod el(viewRow );
table.convertRowIndexToMod
I guess, you can use TableRowSorter
http://download.oracle.com/javase/6/docs/api/javax/swing/table/TableRowSorter.html
and it looks like provides such functionality
Id still think that to filter your data and change underlying model will be simpler
at least for not too long tables
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.