Results 1 to 7 of 7
  1. #1
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default Got Trouble with JSlider

    Arg!! I have a problem with slider like this

    i have 3 silder : year, month, day

    My teacher want when the user(Him) choose Febuary, day slider will stop at 28 or 29.

    Default : if choose Febuary or you choose other month that have 30 days, if you choose day 31, it will auto get value from day 1 at NEXT MONTH.

    HELP ME!! :( THANKS!!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    You forgot to post the code for your program.

  3. #3
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    here my code
    Sorry, i use netbean and do this with design, so the code too long, please help me! Thanks!



    /*
    * Assignment_2_VietHung.java
    *
    * Created on August 28, 2008, 1:17 AM
    */



    /**
    *
    * @author YoJimBo
    */

    import java.util.*;
    import javax.swing.JLabel;
    import java.text.*;
    import javax.swing.JFormattedTextField;
    import javax.swing.JOptionPane;
    import javax.swing.JSlider;
    import javax.swing.text.MaskFormatter;

    public class Assignment_2_VietHung extends javax.swing.JFrame {

    /** Creates new form Assignment_2_VietHung */
    public Assignment_2_VietHung() {
    initComponents();

    Hashtable lableTable = new Hashtable();
    lableTable.put(new Integer(1), new JLabel("Jan"));
    String month [] = {"Jan","Feb","Mar","Apr","May","June","July","Aug" ,"Sep","Oct","Nov","Dec"};
    for(int i = 0;i<month.length;i++)
    {
    lableTable.put(new Integer(i + 1), new JLabel(month[i]));
    }
    this.jSlider2.setLabelTable(lableTable);
    }

    public String format()
    {

    Date d = new Date(jSlider1.getValue()-1900,jSlider2.getValue()-1,jSlider3.getValue());
    /* if(jSlider2.getValue()==2 && jSlider3.getValue()>29)
    {

    JOptionPane.showMessageDialog(this,"asdasdasda");
    jSlider3.
    } */
    SimpleDateFormat sd = new SimpleDateFormat("E ,MMM dd,yyyy");
    return sd.format(d);
    }

    /** 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() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    jSlider1 = new javax.swing.JSlider();
    jPanel3 = new javax.swing.JPanel();
    jSlider2 = new javax.swing.JSlider();
    jPanel4 = new javax.swing.JPanel();
    jSlider3 = new javax.swing.JSlider();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("Assignment_2_VietHung"); // NOI18N

    jPanel1.setBorder(javax.swing.BorderFactory.create TitledBorder("SelectedDate"));
    jPanel1.setToolTipText("SelectedDate"); // NOI18N
    jPanel1.setPreferredSize(new java.awt.Dimension(500, 55));

    jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
    jLabel1.setHorizontalAlignment(javax.swing.SwingCo nstants.CENTER);
    jLabel1.setPreferredSize(new java.awt.Dimension(400, 40));
    jPanel1.add(jLabel1);

    jPanel2.setBorder(javax.swing.BorderFactory.create TitledBorder("Year"));
    jPanel2.setPreferredSize(new java.awt.Dimension(100, 70));

    jSlider1.setMajorTickSpacing(5);
    jSlider1.setMaximum(2020);
    jSlider1.setMinimum(2000);
    jSlider1.setMinorTickSpacing(1);
    jSlider1.setPaintLabels(true);
    jSlider1.setPaintTicks(true);
    jSlider1.setValue(2010);
    jSlider1.setPreferredSize(new java.awt.Dimension(500, 39));
    jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(javax.swing.event.ChangeEvent evt) {
    jSlider1StateChanged(evt);
    }
    });
    jPanel2.add(jSlider1);

    jPanel3.setBorder(javax.swing.BorderFactory.create TitledBorder("Month"));
    jPanel3.setPreferredSize(new java.awt.Dimension(100, 50));

    jSlider2.setMajorTickSpacing(1);
    jSlider2.setMaximum(12);
    jSlider2.setMinimum(1);
    jSlider2.setPaintLabels(true);
    jSlider2.setPaintTicks(true);
    jSlider2.setPreferredSize(new java.awt.Dimension(500, 39));
    jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(javax.swing.event.ChangeEvent evt) {
    jSlider2StateChanged(evt);
    }
    });
    jPanel3.add(jSlider2);

    jPanel4.setBorder(javax.swing.BorderFactory.create TitledBorder("Day"));
    jPanel4.setPreferredSize(new java.awt.Dimension(100, 50));

    jSlider3.setMajorTickSpacing(5);
    jSlider3.setMaximum(31);
    jSlider3.setMinimum(1);
    jSlider3.setMinorTickSpacing(1);
    jSlider3.setPaintLabels(true);
    jSlider3.setPaintTicks(true);
    jSlider3.setPreferredSize(new java.awt.Dimension(500, 39));
    jSlider3.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(javax.swing.event.ChangeEvent evt) {
    jSlider3StateChanged(evt);
    }
    });
    jPanel4.add(jSlider3);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE)
    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE)
    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE)
    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
    // TODO add your handling code here:

    // jTextField1.setText(jSlider1.getValue());
    this.jLabel1.setText(format());

    }//GEN-LAST:event_jSlider1StateChanged

    private void jSlider3StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider3StateChanged
    // TODO add your handling code here:
    this.jLabel1.setText(format());
    }//GEN-LAST:event_jSlider3StateChanged

    private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider2StateChanged
    // TODO add your handling code here:

    this.jLabel1.setText(format());
    }//GEN-LAST:event_jSlider2StateChanged





    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Assignment_2_VietHung().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSlider2;
    private javax.swing.JSlider jSlider3;
    // End of variables declaration//GEN-END:variables

    }



    Thanks for helppp!!!
    Last edited by hungleon88; 08-30-2008 at 09:03 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What is the problem? Can you describe why the code you posted doesn't do what you want? What are you having trouble with?
    Break the project up into small steps and do one thing at a time.

  5. #5
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    No, what i means are :
    when you choose The Month that have 30 days , example : You choose April , and then, you choose day 31st, that will auto change to May 1st (all of this is in JSilder)
    Now i want to whenyou choose like that, the Slider will not move.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    A couple of suggestions:

    1) When posting to the forum, avoid using NetBeans generated code as many of us can't compile and run this. Instead just create a simple GUI. I was able to create this by adding this code to the end of your initComponents method:
    Java Code:
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        mainPanel.add(jPanel1);
        mainPanel.add(jPanel2);
        mainPanel.add(jPanel3);
        mainPanel.add(jPanel4);
        mainPanel.setPreferredSize(MAINPANEL_SIZE);
        getContentPane().add(mainPanel);
    
        pack();
    2) Have your variable names mean something. This makes your code self-commenting and thus much easier to read and understand. For instance, instead of "jSlider3", call this variable monthsSlider. Instead of a method "jSlider3StateChanged" call this monthsSliderStateChanged. You will definitely thank me later if you make these changes now (and so will your teacher).

    3) Most of java Calendar class methods use months going from 0 to 11. Thus the java constant Calendar.JANUARY == 0 and Calendar.DECEMBER == 11. I advise you to stick to this convention as it will make it much easier for your code to interact with the Calendar library which you will need to do later in your code. In fact to try to make it easy as possible, I try to avoid using "magic" numbers and just let the Calendar constants decide which numbers to use. For instance:
    Java Code:
        monthsSlider.setMaximum(Calendar.DECEMBER);
        monthsSlider.setMinimum(Calendar.JANUARY);
    
        monthsSlider.setSnapToTicks(true);  // also, don't forget to call this method on all your sliders!
    4) This line is no good as the Date constructor has been deprecated:
    Java Code:
    Date d = new Date(jSlider1.getValue()-1900,jSlider2.getValue()-1,jSlider3.getValue());
    Instead you will need to a GregorianCalendar object which can then be changed to a Date easily with a call to getTime() on this object.

    5) To solve the problem you want to solve, I recommend that you create a method which I called "setMaxDaysInMonth". In this method, you get your current month and year, then I'd create a GregorianCalendar object with this month, year, and a day of 1. You can then derive the actual maximum number of days in the month via the Calendar method "getActualMaximum" which you call on your GregorianCalendar object. You can then easily change your days slider calling setMaximum on your days slider. You will need to call this method from within your month and year slider state changed methods (it needs to be in the year state change method because of leap years). So in pseudocode, you have:
    Java Code:
      // PSEUDOCODE
      private void setMaxDaysInMonth()
      {
        get the current month
        get the current year
        create a GregorianCalendar object using the current month, year, and day = 1.
        get the max days in this month by calling getActualMaximum on this GregorianCalendar object
           don't forget to use the correct Calendar parameter: Calendar.DAY_OF_MONTH
        set the days slider's max with the above value.
      }
    Last edited by Fubarable; 08-30-2008 at 05:17 PM.

  7. #7
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

Similar Threads

  1. How can we zoom a map using JSlider
    By barney in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 02:48 PM
  2. jmf & JSlider
    By nathanr_kamal in forum AWT / Swing
    Replies: 6
    Last Post: 06-28-2008, 08:56 AM
  3. Scale 2 or more pictures using a JSlider
    By Panchitopro in forum AWT / Swing
    Replies: 4
    Last Post: 05-20-2008, 05:44 PM
  4. Scale 2 or more pictures using a JSlider
    By Panchitopro in forum New To Java
    Replies: 0
    Last Post: 05-05-2008, 06:22 PM
  5. How To:Use a JSlider to adjust Text size in a JPanel
    By louiebagz in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •