Reputation: 11
I am making a Java GUI with MySQL database for attendance recording. I have created a JFrame
for adding new attendance. When user selects a subject name from the JComboBox
the code verifies the subject name then gets the dates from the database and highlight the dates in JCalendar
showing the user about that particular subject's lecture days.
The issue I am facing is that when the user selects another item/subject from the combo box, the frame should de-highlight the previous dates and only highlight the new ones. I am struggling with it. Haven't found anything on it.
My Code:
(HighlightTest
Class)
import com.toedter.calendar.IDateEvaluator;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class HighlightTest {
public static class HighlightEvaluator implements IDateEvaluator {
private final List<Date> list = new ArrayList<>();
public void add(Date date) {
list.add(date);
}
@Override
public boolean isSpecial(Date date) {
return list.contains(date);
}
@Override
public Color getSpecialForegroundColor() {
return Color.WHITE;
}
@Override
public Color getSpecialBackroundColor() {
return Color.GREEN;
}
@Override
public String getSpecialTooltip() {
return "Highlighted event.";
}
@Override
public boolean isInvalid(Date date) {
return false;
}
@Override
public Color getInvalidForegroundColor() {
return null;
}
@Override
public Color getInvalidBackroundColor() {
return null;
}
@Override
public String getInvalidTooltip() {
return null;
}
}
}
(Main JFrame
Class)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
public final class framer extends javax.swing.JFrame {
/**
* Creates new form framer
*/
public void subjectsFillData(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbdb?useSSL=false","root","");
Statement stat = con.createStatement();
String selectQuery="select SubjectName from subjects";
ResultSet rs=stat.executeQuery(selectQuery);
while(rs.next()){
subjects.addItem(rs.getString("SubjectName"));
}
}
catch(Exception e){
System.out.println(e);
}
}
private Date createDate(int d) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, d);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return (c.getTime());
}
public framer() {
initComponents();
subjectsFillData();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
calEn = new com.toedter.calendar.JCalendar();
sButton = new javax.swing.JButton();
subjects = new javax.swing.JComboBox<>();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
sButton.setText("Show");
sButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sButtonActionPerformed(evt);
}
});
subjects.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
subjectsItemStateChanged(evt);
}
});
//java-swing-code-here
private void sButtonActionPerformed(java.awt.event.ActionEvent evt) {
String subject = (String) subjects.getSelectedItem();
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbdb?useSSL=false","root","");
Statement stat = con.createStatement();
String searchQuery="select * from "+subject+" ";
ResultSet rs=stat.executeQuery(searchQuery);
while (rs.next()){
String ds = rs.getString(2);
String s = ds.split("-")[0];
int d = Integer.parseInt(s);
HighlightTest.HighlightEvaluator evaluator = new HighlightTest.HighlightEvaluator();
evaluator.add(createDate(d));
evaluator.add(createDate(d));
calEn.getDayChooser().addDateEvaluator(evaluator);
calEn.setCalendar(calEn.getCalendar());
}
}
catch(Exception e){
System.out.println(e);
}
}
private void subjectsItemStateChanged(java.awt.event.ItemEvent evt) {
**// Check if the item is changed and de-highlight previous dates and highlight new ones.**
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
//GUIeditorfold-code-here
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new framer().setVisible(true);
}
});
}
// Variables declaration - do not modify
private com.toedter.calendar.JCalendar calEn;
private javax.swing.JButton sButton;
private javax.swing.JComboBox<String> subjects;
// End of variables declaration
}
GUI:
Upvotes: 1
Views: 63