Results 1 to 16 of 16
Thread: jtogglebutton if then?
- 06-09-2010, 12:17 AM #1
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
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 12:20 AM.
- 06-09-2010, 12:27 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-09-2010, 02:32 AM #3
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.to lock down everything until
What will make code execute if a button is NOT pressed?if it is not pressed, it uses
- 06-09-2010, 03:48 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 } } };
- 06-09-2010, 06:43 AM #5
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
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 }
- 06-09-2010, 07:35 AM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
use isSelected() method.
Java Code:if(jToggleButton1.isSelected() == true) {System.out.println(1 + 1); } else {System.out.println(1 - 1); }
- 06-09-2010, 10:49 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-09-2010, 10:51 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-12-2010, 05:51 AM #9
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
thank you. Is there a thanks button or equivalent? You deserve it.
edit: found it. thanks Also, usinghow do i make it change what is used inJava Code:if(jToggleButton1.isSelected() == true) {System.out.println(1 + 1); } else {System.out.println(1 - 1); }
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 05:54 AM.
- 06-12-2010, 03:49 PM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I don't see any in your code where you need to use if..else statement. Anyway:
Hope this help.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. } }
Goodluck,
geje
- 06-12-2010, 03:57 PM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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:
Goodluck. :)Java Code:int newvalue = (((500 + 5) - 200) * 6/2 [b]//this operation will return-->>915[/b]
Last edited by mine0926; 06-12-2010 at 04:06 PM.
- 06-13-2010, 05:33 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-13-2010, 05:45 AM #13
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
you mean ilop12's operation will return correct result even if parenthesis "()" was not used?
- 06-13-2010, 05:56 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-13-2010, 06:03 AM #15
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Ok. I just misundestood. :o
- 06-13-2010, 06:05 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Show/Hide Label with JToggleButton
By ntagrafix in forum AWT / SwingReplies: 4Last Post: 11-04-2009, 02:19 AM -
JToggleButton Demonstration
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks