Results 1 to 16 of 16
  1. #1
    ilop12 is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default jtogglebutton if then?

    Hey guys i just found out about net beans and im really happy because it makes everything easier. Anyway im writing a conversion gui applet, really simple, that coverts A to B with a button. Im wondering if theres a way that, if the jtogglebutton is pressed, the convert button uses formula A and if it is not pressed, it uses formula b

    thanks.

    edit: and if theres an easier way, whatever works for you guys. Also, is it possible to lock down everything until the user puts the right password in a password field?
    Last edited by ilop12; 06-09-2010 at 01:20 AM.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Myself, I'd use JRadioButtons to switch between one conversion vs another, but be sure to link the two JRadioButtons with a ButtonGroup object -- the Sun tutorial on buttons will show you how to do this. Otherwise sure, you can use a JToggleButton for this, this toggle button cannot be the one that triggers the actual conversion. Instead you'll need a separate JButton for that.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    to lock down everything until
    You can disable components to keep users from using them. It there are a lot of them to turn off and on, put then in an array and run a loop.

    if it is not pressed, it uses
    What will make code execute if a button is NOT pressed?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Or else you can treat the button as a abstract button through an action listener.

    Java Code:
    ActionListener actionListener = new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            if(selected) {
              // Selection process
            }
            else {
              // non-selection process
            }
          }
        };

  5. #5
    ilop12 is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    ok ill change my question. Using netbeans, how do i make two radio buttons and make them change the formula being used for conversion. Ill post the entire source code if that helps you guys.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * NewJFrame.java
     *
     * Created on Jun 8, 2010, 6:37:28 PM
     */
    
    /**
     *
     * @author Administrator
     */
    public class NewJFrame extends javax.swing.JFrame {
        private Object zoinksLabel;
    
        /** Creates new form NewJFrame */
        public NewJFrame() {
            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">
        private void initComponents() {
    
            buttonGroup1 = new javax.swing.ButtonGroup();
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jLabel2 = new javax.swing.JLabel();
            text = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("This will convert Zoinks to Zinks");
    
            jButton1.setText("Convert");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel2.setText("Zoinks");
    
            text.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    textActionPerformed(evt);
                }
            });
    
            jLabel3.setText("Zinks");
    
            jRadioButton1.setText("jRadioButton1");
    
            jRadioButton2.setText("jRadioButton2");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(47, 47, 47)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton1)
                                .addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(30, 30, 30)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(62, 62, 62)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jRadioButton1)
                                        .addComponent(jRadioButton2))))))
                    .addContainerGap(71, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(97, 97, 97)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton1)
                        .addComponent(jLabel3))
                    .addContainerGap(100, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(123, Short.MAX_VALUE)
                    .addComponent(jRadioButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton1)
                    .addGap(128, 128, 128))
            );
    
            pack();
        }// </editor-fold>
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    //Parse degrees Celsius as a double and convert to Fahrenheit.
        //Parse degrees Celsius as a double and convert to Fahrenheit.
        int tempFahr = (int)((Double.parseDouble(text.getText())) * 4.6 + 32 - 3.14 * 2.7777);
        jLabel2.setText(tempFahr + " Zinks");
    
            // TODO add your handling code here:
        }                                        
    
        private void textActionPerformed(java.awt.event.ActionEvent evt) {                                     
            // TODO add your handling code here:
        }                                    
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        private javax.swing.JTextField text;
        // End of variables declaration
    
    }

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    use isSelected() method.
    Java Code:
    if(jToggleButton1.isSelected() == true)
    {System.out.println(1 + 1);
    }
    else
    {System.out.println(1 - 1);
    }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by ilop12 View Post
    ok ill change my question. Using netbeans, how do i make two radio buttons and make them change the formula being used for conversion. Ill post the entire source code if that helps you guys.
    Best thing is that explain your question more clearly, without posting a huge code which could be lazy to read. Yea, posting the code segment, I mean the specific code segment, really helpful to answering.

    Check my previous post for the solution.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mine0926 View Post
    use isSelected() method.
    Java Code:
    if(jToggleButton1.isSelected() == true)
    {System.out.println(1 + 1);
    }
    else
    {System.out.println(1 - 1);
    }
    Check post #4.

  9. #9
    ilop12 is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    thank you. Is there a thanks button or equivalent? You deserve it.

    edit: found it. thanks Also, using
    Java Code:
    if(jToggleButton1.isSelected() == true)
    {System.out.println(1 + 1);
    }
    else
    {System.out.println(1 - 1);
    }
    how do i make it change what is used in
    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    //Parse degrees Celsius as a double and convert to Fahrenheit.
        //Parse degrees Celsius as a double and convert to Fahrenheit.
        int tempFahr = (int)((Double.parseDouble(text.getText())) * 4.6 + 32 - 3.14 * 2.7777);
        jLabel2.setText(tempFahr + " Zinks");
    Last edited by ilop12; 06-12-2010 at 06:54 AM.

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I don't see any in your code where you need to use if..else statement. Anyway:

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {
                 if(jToggleButton.isSelected == true)
                 {     //Parse degrees Celsius as a double and convert to Fahrenheit.
                       int tempFahr = (int)((Double.parseDouble(text.getText())) * 4.6 + 32 - 3.14 * 2.7777);
                       jLabel2.setText(tempFahr + " Zinks");
                 }
                 else
                 {//other operation to do.
                  }               
    }
    Hope this help.

    Goodluck,
    geje

  11. #11
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I am just suggesting, I think you need to use parenthesis"()" to seperate your operations. I am not 100% sure but I think your operation will cause error.

    EXAMPLE#1:
    Java Code:
    int newvalue = ((500 + 5) - (200 * 6))/2
    [b]//this operation will return-->>547.5[/b]

    this one will compute differently with:


    EXAMPLE#2:
    Java Code:
    int newvalue = (((500 + 5) - 200) * 6/2
    [b]//this operation will return-->>915[/b]
    Goodluck. :)
    Last edited by mine0926; 06-12-2010 at 05:06 PM.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mine0926 View Post
    I am just suggesting, I think you need to use parenthesis"()" to seperate your operations.
    That govern by order of operations, or the precedence of mathematical operations.

  13. #13
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    you mean ilop12's operation will return correct result even if parenthesis "()" was not used?

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mine0926 View Post
    you mean ilop12's operation will return correct result even if parenthesis "()" was not used?
    Of course not. What I'm say is that in your previous post, you are explaining about the order of operations, and that's why we use parenthesis for group them accordingly. :)

  15. #15
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Talking

    Ok. I just misundestood. :o

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Show/Hide Label with JToggleButton
    By ntagrafix in forum AWT / Swing
    Replies: 4
    Last Post: 11-04-2009, 03:19 AM
  2. JToggleButton Demonstration
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:38 PM

Posting Permissions

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