-1

My problem is that I have a JTable with 2 vector data for data & column names respectively,

Vector<String> rowOne = new Vector<>(); 
rowOne.addElement("Harry");
rowOne.addElement("100414");
rowOne.addElement("21");
rowOne.addElement("239438"); 
rowOne.addElement("08/21/2016");
rowOne.addElement("30000");

Vector<String> rowTwo = new Vector<>();
rowTwo.addElement("Gordon");
rowTwo.addElement("34353");
rowTwo.addElement("25");
rowTwo.addElement("2538"); 
rowTwo.addElement("07/ 16/2016");
rowTwo.addElement("20000");

Vector<Vector> rowData = new Vector<>();
rowData.addElement(rowOne);
rowData.addElement(rowTwo);

Vector<String> columnNames = new Vector<>();
columnNames.addElement("Name");
columnNames.addElement("Cc");
columnNames.addElement("Age");
columnNames.addElement("Phone");
columnNames.addElement("Date");
columnNames.addElement("Amount");

DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
table = new JTable(model);

As we can see is a normal JTable with 6 columns, in which the fourth one is a string representing a date.

I tried to add a button with a action listener like this, in order to iterate over all rows of the JTable at the column 4 in order to get all string date values to compare it with the current date to verify if they're older than 30 days.

public void actionPerformed(ActionEvent e)
    {

else if(e.getActionCommand().equals("limPago"))
        {
            DefaultTableModel model = (DefaultTableModel)table.getModel();
            int num = model.getRowCount();

            for(int i = 0; i < num; i++)
            {
             //Right here i don't know how to identity the columns with date greater
             //than 30 days and take the jtable index corresponding to that row.
            }
    }

I need to get the index row corresponding to the Object of JTable that have the column 4 date with 30 days older.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Cohen
  • 79
  • 1
  • 9
  • (1-), Start "accepting" answers in other questions (http://stackoverflow.com/questions/39060135/error-deleting-last-row-jtable) BEFORE you start posting new questions. Once again you haven't posted a SSCCE. Also not the code you posted in your last question isn't even the code you are executing in your program so how do we know the code you posted here is the code you are executing. That is another reason why a SSCCE should be posted with every question. – camickr Aug 22 '16 at 01:04

1 Answers1

2

Add instances of java.util.Date to your TableModel, and return the correct type in your implementation of getColumnClass(), as shown here. Date implements Comparable<T>, making the comparison easy. Starting from the example and adding 32 daily rows, the following Action prints rows greater than or equal to then, which has been initalized to a date 30 days hence:

Console:

30 Wed Sep 21 19:34:35 EDT 2016
31 Thu Sep 22 19:34:35 EDT 2016

Code:

for (int i = 1; i <= 32; i++) {…}
…
f.add(new JButton(new AbstractAction("Scan") {
    @Override
    public void actionPerformed(ActionEvent e) {
        Calendar then = Calendar.getInstance();
        then.add(Calendar.DAY_OF_YEAR, 30);
        for (int i = 0; i < model.getRowCount(); i++) {
            Date d = (Date) model.getValueAt(i, 1);
            if (d.compareTo(then.getTime()) > -1) {
                System.out.println(i + " " + d);
            }
        }
    }
}), BorderLayout.PAGE_END);

Change the predicate to d.compareTo(then.getTime()) < 1 to see the rows before then.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045