This is the date : String date = "01.11.2020";
Column1 : SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyy");
Column2 : DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
Column1 parsed with SimpleDateFormat and Column2 parsed with DateTimeFormatter.
//Column1 Column2
01 Kas 2020 2020-11-01
I have a JComboBox with items like this {"ALL", "LAST 1 WEEK", "LAST 1 MONTH", "LAST 1 YEAR"}
So when i want to see LAST 1 WEEK dates i use a code like this.
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(m1);
java.util.List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
table.setRowSorter(sorter);
filters.add(RowFilter.dateFilter(ComparisonType.AFTER, date,columnIndex));
RowFilter<Object,Object> serviceFilter = RowFilter.andFilter(filters);
sorter.setRowFilter(serviceFilter);
It works fine with Column1. But it does not sort Column2. I guess it does not accept it as a date.
Column1 is Date.Class* column in table model. Column2 is LocalDate.Class in table model.( I have tryed to make both Date.Class but Column2 gives error. ).
Is there any way i can use RowFilter.dateFilter on LocalDate.Class columns.
I wrote some codes to explain it with more details. Here is a class for example:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.RowFilter.ComparisonType;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
public class frm1 extends JFrame {
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frm1 frame = new frm1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws ParseException
*/
public frm1() throws ParseException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 501, 425);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
DefaultTableModel m1 = new DefaultTableModel() {
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Date.class;
case 1:
return LocalDate.class;
default:
return String.class;
}
}
public boolean isCellEditable(int row, int column) {
return false;
}
};
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 263, 325);
contentPane.add(scrollPane);
table = new JTable();
table.setBounds(329, 256, 1, 1);
//contentPane.add(table);
scrollPane.setViewportView(table);
JComboBox cbx_date = new JComboBox();
cbx_date.setModel(new DefaultComboBoxModel(new String[] {"ALL", "LAST 1 WEEK", "LAST 1 MONTH", "LAST 1 YEAR"}));
cbx_date.setBounds(283, 53, 163, 31);
contentPane.add(cbx_date);
JComboBox cbx_localdate = new JComboBox();
cbx_localdate.setModel(new DefaultComboBoxModel(new String[] {"ALL", "LAST 1 WEEK", "LAST 1 MONTH", "LAST 1 YEAR"}));
cbx_localdate.setBounds(283, 125, 163, 31);
contentPane.add(cbx_localdate);
JLabel lblNewLabel = new JLabel("With Date");
lblNewLabel.setBounds(283, 11, 163, 31);
contentPane.add(lblNewLabel);
JLabel lblWithLocaldate = new JLabel("With LocalDate");
lblWithLocaldate.setBounds(283, 95, 163, 31);
contentPane.add(lblWithLocaldate);
//Columns
table.setModel(m1);
Object[] columns = {"Date","Local Date"};
m1.setColumnIdentifiers(columns);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(m1);
java.util.List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
table.setRowSorter(sorter);
//Rows
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyy");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String[] dates = {"01.08.2020","05.08.2020","20.08.2020","21.08.2020","01.09.2020","15.09.2020","01.10.2020","15.10.2020","01.11.2020","01.08.2019","01.07.2019"};
Object[] rows = new Object[2];
for(int i=0;i<dates.length;i++) {
rows[0]=sdf.parse(dates[i]);
rows[1]=LocalDate.parse(dates[i],formatter);
m1.addRow(rows);
}
//Date state change event.
cbx_date.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
filters.clear();
String selected = cbx_date.getSelectedItem().toString();
if(!selected.equals("ALL")) {
filters.add(RowFilter.dateFilter(ComparisonType.AFTER, datereturn(selected),0));
filters.add(RowFilter.dateFilter(ComparisonType.BEFORE,new Date(),0));
}
RowFilter<Object,Object> serviceFilter = RowFilter.andFilter(filters);
sorter.setRowFilter(serviceFilter);
}
});
//LocalDate state change event.
cbx_localdate.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
filters.clear();
String selected = cbx_localdate.getSelectedItem().toString();
if(!selected.equals("ALL")) {
filters.add(RowFilter.dateFilter(ComparisonType.AFTER, datereturn(selected),1));
filters.add(RowFilter.dateFilter(ComparisonType.BEFORE,new Date(),1));
}
RowFilter<Object,Object> serviceFilter = RowFilter.andFilter(filters);
sorter.setRowFilter(serviceFilter);
}
});
}
//Returs the date as wanted.
public static Date datereturn (String selected) {
if(selected.equals("LAST 1 WEEK")) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -7);
cal.add(Calendar.DAY_OF_MONTH, -1);// make 1 less date so i can get the EQUALS day.Becouse of ComparisonType.AFTER
Date old = cal.getTime();
return old;
}else if(selected.equals("LAST 1 MONTH")) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH , -1);
cal.add(Calendar.DAY_OF_MONTH, -1);
Date old = cal.getTime();
return old;
}else if(selected.equals("LAST 1 YEAR")) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR , -1);
cal.add(Calendar.DAY_OF_MONTH, -1);
Date old = cal.getTime();
return old;
}else {
Calendar cal = Calendar.getInstance();
Date old = cal.getTime();
return old;
}
}
}
I finally figured out how to write a
RowFilterfor a LocalDate.When you create a GUI, you should use Swing layout managers. I used the
FlowLayout,BorderLayout, andGridBagLayoutto create the GUI. Absolute positioning is brittle and does not work well when you move from one computer to another with a different monitor or different operating system.Also, it helps a lot to separate your code into methods and classes. The smaller the method or class, the easier it is to test. Believe me, I ran hundreds of tests before I got the RowFilter to work correctly.
Here's the GUI I created.
Please review all of the code. The code with the custom
RowFilteris in theLocalDateItemListenerclass.