Results 1 to 20 of 33
Thread: Java help...Checkboxes
- 10-21-2010, 05:03 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Java help...Checkboxes
I have my code for my program here, and I am having trouble with two different things. First of all I can't get my check box to check when I hit calculate. Any one have any ideas? I have the check boxes in the code but it wont check when I hit calculate to show the largest number.
Second, I have code for if there is a tie but it wont seem to work where ever I put it in my code. here it is!
Does anyone have any suggestins on how to do this?
Java Code:// String tieMessage = null; // // if (piece1 == piece2) { // if (piece1 == piece3) { // tieMessage = "It's a tie between pieces 1,2 and 3"; // } else { // tieMessage = "It's a tie between pieces 1 and 2"; // } // } else if (piece1 == piece3) { // tieMessage = "It's a tie between pieces 1 and 3"; // } else if (piece3 == piece2) { // tieMessage = "It's a tie between pieces 2 and 3"; // } // if (tieMessage == null) {
Java Code:import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.FlowLayout; class ArtPrizeDemo extends JFrame { private JPanel panel; // A holding panel private JLabel messageLabel1; // A message to the user private JLabel messageLabel2; // A message to the user private JLabel messageLabel3; // A message to the user private JLabel result; // A message to the user private JTextField piece1; // To hold user input private JTextField piece2; // To hold user input private JTextField piece3; // To hold user input private JCheckBox box1; private JCheckBox box2; private JCheckBox box3; private JButton calculate; private final int WINDOW_WIDTH = 400; // Window width private final int WINDOW_HEIGHT = 200; // Window height int num; public ArtPrizeDemo() { // Set the title. setTitle("Art Prize"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame. buildPanel(); // Add the panel to the frame's content pane. add(panel); // Display the window. setVisible(true); } private void buildPanel() { panel= new JPanel(); messageLabel1 = new JLabel("Enter the number of votes for piece 1:"); piece1 = new JTextField(10); box1 = new JCheckBox(" "); messageLabel2 = new JLabel("Enter the number of votes for piece 2:"); piece2 = new JTextField(10); box2 = new JCheckBox(" "); messageLabel3 = new JLabel("Enter the number of votes for piece 3:"); piece3 = new JTextField(10); box3 = new JCheckBox(" "); calculate= new JButton("Calculate"); panel.add(messageLabel1); panel.add(piece1); panel.add(box1); panel.add(messageLabel2); panel.add(piece2); panel.add(box2); panel.add(messageLabel3); panel.add(piece3); panel.add(box3); calculate.addActionListener(new calButtonListener()); panel.add(calculate); result = new JLabel(" "); panel.add(result); } private class calButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String in; int p1,p2,p3; in=piece1.getText(); p1=Integer.parseInt(in); in=piece2.getText(); p2=Integer.parseInt(in); in=piece3.getText(); p3=Integer.parseInt(in); if(p1>p2) { if(p1>p3) { result.setText("Piece 1 is the winner"); box1.enable(true); } else { result.setText("Piece 3 is the winner"); box3.enable(true); } } else { if(p2>p3) { result.setText("Piece 2 is the winner"); box2.enable(true); } else { result.setText("Piece 3 is the winner"); box3.enable(true); } } class ArtPrize { public static void main(String[] args) { ArtPrizeDemo frm = new ArtPrizeDemo(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(350,100); frm.setVisible(true); } }
- 10-21-2010, 05:29 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Plus points for using code tags, but does that mean you don't indent your code?
Can't follow the flow otherwise.
- 10-21-2010, 06:07 PM #3
First let's take care of the checkbox issue. When you use the enable() method all it does if you set it to true is make it so you CAN click on it, it won't actually make it checked. You will have to use the setSelected() method in order to actually make the checkbox show up as checked.
Second is your tie algorithm. You are trying to compare text fields using the == operator. They are not integers. So simply go into your algorithm and change "piece1" to p1, "piece2" to p2, etc... just like you did with your checkbox algorithm. Also, the placement. I put your tie code above your checkbox algorithm encasing the checkbox algorithm inside the end bracket of "if (tieMessage == null) {". The reason for doing this is because you want to see if things are a tie before you actually tell the user who won. Since you have setText() inside the checkbox code, your tie algorithm had to be first.
Third is why you are not getting output like "...is a tie...". It's because you use the variable tieMessage but do not use the setText method to print it to the screen anywhere.Last edited by joshdgreen; 10-21-2010 at 06:09 PM.
Sincerely, Joshua Green
Please REP if I help :)
- 10-23-2010, 02:43 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
I'm still having trouble with the "tie" scenario. I guess i'm confused as where I should be putting it, and what I should be changing it too.
Java Code:import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.FlowLayout; class ArtPrizeDemo extends JFrame { private JPanel panel; // A holding panel private JLabel messageLabel1; // A message to the user private JLabel messageLabel2; // A message to the user private JLabel messageLabel3; // A message to the user private JLabel result; // A message to the user private JTextField piece1; // To hold user input private JTextField piece2; // To hold user input private JTextField piece3; // To hold user input private JCheckBox box1; private JCheckBox box2; private JCheckBox box3; private JButton calculate; private final int WINDOW_WIDTH = 400; // Window width private final int WINDOW_HEIGHT = 200; // Window height int num; public ArtPrizeDemo() { // Set the title. setTitle("Art Prize"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame. buildPanel(); // Add the panel to the frame's content pane. add(panel); // Display the window. setVisible(true); } private void buildPanel() { panel= new JPanel(); messageLabel1 = new JLabel("Enter the number of votes for piece 1:"); piece1 = new JTextField(10); box1 = new JCheckBox(" "); messageLabel2 = new JLabel("Enter the number of votes for piece 2:"); piece2 = new JTextField(10); box2 = new JCheckBox(" "); messageLabel3 = new JLabel("Enter the number of votes for piece 3:"); piece3 = new JTextField(10); box3 = new JCheckBox(" "); calculate= new JButton("Calculate"); panel.add(messageLabel1); panel.add(piece1); panel.add(box1); panel.add(messageLabel2); panel.add(piece2); panel.add(box2); panel.add(messageLabel3); panel.add(piece3); panel.add(box3); calculate.addActionListener(new calButtonListener()); panel.add(calculate); result = new JLabel(" "); panel.add(result); } private class calButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String in; int p1,p2,p3; in=piece1.getText(); p1=Integer.parseInt(in); in=piece2.getText(); p2=Integer.parseInt(in); in=piece3.getText(); p3=Integer.parseInt(in); String tieMessage = null; if (piece1 == piece2) { if (piece1 == piece3) { tieMessage = "It's a tie between pieces 1,2 and 3"; } else { tieMessage = "It's a tie between pieces 1 and 2"; } } else if (piece1 == piece3) { tieMessage = "It's a tie between pieces 1 and 3"; } else if (piece3 == piece2) { tieMessage = "It's a tie between pieces 2 and 3"; } if (tieMessage == null) { } if(p1>p2) { if(p1>p3) { result.setText("Piece 1 is the winner"); box1.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } else { if(p2>p3) { result.setText("Piece 2 is the winner"); box2.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } } } } class ArtPrize { public static void main(String[] args) { ArtPrizeDemo frm = new ArtPrizeDemo(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(350,100); frm.setVisible(true); } }
-
Please edit your code above to fix the indentation. It's very hard to read, so fewer folks will take the time to read it.
Edit: I see that a request for improved indentation fix has already been made, but you've chosen to ignore it, why? You're only hurting your chances of getting help.Last edited by Fubarable; 10-23-2010 at 03:02 AM.
- 10-23-2010, 03:07 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Java Code:import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.FlowLayout; class ArtPrizeDemo extends JFrame { private JPanel panel; // A holding panel private JLabel messageLabel1; // A message to the user private JLabel messageLabel2; // A message to the user private JLabel messageLabel3; // A message to the user private JLabel result; // A message to the user private JTextField piece1; // To hold user input private JTextField piece2; // To hold user input private JTextField piece3; // To hold user input private JCheckBox box1; private JCheckBox box2; private JCheckBox box3; private JButton calculate; private final int WINDOW_WIDTH = 400; // Window width private final int WINDOW_HEIGHT = 200; // Window height int num; public ArtPrizeDemo() { // Set the title. setTitle("Art Prize"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame. buildPanel(); // Add the panel to the frame's content pane. add(panel); // Display the window. setVisible(true); } private void buildPanel() { panel= new JPanel(); messageLabel1 = new JLabel("Enter the number of votes for piece 1:"); piece1 = new JTextField(10); box1 = new JCheckBox(" "); messageLabel2 = new JLabel("Enter the number of votes for piece 2:"); piece2 = new JTextField(10); box2 = new JCheckBox(" "); messageLabel3 = new JLabel("Enter the number of votes for piece 3:"); piece3 = new JTextField(10); box3 = new JCheckBox(" "); calculate= new JButton("Calculate"); panel.add(messageLabel1); panel.add(piece1); panel.add(box1); panel.add(messageLabel2); panel.add(piece2); panel.add(box2); panel.add(messageLabel3); panel.add(piece3); panel.add(box3); calculate.addActionListener(new calButtonListener()); panel.add(calculate); result = new JLabel(" "); panel.add(result); } private class calButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String in; int p1,p2,p3; in=piece1.getText(); p1=Integer.parseInt(in); in=piece2.getText(); p2=Integer.parseInt(in); in=piece3.getText(); p3=Integer.parseInt(in); String tieMessage = null; if (piece1 == piece2) { if (piece1 == piece3) { tieMessage = "It's a tie between pieces 1,2 and 3"; } else { tieMessage = "It's a tie between pieces 1 and 2"; } } else if (piece1 == piece3) { tieMessage = "It's a tie between pieces 1 and 3"; } else if (piece3 == piece2) { tieMessage = "It's a tie between pieces 2 and 3"; } if (tieMessage == null) { } if(p1>p2) { if(p1>p3) { result.setText("Piece 1 is the winner"); box1.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } else { if(p2>p3) { result.setText("Piece 2 is the winner"); box2.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } } } } class ArtPrize { public static void main(String[] args) { ArtPrizeDemo frm = new ArtPrizeDemo(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(350,100); frm.setVisible(true); } }
- 10-23-2010, 03:13 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Java Code:if (piece1 == piece2) {
As noted above this line (and the others like it) is comparing two JTextField instances for identity. They are not identical so "piece1==piece2" will always be false.
I don't know what you are trying to do with that line (perhaps you could say?) but, whatever it is, don't do it that way.
- 10-23-2010, 03:23 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
I need it to actually say this: It's a tie between pieces 1,2 and 3";
when there is a tie!
- 10-23-2010, 03:38 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Yes, but what exactly do you intend this line to do?
Java Code:if (piece1 == piece2) {
As pointed out the condition is always false. But what was it that you intended when you wrote this particular line of code?
- 10-23-2010, 03:47 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Like I said, I thought it would give me what I wanted:
It's a tie between pieces 1,2 and 3"
It's a tie between pieces 1 and 2"
It's a tie between pieces 1 and 3"
"It's a tie between pieces 2 and 3"
Those are the 4 statements that I need to put into my code that I can't seem to do! I have tried and everyone keeps saying the same thing. So I need to change it, but to what? Thats what I need to know!
-
piece1 and piece2 are JTextFields. Think of them as if they were cardboard boxes, and when you check if piece1 == piece2, you're checking if they are actual one and the same box. What are you trying to compare here, the boxes, or the numbers that are contained inside of the boxes? If the numbers, then extract them (you already know how per your code above), and compare the numbers that the boxes/JTextFields hold.
- 10-23-2010, 03:56 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
There seems to be a communication problem ... but that's OK!
I understand what you wanted to get output expressing what was tied with what. That's not what I'm asking. It's just that I look at your code and see a condition that will never be true, so that output will never be output. Never.
Hence my question. I'm thinking "what did the OP mean by if(piece1==piece2)?" because I figure that if I knew what you meant, I might be able to come up with some other way of expressing it. Some way that gives a figting chance for the output to actually appear.
Java Code:if(piece1 == piece2) { // under what circumstances did you think you might get to here?
- 10-23-2010, 04:29 AM #13
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
OK I understand what you are saying, but I am just all confused on how to change it. I have tried several different ways and it hasn't worked yet!
- 10-23-2010, 05:04 AM #14
- 10-23-2010, 05:17 AM #15
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
I know I havent made any changes b/c I dont know what you mean. I changed it to the code I have added and tried putting it in several different places but still it wont work. I guess i'm not understanding what you are trying to tell me. Plus I have people telling me the == wont work, so that is confusing me even more!!
Java Code:String setText = null; if (p1 == p2) { if (p1 == p3) { setText = "It's a tie between pieces 1,2 and 3"; } else { setText = "It's a tie between pieces 1 and 2"; } } else if (p1 == p3) { setText = "It's a tie between pieces 1 and 3"; } else if (p3 == p2) { setText = "It's a tie between pieces 2 and 3"; } if (setText == null) {
- 10-23-2010, 05:36 AM #16
That code will work, and you have it in the right place in your code. The part that is wrong is the ending bracket of the statement below.
Java Code:if (tieMessage == null) { }Sincerely, Joshua Green
Please REP if I help :)
- 10-23-2010, 05:45 AM #17
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Josh I don't mean to bug you but I moved that bracket all over and it still wouldn't work. If I give you my code can you bold it or show me where to exactly put it!
Java Code:import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.FlowLayout; class ArtPrizeDemo extends JFrame { private JPanel panel; // A holding panel private JLabel messageLabel1; // A message to the user private JLabel messageLabel2; // A message to the user private JLabel messageLabel3; // A message to the user private JLabel result; // A message to the user private JTextField piece1; // To hold user input private JTextField piece2; // To hold user input private JTextField piece3; // To hold user input private JCheckBox box1; private JCheckBox box2; private JCheckBox box3; private JButton calculate; private final int WINDOW_WIDTH = 400; // Window width private final int WINDOW_HEIGHT = 200; // Window height int num; public ArtPrizeDemo() { // Set the title. setTitle("Art Prize"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame. buildPanel(); // Add the panel to the frame's content pane. add(panel); // Display the window. setVisible(true); } private void buildPanel() { panel= new JPanel(); messageLabel1 = new JLabel("Enter the number of votes for piece 1:"); piece1 = new JTextField(10); box1 = new JCheckBox(" "); messageLabel2 = new JLabel("Enter the number of votes for piece 2:"); piece2 = new JTextField(10); box2 = new JCheckBox(" "); messageLabel3 = new JLabel("Enter the number of votes for piece 3:"); piece3 = new JTextField(10); box3 = new JCheckBox(" "); calculate= new JButton("Calculate"); panel.add(messageLabel1); panel.add(piece1); panel.add(box1); panel.add(messageLabel2); panel.add(piece2); panel.add(box2); panel.add(messageLabel3); panel.add(piece3); panel.add(box3); calculate.addActionListener(new calButtonListener()); panel.add(calculate); result = new JLabel(" "); panel.add(result); } private class calButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String in; int p1,p2,p3; in=piece1.getText(); p1=Integer.parseInt(in); in=piece2.getText(); p2=Integer.parseInt(in); in=piece3.getText(); p3=Integer.parseInt(in); String setText = null; if (p1 == p2) { if (p1 == p3) { setText = "It's a tie between pieces 1,2 and 3"; } else { setText = "It's a tie between pieces 1 and 2"; } } else if (p1 == p3) { setText = "It's a tie between pieces 1 and 3"; } else if (p3 == p2) { setText = "It's a tie between pieces 2 and 3"; } if (setText == null) { } if(p1>p2) { if(p1>p3) { result.setText("Piece 1 is the winner"); box1.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } else { if(p2>p3) { result.setText("Piece 2 is the winner"); box2.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } } } } class ArtPrize { public static void main(String[] args) { ArtPrizeDemo frm = new ArtPrizeDemo(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(350,100); frm.setVisible(true); } }
- 10-23-2010, 05:47 AM #18
Yeah, I need to switch to my Windows system though, so gimme a couple minutes.
Sincerely, Joshua Green
Please REP if I help :)
- 10-23-2010, 05:48 AM #19
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
OK thanks a ton!!
- 10-23-2010, 06:18 AM #20
You want it to look something like this. Take note of the code that I have bolded and make sure that you understand why I put that code where I did.
Java Code:if (p1 == p2) { if (p1 == p3) setText = "It's a tie between pieces 1,2 and 3"; else setText = "It's a tie between pieces 1 and 2"; } else if (p1 == p3) setText = "It's a tie between pieces 1 and 3"; else if (p3 == p2) setText = "It's a tie between pieces 2 and 3"; [COLOR="Red"][B]if (setText == null) {[/B][/COLOR] if(p1>p2) { if(p1>p3) { result.setText("Piece 1 is the winner"); box1.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } else if (p2 > p3) { result.setText("Piece 2 is the winner"); box2.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } [COLOR="Red"][B]} result.setText(setText);[/B][/COLOR]Last edited by joshdgreen; 10-23-2010 at 06:20 AM.
Sincerely, Joshua Green
Please REP if I help :)
Similar Threads
-
ComboBox with CheckBoxes
By heba.farouk in forum AWT / SwingReplies: 14Last Post: 06-09-2010, 11:03 AM -
CheckBoxes and JTables
By lakshayghai in forum AWT / SwingReplies: 1Last Post: 03-16-2010, 08:01 PM -
How to use Mnemonic for CheckBoxes
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:45 PM -
How to use Swing CheckBoxes
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:44 PM -
Problem with checkboxes
By carl in forum Java AppletsReplies: 1Last Post: 08-06-2007, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks