linkacademyswingproject.MainDisplay.java Source code

Java tutorial

Introduction

Here is the source code for linkacademyswingproject.MainDisplay.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package linkacademyswingproject;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.LocalDateTime;

/**
 *
 * @author adib
 */
public class MainDisplay extends javax.swing.JPanel {
    static String chosenType;
    static Date chosenDate;
    static int chosenTime = 0;
    static JColorChooser colorChooser;
    static Color chosenColor;
    static Timer tmp;

    /**
     * Creates new form MainDisplay
     */
    public MainDisplay() {
        initComponents();
    }

    /**
     * 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jSpinner1 = new javax.swing.JSpinner();
        jSpinner2 = new javax.swing.JSpinner();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        jCheckBox1.setText("on time:");
        jCheckBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBox1ItemStateChanged(evt);
            }
        });

        jCheckBox2.setText("countdown(mins)");
        jCheckBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBox2ItemStateChanged(evt);
            }
        });

        jSpinner1.setModel(
                new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.MINUTE));

        jSpinner2.setModel(new javax.swing.SpinnerNumberModel());

        jButton1.setText("Chose Color");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("no color chosen");

        jSlider1.setMaximum(3000);
        jSlider1.setMinimum(100);
        jSlider1.setMinorTickSpacing(1000);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.setToolTipText("Select Animation Time");

        jButton2.setText("Stop");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Start");
        jButton3.setToolTipText("");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jCheckBox1).addComponent(jCheckBox2).addComponent(jButton1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12,
                                        Short.MAX_VALUE)
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jSpinner1, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jSpinner2, javax.swing.GroupLayout.Alignment.TRAILING)))
                        .addGroup(layout.createSequentialGroup().addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jCheckBox1).addComponent(jSpinner1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jCheckBox2).addComponent(jSpinner2,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1).addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton3).addComponent(jButton2))
                        .addContainerGap(49, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox1ItemStateChanged
        // TODO add your handling code here:
        if (jCheckBox1.isSelected()) {
            MainDisplay.chosenType = "Date";
            jCheckBox2.setSelected(false);
            chosenDate = (Date) jSpinner1.getValue();
        } else {
            MainDisplay.chosenType = "Time";
            jCheckBox2.setSelected(true);
            chosenTime = (int) jSpinner2.getValue();
        }
    }//GEN-LAST:event_jCheckBox1ItemStateChanged

    private void jCheckBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox2ItemStateChanged
        // TODO add your handling code here:
        if (jCheckBox2.isSelected()) {
            MainDisplay.chosenType = "Time";
            jCheckBox1.setSelected(false);
            chosenTime = (int) jSpinner2.getValue();
        } else {
            MainDisplay.chosenType = "Date";
            jCheckBox1.setSelected(true);
            chosenDate = (Date) jSpinner1.getValue();
        }
    }//GEN-LAST:event_jCheckBox2ItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        MainDisplay.colorChooser = new JColorChooser();
        JDialog popup = JColorChooser.createDialog(null, "Choose Color", true, MainDisplay.colorChooser,
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        MainDisplay.chosenColor = MainDisplay.colorChooser.getColor();
                        jLabel1.setText("Color Selected");
                    }
                }, null);
        popup.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if ((chosenTime != 0) || (chosenType != null)) {
            if (chosenColor != null) {
                if (chosenType.equals("Time")) {
                    //                    int start = ((chosenTime)*60)*1000;
                    int start = chosenTime;
                    JPanel that = this;
                    tmp = new Timer(start, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            JFrame wind = new JFrame("Alarm");
                            wind.setLayout(new FlowLayout());
                            Timer tmp = new Timer(jSlider1.getValue(), new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    wind.getContentPane().setBackground(chosenColor);
                                    disbleComponents(that, true);
                                }
                            });
                            tmp.start();
                            wind.setSize(new Dimension(300, 300));
                            wind.setVisible(true);
                        }
                    });
                    tmp.setRepeats(false);
                    tmp.start();
                    disbleComponents(this, false);
                    jButton2.setEnabled(true);
                } else {
                    DateTime then = new DateTime(chosenDate.getTime());
                    DateTime now = new DateTime();
                    Duration diff = new Duration(now, then);
                    Long lWait = diff.getMillis();
                    Integer wait = Integer.valueOf(lWait.intValue());
                    if (wait > 0) {
                        JPanel that = this;
                        tmp = new Timer(wait, new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                JFrame wind = new JFrame("Alarm");
                                wind.setLayout(new FlowLayout());
                                Timer dur = new Timer(jSlider1.getValue(), new ActionListener() {

                                    @Override
                                    public void actionPerformed(ActionEvent e) {
                                        wind.getContentPane().setBackground(chosenColor);
                                        disbleComponents(that, true);
                                    }
                                });
                                dur.start();
                                wind.setSize(new Dimension(300, 300));
                                wind.setVisible(true);
                            }
                        });
                        tmp.setRepeats(false);
                        tmp.start();
                        disbleComponents(this, false);
                        jButton2.setEnabled(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "Date for alarm must be in the future");
                    }
                }
            } else {
                JOptionPane.showMessageDialog(null, "Must Select A color First");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Must Select Time First");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (chosenType.equals("Time")) {
            tmp.stop();
            disbleComponents(this, true);
        } else if (chosenType.equals("Date")) {
            tmp.stop();
            disbleComponents(this, true);
        } else {
            JOptionPane.showMessageDialog(null, "There is no action to cancel");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    static void disbleComponents(Container elem, boolean state) {
        Component[] group = elem.getComponents();
        for (Component component : group) {
            component.setEnabled(state);
            if (component instanceof Container) {
                disbleComponents((Container) component, state);
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    // End of variables declaration//GEN-END:variables
}