How to display specific items from an ArrayList based on a value from another ArrayList?











up vote
-1
down vote

favorite












I'm currently carrying out a task, where I have to create a software development solution to allow managers to add new staff and tasks. for this, I have an ArrayList called staffMembers, which takes name, grade and salary as parameters and an array list called tasksToDo, which takes taskName, taskGrade and taskHours as parameters.



One of the requirements is to enable the managers to click a JButton and this will only display staffMembers whose grade is equal to or greater than the taskGrade for the selected task (this is selected using JComboBox). the result will be displayed in a JTable.



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


However, the code above doesn't give the required result and I hoped someone my be able to point me in the right direction.










share|improve this question
























  • What result does it give?
    – Jason Armstrong
    Nov 22 at 14:13






  • 1




    Share the results of your code and add an actual question.
    – sechanakira
    Nov 22 at 14:15










  • It gives me a list of all staff, including those whose grade is less than the job grade
    – smokinraygunn
    Nov 22 at 14:15










  • Where exactly is this.jobGrade and what value does it have (initially and possibly reassigned)?
    – deHaar
    Nov 22 at 14:19












  • @deHaar this.jobGrade takes the value from the tasksToDo ArrayList once the task is created
    – smokinraygunn
    Nov 22 at 14:23















up vote
-1
down vote

favorite












I'm currently carrying out a task, where I have to create a software development solution to allow managers to add new staff and tasks. for this, I have an ArrayList called staffMembers, which takes name, grade and salary as parameters and an array list called tasksToDo, which takes taskName, taskGrade and taskHours as parameters.



One of the requirements is to enable the managers to click a JButton and this will only display staffMembers whose grade is equal to or greater than the taskGrade for the selected task (this is selected using JComboBox). the result will be displayed in a JTable.



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


However, the code above doesn't give the required result and I hoped someone my be able to point me in the right direction.










share|improve this question
























  • What result does it give?
    – Jason Armstrong
    Nov 22 at 14:13






  • 1




    Share the results of your code and add an actual question.
    – sechanakira
    Nov 22 at 14:15










  • It gives me a list of all staff, including those whose grade is less than the job grade
    – smokinraygunn
    Nov 22 at 14:15










  • Where exactly is this.jobGrade and what value does it have (initially and possibly reassigned)?
    – deHaar
    Nov 22 at 14:19












  • @deHaar this.jobGrade takes the value from the tasksToDo ArrayList once the task is created
    – smokinraygunn
    Nov 22 at 14:23













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm currently carrying out a task, where I have to create a software development solution to allow managers to add new staff and tasks. for this, I have an ArrayList called staffMembers, which takes name, grade and salary as parameters and an array list called tasksToDo, which takes taskName, taskGrade and taskHours as parameters.



One of the requirements is to enable the managers to click a JButton and this will only display staffMembers whose grade is equal to or greater than the taskGrade for the selected task (this is selected using JComboBox). the result will be displayed in a JTable.



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


However, the code above doesn't give the required result and I hoped someone my be able to point me in the right direction.










share|improve this question















I'm currently carrying out a task, where I have to create a software development solution to allow managers to add new staff and tasks. for this, I have an ArrayList called staffMembers, which takes name, grade and salary as parameters and an array list called tasksToDo, which takes taskName, taskGrade and taskHours as parameters.



One of the requirements is to enable the managers to click a JButton and this will only display staffMembers whose grade is equal to or greater than the taskGrade for the selected task (this is selected using JComboBox). the result will be displayed in a JTable.



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


However, the code above doesn't give the required result and I hoped someone my be able to point me in the right direction.







java swing arraylist jtable jcombobox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 14:10









Andrew Thompson

152k27162336




152k27162336










asked Nov 22 at 14:05









smokinraygunn

11




11












  • What result does it give?
    – Jason Armstrong
    Nov 22 at 14:13






  • 1




    Share the results of your code and add an actual question.
    – sechanakira
    Nov 22 at 14:15










  • It gives me a list of all staff, including those whose grade is less than the job grade
    – smokinraygunn
    Nov 22 at 14:15










  • Where exactly is this.jobGrade and what value does it have (initially and possibly reassigned)?
    – deHaar
    Nov 22 at 14:19












  • @deHaar this.jobGrade takes the value from the tasksToDo ArrayList once the task is created
    – smokinraygunn
    Nov 22 at 14:23


















  • What result does it give?
    – Jason Armstrong
    Nov 22 at 14:13






  • 1




    Share the results of your code and add an actual question.
    – sechanakira
    Nov 22 at 14:15










  • It gives me a list of all staff, including those whose grade is less than the job grade
    – smokinraygunn
    Nov 22 at 14:15










  • Where exactly is this.jobGrade and what value does it have (initially and possibly reassigned)?
    – deHaar
    Nov 22 at 14:19












  • @deHaar this.jobGrade takes the value from the tasksToDo ArrayList once the task is created
    – smokinraygunn
    Nov 22 at 14:23
















What result does it give?
– Jason Armstrong
Nov 22 at 14:13




What result does it give?
– Jason Armstrong
Nov 22 at 14:13




1




1




Share the results of your code and add an actual question.
– sechanakira
Nov 22 at 14:15




Share the results of your code and add an actual question.
– sechanakira
Nov 22 at 14:15












It gives me a list of all staff, including those whose grade is less than the job grade
– smokinraygunn
Nov 22 at 14:15




It gives me a list of all staff, including those whose grade is less than the job grade
– smokinraygunn
Nov 22 at 14:15












Where exactly is this.jobGrade and what value does it have (initially and possibly reassigned)?
– deHaar
Nov 22 at 14:19






Where exactly is this.jobGrade and what value does it have (initially and possibly reassigned)?
– deHaar
Nov 22 at 14:19














@deHaar this.jobGrade takes the value from the tasksToDo ArrayList once the task is created
– smokinraygunn
Nov 22 at 14:23




@deHaar this.jobGrade takes the value from the tasksToDo ArrayList once the task is created
– smokinraygunn
Nov 22 at 14:23












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You are setting model.setDataVector each time you get a match. Besides, (sm.grade > num1) must have >= comparing, if you want equal or greater grade.



Also, you shouldn't use get(i) multiple times, do it once and store in a variable for any further actions. Or just use forEach.(Or lambda if You use java8+)



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

List<Object> rowData = new ArrayList<>();
Object member;
for (StaffMember sm:staffMembers) {
if (sm.grade >= num1) {
rowData.add(new Object {(Object)sm.name, (Object)sm.grade,
(Object)sm.getCost()});
}
}
model.setDataVector(rowData.toArray(new Object[rowData.size()]), colNames);





share|improve this answer





















  • Thank you very much for your suggestion. However, I still get the same result.
    – smokinraygunn
    Nov 22 at 14:36


















up vote
0
down vote













Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


Don't use code like the above. You are reserving space in your 2D array assuming all staff members are selected. What if only the first and last meet your criteria? You will have a bunch of empty lines in the middle of the table.



Instead you should just dynamically add a row of data to the model when your specific criteria is met. Something like:



String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = new DefaultTableModel(colNames, 0); // create empty model


for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {
Vector<Object> row = new Vector<Object>(3);
row.add( staffMembers.get(i).name );
row.add( staffMembers.get(i).grade );
row.add( staffMembers.get(i).getCost() );
model.addRow( row );
}
}

staffByTaskGradeTable.setModel( model );


You still need to debug why your "if condition" doesn't do what you expect.



Or how do you know that you are replacing the data in the model of the table that is visible on the frame. Maybe you have to instance of the table and only one is visible on the frame. A simple test it to just replace all the code in your method with:



DefaultTableModel model = new DefaultTableModel(5, 5); 
staffByTaskGradeTable.setModel(model);


Now you should see an empty table with 5 row and 5 columns. If this works the problem is with your "if condition". The data is not what you expect.






share|improve this answer























  • Thanks for your advice. I think my issue is getting the value of jobGrade when selecting it in the jComboBox. Assuming that the objects are converted to strings and I am trying to get an int value. Does that make sense? My terminology isn't the best as I'm relatively new and just learning the basics of Swing
    – smokinraygunn
    Nov 23 at 14:35










  • Assuming that the objects are converted to strings - why would it be converted to a string? If the value is an int then it should be stored in your object as an int so you can do comparison with your "num1" variable which is an int. If your object stores the value as a string then you first need to convert it to an int by using the Integer.parseInt(...) method.
    – camickr
    Nov 23 at 15:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432712%2fhow-to-display-specific-items-from-an-arraylist-based-on-a-value-from-another-ar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













You are setting model.setDataVector each time you get a match. Besides, (sm.grade > num1) must have >= comparing, if you want equal or greater grade.



Also, you shouldn't use get(i) multiple times, do it once and store in a variable for any further actions. Or just use forEach.(Or lambda if You use java8+)



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

List<Object> rowData = new ArrayList<>();
Object member;
for (StaffMember sm:staffMembers) {
if (sm.grade >= num1) {
rowData.add(new Object {(Object)sm.name, (Object)sm.grade,
(Object)sm.getCost()});
}
}
model.setDataVector(rowData.toArray(new Object[rowData.size()]), colNames);





share|improve this answer





















  • Thank you very much for your suggestion. However, I still get the same result.
    – smokinraygunn
    Nov 22 at 14:36















up vote
0
down vote













You are setting model.setDataVector each time you get a match. Besides, (sm.grade > num1) must have >= comparing, if you want equal or greater grade.



Also, you shouldn't use get(i) multiple times, do it once and store in a variable for any further actions. Or just use forEach.(Or lambda if You use java8+)



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

List<Object> rowData = new ArrayList<>();
Object member;
for (StaffMember sm:staffMembers) {
if (sm.grade >= num1) {
rowData.add(new Object {(Object)sm.name, (Object)sm.grade,
(Object)sm.getCost()});
}
}
model.setDataVector(rowData.toArray(new Object[rowData.size()]), colNames);





share|improve this answer





















  • Thank you very much for your suggestion. However, I still get the same result.
    – smokinraygunn
    Nov 22 at 14:36













up vote
0
down vote










up vote
0
down vote









You are setting model.setDataVector each time you get a match. Besides, (sm.grade > num1) must have >= comparing, if you want equal or greater grade.



Also, you shouldn't use get(i) multiple times, do it once and store in a variable for any further actions. Or just use forEach.(Or lambda if You use java8+)



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

List<Object> rowData = new ArrayList<>();
Object member;
for (StaffMember sm:staffMembers) {
if (sm.grade >= num1) {
rowData.add(new Object {(Object)sm.name, (Object)sm.grade,
(Object)sm.getCost()});
}
}
model.setDataVector(rowData.toArray(new Object[rowData.size()]), colNames);





share|improve this answer












You are setting model.setDataVector each time you get a match. Besides, (sm.grade > num1) must have >= comparing, if you want equal or greater grade.



Also, you shouldn't use get(i) multiple times, do it once and store in a variable for any further actions. Or just use forEach.(Or lambda if You use java8+)



private void jButtonSelectTaskActionPerformed(java.awt.event.ActionEvent evt) {                                                  

int num1 = this.jobGrade;//use the int supplied
//by user
String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = (DefaultTableModel) staffByTaskGradeTable.getModel();

List<Object> rowData = new ArrayList<>();
Object member;
for (StaffMember sm:staffMembers) {
if (sm.grade >= num1) {
rowData.add(new Object {(Object)sm.name, (Object)sm.grade,
(Object)sm.getCost()});
}
}
model.setDataVector(rowData.toArray(new Object[rowData.size()]), colNames);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 14:26









Alermikon

214




214












  • Thank you very much for your suggestion. However, I still get the same result.
    – smokinraygunn
    Nov 22 at 14:36


















  • Thank you very much for your suggestion. However, I still get the same result.
    – smokinraygunn
    Nov 22 at 14:36
















Thank you very much for your suggestion. However, I still get the same result.
– smokinraygunn
Nov 22 at 14:36




Thank you very much for your suggestion. However, I still get the same result.
– smokinraygunn
Nov 22 at 14:36












up vote
0
down vote













Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


Don't use code like the above. You are reserving space in your 2D array assuming all staff members are selected. What if only the first and last meet your criteria? You will have a bunch of empty lines in the middle of the table.



Instead you should just dynamically add a row of data to the model when your specific criteria is met. Something like:



String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = new DefaultTableModel(colNames, 0); // create empty model


for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {
Vector<Object> row = new Vector<Object>(3);
row.add( staffMembers.get(i).name );
row.add( staffMembers.get(i).grade );
row.add( staffMembers.get(i).getCost() );
model.addRow( row );
}
}

staffByTaskGradeTable.setModel( model );


You still need to debug why your "if condition" doesn't do what you expect.



Or how do you know that you are replacing the data in the model of the table that is visible on the frame. Maybe you have to instance of the table and only one is visible on the frame. A simple test it to just replace all the code in your method with:



DefaultTableModel model = new DefaultTableModel(5, 5); 
staffByTaskGradeTable.setModel(model);


Now you should see an empty table with 5 row and 5 columns. If this works the problem is with your "if condition". The data is not what you expect.






share|improve this answer























  • Thanks for your advice. I think my issue is getting the value of jobGrade when selecting it in the jComboBox. Assuming that the objects are converted to strings and I am trying to get an int value. Does that make sense? My terminology isn't the best as I'm relatively new and just learning the basics of Swing
    – smokinraygunn
    Nov 23 at 14:35










  • Assuming that the objects are converted to strings - why would it be converted to a string? If the value is an int then it should be stored in your object as an int so you can do comparison with your "num1" variable which is an int. If your object stores the value as a string then you first need to convert it to an int by using the Integer.parseInt(...) method.
    – camickr
    Nov 23 at 15:30















up vote
0
down vote













Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


Don't use code like the above. You are reserving space in your 2D array assuming all staff members are selected. What if only the first and last meet your criteria? You will have a bunch of empty lines in the middle of the table.



Instead you should just dynamically add a row of data to the model when your specific criteria is met. Something like:



String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = new DefaultTableModel(colNames, 0); // create empty model


for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {
Vector<Object> row = new Vector<Object>(3);
row.add( staffMembers.get(i).name );
row.add( staffMembers.get(i).grade );
row.add( staffMembers.get(i).getCost() );
model.addRow( row );
}
}

staffByTaskGradeTable.setModel( model );


You still need to debug why your "if condition" doesn't do what you expect.



Or how do you know that you are replacing the data in the model of the table that is visible on the frame. Maybe you have to instance of the table and only one is visible on the frame. A simple test it to just replace all the code in your method with:



DefaultTableModel model = new DefaultTableModel(5, 5); 
staffByTaskGradeTable.setModel(model);


Now you should see an empty table with 5 row and 5 columns. If this works the problem is with your "if condition". The data is not what you expect.






share|improve this answer























  • Thanks for your advice. I think my issue is getting the value of jobGrade when selecting it in the jComboBox. Assuming that the objects are converted to strings and I am trying to get an int value. Does that make sense? My terminology isn't the best as I'm relatively new and just learning the basics of Swing
    – smokinraygunn
    Nov 23 at 14:35










  • Assuming that the objects are converted to strings - why would it be converted to a string? If the value is an int then it should be stored in your object as an int so you can do comparison with your "num1" variable which is an int. If your object stores the value as a string then you first need to convert it to an int by using the Integer.parseInt(...) method.
    – camickr
    Nov 23 at 15:30













up vote
0
down vote










up vote
0
down vote









Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


Don't use code like the above. You are reserving space in your 2D array assuming all staff members are selected. What if only the first and last meet your criteria? You will have a bunch of empty lines in the middle of the table.



Instead you should just dynamically add a row of data to the model when your specific criteria is met. Something like:



String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = new DefaultTableModel(colNames, 0); // create empty model


for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {
Vector<Object> row = new Vector<Object>(3);
row.add( staffMembers.get(i).name );
row.add( staffMembers.get(i).grade );
row.add( staffMembers.get(i).getCost() );
model.addRow( row );
}
}

staffByTaskGradeTable.setModel( model );


You still need to debug why your "if condition" doesn't do what you expect.



Or how do you know that you are replacing the data in the model of the table that is visible on the frame. Maybe you have to instance of the table and only one is visible on the frame. A simple test it to just replace all the code in your method with:



DefaultTableModel model = new DefaultTableModel(5, 5); 
staffByTaskGradeTable.setModel(model);


Now you should see an empty table with 5 row and 5 columns. If this works the problem is with your "if condition". The data is not what you expect.






share|improve this answer














Object rowData = new Object[staffMembers.size()][3];
for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {

rowData[i][0] = staffMembers.get(i).name;
rowData[i][1] = staffMembers.get(i).grade;
rowData[i][2] = staffMembers.get(i).getCost();

model.setDataVector(rowData, colNames);
}
}


Don't use code like the above. You are reserving space in your 2D array assuming all staff members are selected. What if only the first and last meet your criteria? You will have a bunch of empty lines in the middle of the table.



Instead you should just dynamically add a row of data to the model when your specific criteria is met. Something like:



String colNames = {"Name", "Grade", "Montlhly Salary/Hourly Rate"};
DefaultTableModel model = new DefaultTableModel(colNames, 0); // create empty model


for (int i = 0; i < staffMembers.size(); i++) {
if (staffMembers.get(i).grade > num1) {
Vector<Object> row = new Vector<Object>(3);
row.add( staffMembers.get(i).name );
row.add( staffMembers.get(i).grade );
row.add( staffMembers.get(i).getCost() );
model.addRow( row );
}
}

staffByTaskGradeTable.setModel( model );


You still need to debug why your "if condition" doesn't do what you expect.



Or how do you know that you are replacing the data in the model of the table that is visible on the frame. Maybe you have to instance of the table and only one is visible on the frame. A simple test it to just replace all the code in your method with:



DefaultTableModel model = new DefaultTableModel(5, 5); 
staffByTaskGradeTable.setModel(model);


Now you should see an empty table with 5 row and 5 columns. If this works the problem is with your "if condition". The data is not what you expect.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 15:22

























answered Nov 22 at 15:07









camickr

273k15126238




273k15126238












  • Thanks for your advice. I think my issue is getting the value of jobGrade when selecting it in the jComboBox. Assuming that the objects are converted to strings and I am trying to get an int value. Does that make sense? My terminology isn't the best as I'm relatively new and just learning the basics of Swing
    – smokinraygunn
    Nov 23 at 14:35










  • Assuming that the objects are converted to strings - why would it be converted to a string? If the value is an int then it should be stored in your object as an int so you can do comparison with your "num1" variable which is an int. If your object stores the value as a string then you first need to convert it to an int by using the Integer.parseInt(...) method.
    – camickr
    Nov 23 at 15:30


















  • Thanks for your advice. I think my issue is getting the value of jobGrade when selecting it in the jComboBox. Assuming that the objects are converted to strings and I am trying to get an int value. Does that make sense? My terminology isn't the best as I'm relatively new and just learning the basics of Swing
    – smokinraygunn
    Nov 23 at 14:35










  • Assuming that the objects are converted to strings - why would it be converted to a string? If the value is an int then it should be stored in your object as an int so you can do comparison with your "num1" variable which is an int. If your object stores the value as a string then you first need to convert it to an int by using the Integer.parseInt(...) method.
    – camickr
    Nov 23 at 15:30
















Thanks for your advice. I think my issue is getting the value of jobGrade when selecting it in the jComboBox. Assuming that the objects are converted to strings and I am trying to get an int value. Does that make sense? My terminology isn't the best as I'm relatively new and just learning the basics of Swing
– smokinraygunn
Nov 23 at 14:35




Thanks for your advice. I think my issue is getting the value of jobGrade when selecting it in the jComboBox. Assuming that the objects are converted to strings and I am trying to get an int value. Does that make sense? My terminology isn't the best as I'm relatively new and just learning the basics of Swing
– smokinraygunn
Nov 23 at 14:35












Assuming that the objects are converted to strings - why would it be converted to a string? If the value is an int then it should be stored in your object as an int so you can do comparison with your "num1" variable which is an int. If your object stores the value as a string then you first need to convert it to an int by using the Integer.parseInt(...) method.
– camickr
Nov 23 at 15:30




Assuming that the objects are converted to strings - why would it be converted to a string? If the value is an int then it should be stored in your object as an int so you can do comparison with your "num1" variable which is an int. If your object stores the value as a string then you first need to convert it to an int by using the Integer.parseInt(...) method.
– camickr
Nov 23 at 15:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432712%2fhow-to-display-specific-items-from-an-arraylist-based-on-a-value-from-another-ar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Trompette piccolo

How do I get these specific pathlines to nodes?

What visual should I use to simply compare current year value vs last year in Power BI desktop