Search code examples
javaswingeventsawtjscrollbar

how to store the final value of a variable which changes after an event is performed in Java


I'm developing a Java Sleep Cycle Calculator Application using Swing and AWT components, but I'm stuck here.

I'm using integer variables a and b to store the 'hour' and 'minute' values of time the user sets on the Scrollbar, which I'll use later to do some calculations and display some data.

But the value of a and b are automatically being set to 0, and the calculations are being done without waiting for the adjustment event on the scrollbar to be performed.

How to update the values of a and b after the event is performed and then use them for my calculations ?

this is where I'm setting the time a=23 and b=41

but the calculations are performed with values a=0 and b=0

Here is the Code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Sleep {
    JFrame f;
    JButton b1;
    JLabel l3;
    JLabel l4;
    JLabel l5;
    JLabel l6;
    JButton b2;
    JLabel l7;
    JButton b3;
    int a,b;
    Sleep() {
        f=new JFrame();

        JPanel p2=new JPanel();
        l3=new JLabel();
        l3.setText("***************** RAZHA'S SLEEP CYCLE CALCULATOR *****************");
        l4=new JLabel();
        l4.setText("Tired of waking up exhausted ???");
        l5=new JLabel("This Application calculates sleep cycles and finds the best time to wake up");
        l6=new JLabel("Waking you up at the most perfect time, feeling rested!");
        p2.add(l3); p2.add(l4); p2.add(l5); p2.add(l6);
        p2.setBounds(700,100,500,100);

        b1=new JButton("I WANT TO SLEEP AT");
        b1.setBounds(800,350,300,150);


        JPanel p1=new JPanel();
        JLabel l1=new JLabel("Select Time (HH-MM)");
                final JLabel label1 = new JLabel();          
            label1.setHorizontalAlignment(JLabel.CENTER);    
            label1.setSize(400,100);  
        final JLabel label2 = new JLabel();          
            label2.setHorizontalAlignment(JLabel.CENTER);    
            label2.setSize(400,100);  
            final JScrollBar hour=new JScrollBar(JScrollBar.VERTICAL, 2, 0, 0, 23);  
            final JScrollBar min=new JScrollBar(JScrollBar.VERTICAL, 2, 0, 0, 59);  
        hour.addAdjustmentListener(new AdjustmentListener() {  
                public void adjustmentValueChanged(AdjustmentEvent e) {  
                final int c=hour.getValue();
                    label1.setText("Hours : "+ c); 
                System.out.println("hr "+c);
                a=c;
                }
        });
        min.addAdjustmentListener(new AdjustmentListener() {  
                public void adjustmentValueChanged(AdjustmentEvent e) {  
                final int d=min.getValue();
                    label2.setText("Minutes : "+ d);
                System.out.println("min "+d);
                b=d;
                }
        }); 
        p1.add(l1); p1.add(hour); p1.add(min); p1.add(label1); p1.add(label2);
        p1.setBounds(780,550,340,80);


        l7=new JLabel("THE BEST TIMES TO WAKE UP ARE : ");
        DefaultListModel<String> lx = new DefaultListModel<>();  
            lx.addElement(((a+1)+(b+44)/60)+":"+((b+44)%60)+"        -       "+"Nap for 1 hr 44 min");  
            lx.addElement(((a+3)+(b+14)/60)+":"+((b+14)%60)+"        -       "+"Nap for 3 hrs 14 min");  
            lx.addElement(((a+4)+(b+44)/60)+":"+((b+44)%60)+"        -       "+"Nap for 4 hrs 44 min");  
            lx.addElement(((a+6)+(b+14)/60)+":"+((b+14)%60)+"        -       "+"Nap for 6 hrs 14 min");  
        lx.addElement(((a+7)+(b+44)/60)+":"+((b+44)%60)+"        -       "+"Nap for 7 hrs 44 min");
        lx.addElement(((a+9)+(b+14)/60)+":"+((b+14)%60)+"        -       "+"Nap for 9 hrs 14 min");
        lx.addElement(((a+10)+(b+44)/60)+":"+((b+44)%60)+"      -       "+"Nap for 10 hrs 44 min");
        lx.addElement(((a+12)+(b+14)/60)+":"+((b+14)%60)+"      -       "+"Nap for 12 hrs 14 min");
            JList<String> list = new JList<>(lx);  
        list.setFixedCellHeight(48);
            list.setFixedCellWidth(200);


        JPanel p3=new JPanel();
        p3.setBounds(800,350,300,420);
        p3.add(l7);
        p3.add(list);  
        p3.setVisible(false);

        b3=new JButton("EXIT");
        b3.setBounds(900,800,100,50);
        b3.setVisible(false);
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    System.exit(0);
            } 
        });

        b2=new JButton("SUBMIT");
        b2.setBounds(900,650,100,50);
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    b2.setVisible(false); 
                b1.setVisible(false); 
                p1.setVisible(false);
                p3.setVisible(true);
                b3.setVisible(true);
            } 
        });
        
        f.add(b1);
        f.add(b2);
        f.add(p1);
        f.add(p2);
        f.add(p3);
        f.add(b3);
        f.setSize(400,400);
        f.getContentPane().setBackground(Color.BLUE);
        f.setLayout(null);
        f.setVisible(true);
    }
    public static void main(String args[])
    {
        new Sleep();        
    }
}

Please help me out.


Solution

  • The JList component is initialized once with a ListModel that is created when a and b are zero. When the values of a and b change at a later point in time, the list model is not changed automatically. The JList component needs to be explicitly updated with a new model when the user clicks the submit button (b2).

    Something like this:

    Sleep() {
        // [...]
    
        l7=new JLabel("THE BEST TIMES TO WAKE UP ARE : ");
        JList<String> list = new JList<>();
        updateTimesListModel(list);
        list.setFixedCellHeight(48);
        list.setFixedCellWidth(200);
    
        // [...]
    
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateTimesListModel(list);
                b2.setVisible(false);
                b1.setVisible(false);
                p1.setVisible(false);
                p3.setVisible(true);
                b3.setVisible(true);
            }
        });
    
        // [...]
    }
    
    private void updateTimesListModel(JList<String> list) {
        DefaultListModel<String> lx = new DefaultListModel<>();
        lx.addElement(((a+1)+(b+44)/60)+":"+((b+44)%60)+"        -       "+"Nap for 1 hr 44 min");
        lx.addElement(((a+3)+(b+14)/60)+":"+((b+14)%60)+"        -       "+"Nap for 3 hrs 14 min");
        lx.addElement(((a+4)+(b+44)/60)+":"+((b+44)%60)+"        -       "+"Nap for 4 hrs 44 min");
        lx.addElement(((a+6)+(b+14)/60)+":"+((b+14)%60)+"        -       "+"Nap for 6 hrs 14 min");
        lx.addElement(((a+7)+(b+44)/60)+":"+((b+44)%60)+"        -       "+"Nap for 7 hrs 44 min");
        lx.addElement(((a+9)+(b+14)/60)+":"+((b+14)%60)+"        -       "+"Nap for 9 hrs 14 min");
        lx.addElement(((a+10)+(b+44)/60)+":"+((b+44)%60)+"      -       "+"Nap for 10 hrs 44 min");
        lx.addElement(((a+12)+(b+14)/60)+":"+((b+14)%60)+"      -       "+"Nap for 12 hrs 14 min");
        list.setModel(lx);
    }