I am trying to make an attendance program in Java Swing and I try to count the number of Boolean values in every column of the first table as the number of days attended except for the 0 column index (since it contained the string values for the names) in a button click . I try to get the count of the Boolean values using a nested loop. Now when I try to use setValueAt
to the cells of the other table which shows the number of days absent and present, it doesn't seem to work. Am I missing something here?
int mainTRow = mainTable.getRowCount();
int mainTCol = mainTable.getColumnCount();
JButton totBTN = new JButton("Total");
totBTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel mdl1 = (DefaultTableModel)showTable.getModel();
int numT=0, numF=0;
for (int i1=0; i1<mainTRow; i1 ) {
for (int y=0; y<mainTCol; y ) {
Boolean obj = GetData(mainTable, i1, y 1);
if (obj == true) {
numT ;
}
else {
numF ;
}
mdl1.setValueAt(numT, i1, 1);
mdl1.setValueAt(numF, i1, 2);
}
}
}
});
Also, here is the method of the GetData
used.
public Boolean GetData(JTable mainTable, int row_index, int col_index){
return (Boolean) mainTable.getModel().getValueAt(row_index, col_index);
}
CodePudding user response:
This is basically a rehash of the example provided in your last question,
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
private DefaultTableModel leftModel = new DefaultTableModel(new String[] {"Name", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return String.class;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6: return Boolean.class;
}
return Object.class;
}
};
private DefaultTableModel rightModel = new DefaultTableModel(new String[]{"Name", "Attended", "Absent"}, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return String.class;
case 1:
case 2: return Integer.class;
}
return Object.class;
}
};
public MainPane() {
setLayout(new GridBagLayout());
JTable leftTable = new JTable(leftModel);
JTable rightTable = new JTable(rightModel);
reload();
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
JButton calculateButton = new JButton(">");
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int rowCount = leftModel.getRowCount();
int colCount = leftModel.getColumnCount();
rightModel.setRowCount(rowCount);
for (int row = 0; row < rowCount; row ) {
int attendedCount = 0;
int absentCount = 0;
for (int col = 1; col < colCount; col ) {
Object value = leftModel.getValueAt(row, col);
if (value instanceof Boolean) {
boolean bValue = (boolean) value;
if (bValue) {
attendedCount ;
} else {
absentCount ;
}
}
}
// Technically, you could calculate the absentCount by
// using the column count - 1 and subtracting the attendedCount
// from it
rightModel.setValueAt(leftModel.getValueAt(row, 0), row, 0);
rightModel.setValueAt(attendedCount, row, 1);
rightModel.setValueAt(absentCount, row, 2);
}
}
});
JButton reloadButton = new JButton("Reload");
reloadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reload();
}
});
add(new JScrollPane(leftTable), gbc);
gbc.gridx = 2;
add(new JScrollPane(rightTable), gbc);
gbc.gridx = 1;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.VERTICAL;
JPanel actions = new JPanel(new GridBagLayout());
add(actions, gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
actions.add(calculateButton, gbc);
actions.add(reloadButton, gbc);
}
protected void reload() {
leftModel.setRowCount(0);
rightModel.setRowCount(0);
Random rnd = new Random();
for (String[] name : NAMES) {
Object[] rowData = new Object[6];
rowData[0] = name[0] " " name[1];
for (int index = 1; index < 6; index ) {
rowData[index] = rnd.nextBoolean();
}
leftModel.addRow(rowData);
}
}
}
public static final String[][] NAMES = new String[][]{
new String[]{"Laura", "Williams"},
new String[]{"Andrea", "Ellis"},
new String[]{"John", "King"},
new String[]{"Kenneth", "Garcia"},
new String[]{"William", "Miller"},
new String[]{"William", "Benitez"},
new String[]{"Michelle", "Hansen"},
new String[]{"Nicholas", "Jones"},
new String[]{"Joshua", "Sanchez"},
new String[]{"Stephen", "Herring"},
new String[]{"Kyle", "Wallace"},
new String[]{"Oscar", "Wong"},
new String[]{"Shane", "Santana"},
new String[]{"Jeffrey", "Weber"},
new String[]{"Laura", "Hale"},
new String[]{"Dr.", "Henry"},
new String[]{"Tammy", "Mathis"},
new String[]{"Jennifer", "Rodriguez"},
new String[]{"Joshua", "Tucker"},
new String[]{"Alejandra", "Wong"},
new String[]{"Barbara", "Flores"},
new String[]{"Kristin", "Sims"},
new String[]{"Stephanie", "Green"},
new String[]{"Travis", "Parks"},
new String[]{"Brian", "Meyers"},
new String[]{"Haley", "Casey"},
new String[]{"Laura", "Wilson"},
new String[]{"Sharon", "Berg"},
new String[]{"Joshua", "Warren"},
new String[]{"William", "Martin"},
new String[]{"David", "Ramos"},
new String[]{"Jessica", "Dennis"},
new String[]{"Joel", "Ferrell"},
new String[]{"Michael", "Johnson"},
new String[]{"Kim", "Watkins"},
new String[]{"Loretta", "Reed"},
new String[]{"Jeffrey", "Williams"},
new String[]{"Jennifer", "Hale"},
new String[]{"Alicia", "Padilla"},
new String[]{"Ian", "Wagner"},
new String[]{"Jasmine", "Wheeler"},
new String[]{"Cynthia", "Aguilar"},
new String[]{"Justin", "Flores"},
new String[]{"Mitchell", "Stephens"},
new String[]{"Kristi", "Rodriguez"},
new String[]{"Renee", "Young"},
new String[]{"Shane", "Simmons"},
new String[]{"Beverly", "Werner"},
new String[]{"Jordan", "Townsend"},
new String[]{"Carrie", "Solomon"},
new String[]{"Jessica", "Martin"},
new String[]{"John", "Pearson"},
new String[]{"Steven", "Miranda"},
new String[]{"Jennifer", "Knight"},
new String[]{"Lindsay", "Martinez"},
new String[]{"Joshua", "Roy"},
new String[]{"Jerry", "Bailey"},
new String[]{"Lauren", "Barr"},
new String[]{"Frank", "Castaneda"},
new String[]{"Gary", "Franklin"},
new String[]{"Robert", "Lewis"},
new String[]{"Peter", "Vasquez"},
new String[]{"Brittany", "Rich"},
new String[]{"Jacob", "White"},
new String[]{"Anna", "Smith"},
new String[]{"Michelle", "Davis"},
new String[]{"Cesar", "Frank"},
new String[]{"Chad", "Walsh"},
new String[]{"Thomas", "Johnson"},
new String[]{"Susan", "Wilkerson"},
new String[]{"Hunter", "Garrett"},
new String[]{"Molly", "Hernandez"},
new String[]{"Gary", "Richmond"},
new String[]{"Megan", "Price"},
new String[]{"Daniel", "Mack"},
new String[]{"Margaret", "Andrade"},
new String[]{"Erika", "White"},
new String[]{"Laura", "Carr"},
new String[]{"Robin", "Schultz"},
new String[]{"Valerie", "King"},
new String[]{"Jacob", "Sherman"},
new String[]{"Monique", "King"},
new String[]{"Laura", "Strickland"},
new String[]{"Jonathan", "Zuniga"},
new String[]{"Danny", "Taylor"},
new String[]{"Darrell", "Reese"},
new String[]{"Juan", "Watkins"},
new String[]{"Valerie", "Cohen"},
new String[]{"David", "Ortiz"},
new String[]{"Catherine", "Hawkins"},
new String[]{"William", "Parker"},
new String[]{"Christine", "Freeman"},
new String[]{"Corey", "Keller"},
new String[]{"James", "Hicks"},
new String[]{"Nicole", "Petty"},
new String[]{"Alexandria", "Aguirre"},
new String[]{"Heather", "Kim"},
new String[]{"Nichole", "Palmer"},
new String[]{"Jonathan", "Moore"},
new String[]{"Cynthia", "Gibbs"}
};
}
As I said in the previous answer, if the structures of the two tables are different, you will need to convert the data between them.
If you still can't get it to work, you will need to provide a Minimal, Reproducible Example, just like I have now done, twice.