Solved

How to enter JRadioButtons into Access Database and retrieve it out?

Posted on 2004-04-19
33
734 Views
Last Modified: 2008-02-01
HI..

I am currently doing a vote system using JRadioButton and the problem is I can't capture the data clicked by JRadioButton into microsoft access database.I have linked using ODBC and it linked successfully but still I can't capture it out and retireve it for display.

Would appreciate anyone who can help in anyway.Thanks!

My coding are as follows...

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

public class VoteDialog extends JPanel {
      JButton voteButton;
    JLabel label;
    JFrame frame;
    String simpleDialogDesc = "The candidates";

       DataBase db = new DataBase();
    Connection connection = db.getConnection();

    public VoteDialog(JFrame frame) {
        this.frame = frame;
        JLabel title;
       
        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();
       
        System.out.println("passed createSimpleDialogBox");
               
        title = new JLabel("Click the \"Vote\" button"
                           + " once you have selected a candidate.",
                           JLabel.CENTER);
       
        label = new JLabel("Vote now!", JLabel.CENTER);
      label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);  
        add(label, BorderLayout.SOUTH);        
        add(choicePanel, BorderLayout.CENTER);
    }

    void setLabel(String newText) {
        label.setText(newText);
    }

    private JPanel createSimpleDialogBox() {
      final int numButtons = 5;
      final  JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

         final JButton voteButton;

        final String defaultMessageCommand = "default";
       
        radioButtons[0] = new JRadioButton("<html>Polythenic 1: <font color=red>Nanyang Poly</font></html>");
        radioButtons[0].setActionCommand(defaultMessageCommand);
       

        radioButtons[1] = new JRadioButton("<html>Polythenic 2: <font color=green>Singapore Poly</font></html>");
        radioButtons[1].setActionCommand(defaultMessageCommand);

        radioButtons[2] = new JRadioButton("<html>Polythenic 3: <font color=blue>Temasek Poly</font></html>");
        radioButtons[2].setActionCommand(defaultMessageCommand);

        radioButtons[3] = new JRadioButton("<html>Polythenic 4: <font color=maroon>Republic Poly</font></html>");
        radioButtons[3].setActionCommand(defaultMessageCommand);
            
            radioButtons[4] = new JRadioButton("<html>Polythenic 5: <font color=yellow>Ngee Ann Poly</font></html>");
        radioButtons[4].setActionCommand(defaultMessageCommand);

     
        // Select the first button by default.
        radioButtons[0].setSelected(true);

      for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);
    }
          voteButton = new JButton("Vote");
       
        voteButton.addActionListener(new ActionListener() {
              
            public void actionPerformed(ActionEvent e) {
                  String command = group.getSelection().getActionCommand();
                  
                  if(e.getSource()==voteButton) {
                        
                        String insertString;
                          Statement statement;
              
                          if (radioButtons[i]=radioButtons[0]) {
                          int  p1= Integer.parseInt(radioButtons[0].getText());  
                          int  p2= Integer.parseInt(radioButtons[1].getText());
                          int  p3= Integer.parseInt(radioButtons[2].getText());
                          int  p4= Integer.parseInt(radioButtons[3].getText());
                          int  p5= Integer.parseInt(radioButtons[4].getText());
                          
                          insertString = "Insert into poly (nyp, sp, tp, rp, np) "+
            " VALUES ( '"+ p1 +"', '"+ p2 +"', '"+ p3 +"', '"+ p4 +"','"+ p5 +"')";
            
            try{
           statement = connection.createStatement();
           statement.executeUpdate(insertString);
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
     
          }                  
          }                                                        
                // ok dialog
                if (command == defaultMessageCommand) {
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                         }
                return;                
        }                  
           
       
});

      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }
      
                            
                
                            
    public static void main(String[] args) {
        JFrame frame = new JFrame("VoteDialog");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new VoteDialog(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}
0
Comment
Question by:ProfJL
  • 18
  • 15
33 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10862304
If you're inserting ints then they shouldn't be quoted
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10862316
e.g.

" VALUES ("+ p1 + ","+ p2

etc.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 10862390
This is also wrong:

>>if (radioButtons[i]=radioButtons[0])

that's an assignment, not a comparison. If you want a comparison, it should be

if (radioButtons[i].equals(radioButtons[0])) // compare the radio buttons
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10862426
Or

if (radioButtons[i]==radioButtons[0])

0
 

Author Comment

by:ProfJL
ID: 10862618
Hi CEHJ...

I had changed my codes but when i used           if (radioButtons[i]==radioButtons[0]) ....it say

C:\Documents and Settings\JL\Desktop\filechooser\VoteDialog.java:99: cannot resolve symbol
symbol: variable i
                    if (radioButtons[i]==radioButtons[0]) {
                             }

wat should i do to solve this problem..??

anyway I wann add in number to my database once the user clicked on 1 of the choice like if one user chose the first option of the JradioButton it will enter 1 in the database.Upon clicking the second time it will add on to the number like if the user clicked the first option of the JRadioButton again it will add into the database to 2.

I have tried this method but dunno if it works..
      if (radioButtons[i]==radioButtons[0]) {
                                p1=0+1;
                                p1++;
                          }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10862714
>>I had changed my codes but when i used ...    

It would have said that anyway. There is no variable 'i' in scope there

I'm not entirely sure what you're trying to achieve
0
 

Author Comment

by:ProfJL
ID: 10862763
I am actually trying to do a voting system of some choices...meaning once a user clicked on one fo the choices it will add "1" to the database and upon clikcing on the second time on the same option..it would add the number automatically to "2" meaning it will keep adding..

>>I had changed my codes meaning i change all the parts quoted

Below is my new codes...

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

public class VoteDialog extends JPanel {
      JButton voteButton;
    JLabel label;
    JFrame frame;
    String simpleDialogDesc = "The candidates";

       DataBase db = new DataBase();
    Connection connection = db.getConnection();

    public VoteDialog(JFrame frame) {
        this.frame = frame;
        JLabel title;
       
        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();
       
        System.out.println("passed createSimpleDialogBox");
               
        title = new JLabel("Click the \"Vote\" button"
                           + " once you have selected a candidate.",
                           JLabel.CENTER);
       
        label = new JLabel("Vote now!", JLabel.CENTER);
      label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);  
        add(label, BorderLayout.SOUTH);        
        add(choicePanel, BorderLayout.CENTER);
    }

    void setLabel(String newText) {
        label.setText(newText);
    }

    private JPanel createSimpleDialogBox() {
      final int numButtons = 5;
      final  JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

         final JButton voteButton;

        final String defaultMessageCommand = "default";
       
        radioButtons[0] = new JRadioButton("<html>Polythenic 1: <font color=red>Nanyang Poly</font></html>");
        radioButtons[0].setActionCommand(defaultMessageCommand);
       

        radioButtons[1] = new JRadioButton("<html>Polythenic 2: <font color=green>Singapore Poly</font></html>");
        radioButtons[1].setActionCommand(defaultMessageCommand);

        radioButtons[2] = new JRadioButton("<html>Polythenic 3: <font color=blue>Temasek Poly</font></html>");
        radioButtons[2].setActionCommand(defaultMessageCommand);

        radioButtons[3] = new JRadioButton("<html>Polythenic 4: <font color=maroon>Republic Poly</font></html>");
        radioButtons[3].setActionCommand(defaultMessageCommand);
            
            radioButtons[4] = new JRadioButton("<html>Polythenic 5: <font color=yellow>Ngee Ann Poly</font></html>");
        radioButtons[4].setActionCommand(defaultMessageCommand);

     
        // Select the first button by default.
        radioButtons[0].setSelected(true);

      for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);}

          voteButton = new JButton("Vote");
       
        voteButton.addActionListener(new ActionListener() {
              
            public void actionPerformed(ActionEvent e) {
                  String command = group.getSelection().getActionCommand();
                  
                  if(e.getSource()==voteButton) {
                        int p1,p2,p3,p4,p5;
                        String insertString;
                          Statement statement;
                                                              
                                      while (radioButtons[i]==radioButtons[0]) {
                                p1=0+1;
                                p1++;                                                
                                                                                          
                            p1= Integer.parseInt(radioButtons[0].getText());  
                            p2= Integer.parseInt(radioButtons[1].getText());
                            p3= Integer.parseInt(radioButtons[2].getText());
                            p4= Integer.parseInt(radioButtons[3].getText());
                            p5= Integer.parseInt(radioButtons[4].getText());
                          
                                
                          }
                          insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
            " VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
            
            try{
           statement = connection.createStatement();
           statement.executeUpdate(insertString);
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
     
          }                  
                                                                  
                // ok dialog
                if (command == defaultMessageCommand)
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                         
                return;                
                          
      }      
       
});

      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }
      
                            
                
                            
    public static void main(String[] args) {
        JFrame frame = new JFrame("VoteDialog");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new VoteDialog(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}


however i still get the problem that 'i" is not resolve varibale...wat shld I do to solve that..thanks..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10863035
I would hold the count in a parallel array. Obviously you need to get the value of the radio buttons 'on submit' or you'd have chaos as people changed their minds:

int[] voteCounts = new int[radioButtons.length];
// (if first button selected)
voteCounts[0]++;
0
 

Author Comment

by:ProfJL
ID: 10863202
I had added in the follwing codes and compiled with success but however when i clicked the "vote button" it shows errors shown below:

java.lang.NumberFormatException: For input string: "<html>Polythenic 1: <font co
lor=red>Nanyang Poly</font></html>"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
        at java.lang.Integer.parseInt(Integer.java:468)
        at java.lang.Integer.parseInt(Integer.java:518)
        at VoteDialog$1.actionPerformed(VoteDialog.java:97)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:17
86)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1839)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:245)
        at java.awt.Component.processMouseEvent(Component.java:5100)
        at java.awt.Component.processEvent(Component.java:4897)
        at java.awt.Container.processEvent(Container.java:1569)
        at java.awt.Component.dispatchEventImpl(Component.java:3615)
        at java.awt.Container.dispatchEventImpl(Container.java:1627)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
        at java.awt.Container.dispatchEventImpl(Container.java:1613)
        at java.awt.Window.dispatchEventImpl(Window.java:1606)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


what is happening..plz enlighten me..thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10863235
You're trying to convert that unconvertable radio button text to a number
0
 

Author Comment

by:ProfJL
ID: 10863318
I changed all the text to number and it worked but how do i set all the number to "0" first and then if it clicked on "1" it will go to that column in the database and add on it only..Upon clicking on "1" the second time it will add from 1 to 2 and so on..

"1" is the first option that i had set.
I thought of using your way like

// (if first button selected)---------->what do i put here for the choices?
voteCounts[0]++;

My codes are shown below..

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

public class VoteDialog extends JPanel {
      JButton voteButton;
    JLabel label;
    JFrame frame;
    String simpleDialogDesc = "The candidates";

       DataBase db = new DataBase();
    Connection connection = db.getConnection();

    public VoteDialog(JFrame frame) {
        this.frame = frame;
        JLabel title;
       
        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();
       
        System.out.println("passed createSimpleDialogBox");
               
        title = new JLabel("Click the \"Vote\" button"
                           + " once you have selected a candidate.",
                           JLabel.CENTER);
       
        label = new JLabel("Vote now!", JLabel.CENTER);
      label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);  
        add(label, BorderLayout.SOUTH);        
        add(choicePanel, BorderLayout.CENTER);
    }

    void setLabel(String newText) {
        label.setText(newText);
    }

    private JPanel createSimpleDialogBox() {
      final int numButtons = 5;
      final  JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

         final JButton voteButton;

        final String defaultMessageCommand = "default";
       
        radioButtons[0] = new JRadioButton("1");
        radioButtons[0].setActionCommand(defaultMessageCommand);
       

        radioButtons[1] = new JRadioButton("2");
        radioButtons[1].setActionCommand(defaultMessageCommand);

        radioButtons[2] = new JRadioButton("3");
        radioButtons[2].setActionCommand(defaultMessageCommand);

        radioButtons[3] = new JRadioButton("4");
        radioButtons[3].setActionCommand(defaultMessageCommand);
            
            radioButtons[4] = new JRadioButton("5");
        radioButtons[4].setActionCommand(defaultMessageCommand);

     
        // Select the first button by default.
        radioButtons[0].setSelected(true);

      for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);}

          voteButton = new JButton("Vote");
       
        voteButton.addActionListener(new ActionListener() {
              
            public void actionPerformed(ActionEvent e) {
                  String command = group.getSelection().getActionCommand();
                  
                  if(e.getSource()==voteButton)  {
                        int p1,p2,p3,p4,p5;
                        String insertString;
                          Statement statement;
                          
                          int[] voteCounts = new int[radioButtons.length];                  
                                        //if ( voteCounts[0].selected)  
                                          voteCounts[0]++;
                                          
                                //p1=0+1;
                                //p1++;                                                
                                                                                          
                            p1= Integer.parseInt(radioButtons[0].getText());  
                            p2= Integer.parseInt(radioButtons[1].getText());
                            p3= Integer.parseInt(radioButtons[2].getText());
                            p4= Integer.parseInt(radioButtons[3].getText());
                            p5= Integer.parseInt(radioButtons[4].getText());
                          
                                
                          
                          insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
            " VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
            
            try{
           statement = connection.createStatement();
           statement.executeUpdate(insertString);
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
     
                            
                                                                  
                // ok dialog
                if (command == defaultMessageCommand)
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                         
                return;                
        }                  
      }      
       
});

      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }
      
                            
                
                            
    public static void main(String[] args) {
        JFrame frame = new JFrame("VoteDialog");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new VoteDialog(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10863435
>>int[] voteCounts = new int[radioButtons.length];              

You don't put that in the ActionListener or it'll get created every time it's called. You need to create it only once.

From here:

String command = group.getSelection().getActionCommand();

you need to do

int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen]++;
0
 

Author Comment

by:ProfJL
ID: 10863545
It doesn't seem to work and now it can't add into database..

an error occurs like this shown:

java.lang.NumberFormatException: For input string: "default"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
        at java.lang.Integer.parseInt(Integer.java:468)
        at java.lang.Integer.parseInt(Integer.java:518)
        at VoteDialog$1.actionPerformed(VoteDialog.java:89)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:17
86)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1839)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:245)
        at java.awt.Component.processMouseEvent(Component.java:5100)
        at java.awt.Component.processEvent(Component.java:4897)
        at java.awt.Container.processEvent(Container.java:1569)
        at java.awt.Component.dispatchEventImpl(Component.java:3615)
        at java.awt.Container.dispatchEventImpl(Container.java:1627)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
        at java.awt.Container.dispatchEventImpl(Container.java:1613)
        at java.awt.Window.dispatchEventImpl(Window.java:1606)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10863603
Make sure one button is always selected
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10863618
And the action commands should all be numbers - at the moment you're using "default'
0
 

Author Comment

by:ProfJL
ID: 10863686
I changed the "default" to number "1" and I can insert into the database. However it is still adding all the options "1", "2", "3", "4", "5" all at the same time to the database.what I wanted is to insert only the option that you selected.

My codes are as shown:

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

public class VoteDialog extends JPanel {
      JButton voteButton;
    JLabel label;
    JFrame frame;
    String simpleDialogDesc = "The candidates";

       DataBase db = new DataBase();
    Connection connection = db.getConnection();

    public VoteDialog(JFrame frame) {
        this.frame = frame;
        JLabel title;
       
        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();
       
        System.out.println("passed createSimpleDialogBox");
               
        title = new JLabel("Click the \"Vote\" button"
                           + " once you have selected a candidate.",
                           JLabel.CENTER);
       
        label = new JLabel("Vote now!", JLabel.CENTER);
            label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);  
        add(label, BorderLayout.SOUTH);        
        add(choicePanel, BorderLayout.CENTER);
    }

    void setLabel(String newText) {
        label.setText(newText);
    }

    private JPanel createSimpleDialogBox() {
      final int numButtons = 5;
      final  JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

         final JButton voteButton;

        final String defaultMessageCommand = "1";
       
        radioButtons[0] = new JRadioButton("1");
        radioButtons[0].setActionCommand(defaultMessageCommand);
       

        radioButtons[1] = new JRadioButton("2");
        radioButtons[1].setActionCommand(defaultMessageCommand);

        radioButtons[2] = new JRadioButton("3");
        radioButtons[2].setActionCommand(defaultMessageCommand);

        radioButtons[3] = new JRadioButton("4");
        radioButtons[3].setActionCommand(defaultMessageCommand);
            
            radioButtons[4] = new JRadioButton("5");
        radioButtons[4].setActionCommand(defaultMessageCommand);

     
        // Select the first button by default.
        radioButtons[0].setSelected(true);

      for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);}

          voteButton = new JButton("Vote");
       
        voteButton.addActionListener(new ActionListener() {
              
              
               int[] voteCounts = new int[radioButtons.length];  
            public void actionPerformed(ActionEvent e) {
                  String command = group.getSelection().getActionCommand();
                  
                      
                  int numberChosen = Integer.parseInt(command);
                        voteCounts[--numberChosen]++;

                  if(e.getSource()==voteButton)  {
                        int p1,p2,p3,p4,p5;
                        String insertString;
                          Statement statement;
                          
                    
                                          
                                                                          
                                                                                          
                            p1= Integer.parseInt(radioButtons[0].getText());  
                            p2= Integer.parseInt(radioButtons[1].getText());
                            p3= Integer.parseInt(radioButtons[2].getText());
                            p4= Integer.parseInt(radioButtons[3].getText());
                            p5= Integer.parseInt(radioButtons[4].getText());
                          
                                
                          
                          insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
            " VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
            
            try{
           statement = connection.createStatement();
           statement.executeUpdate(insertString);
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
     
                            
                                                                  
                // ok dialog
                if (command == defaultMessageCommand)
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                         
                return;                
        }                  
      }      
       
});

      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }
      
                            
                
                            
    public static void main(String[] args) {
        JFrame frame = new JFrame("VoteDialog");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new VoteDialog(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10863718
You're now setting all the buttons to the same value. The action commands  should be numbered from 1 to 5. You need to insert the values of the array voteCounts into the database
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10863741
So

>>" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";

should be

" VALUES ( "+ voteCounts[0] +", "+ voteCounts1] +", "+ voteCounts[2] +", "+ voteCounts[3] +","+ voteCounts[4] +")";

0
 

Author Comment

by:ProfJL
ID: 10863806
>>You're now setting all the buttons to the same value. The action commands  should be numbered from 1 to 5.

I don't wat u mean by this..can u show it out to me..?? thanks..Sorry for the inconvenience.

0
 

Author Comment

by:ProfJL
ID: 10867550
hi I had changed my coding to it but it still doesn't works..y does it keep adding everything into the database?


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

public class VoteDialog extends JPanel {
      JButton voteButton;
    JLabel label;
    JFrame frame;
    String simpleDialogDesc = "The candidates";

       DataBase db = new DataBase();
    Connection connection = db.getConnection();

    public VoteDialog(JFrame frame) {
        this.frame = frame;
        JLabel title;
       
        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();
       
        System.out.println("passed createSimpleDialogBox");
               
        title = new JLabel("Click the \"Vote\" button"
                           + " once you have selected a candidate.",
                           JLabel.CENTER);
       
        label = new JLabel("Vote now!", JLabel.CENTER);
            label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);  
        add(label, BorderLayout.SOUTH);        
        add(choicePanel, BorderLayout.CENTER);
    }

    void setLabel(String newText) {
        label.setText(newText);
    }

    private JPanel createSimpleDialogBox() {
      final int numButtons = 5;
      final  JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

         final JButton voteButton;

       
         String defaultMessageCommand = "1";

       
        radioButtons[0] = new JRadioButton("1");
        radioButtons[0].setActionCommand(defaultMessageCommand);
       
        radioButtons[1] = new JRadioButton("2");
        radioButtons[1].setActionCommand(defaultMessageCommand);

        radioButtons[2] = new JRadioButton("3");
        radioButtons[2].setActionCommand(defaultMessageCommand);

        radioButtons[3] = new JRadioButton("4");
        radioButtons[3].setActionCommand(defaultMessageCommand);
            
            radioButtons[4] = new JRadioButton("5");
        radioButtons[4].setActionCommand(defaultMessageCommand);

     
        // Select the first button by default.
        radioButtons[0].setSelected(true);

      for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);}

          voteButton = new JButton("Vote");
       
        voteButton.addActionListener(new ActionListener() {
              
              
               int[] voteCounts = new int[radioButtons.length];  
            public void actionPerformed(ActionEvent e) {
                  String command = group.getSelection().getActionCommand();
                  
                      
                  int numberChosen = Integer.parseInt(command);
                        voteCounts[--numberChosen]++;

                  if(e.getSource()==voteButton)  {
                        
                        String insertString;
                          Statement statement;
                    
                          if(numberChosen==1) {
                                
                                    voteCounts[0]= Integer.parseInt(radioButtons[0].getText());
                                    insertString = "Insert into poly (nyp) "+
            " VALUES ( "+  voteCounts[0] +")";
                                    }
                                    
                                          if(numberChosen==2) {
                                
                                    voteCounts[1]= Integer.parseInt(radioButtons[1].getText());
                                    insertString = "Insert into poly (sp) "+
            " VALUES ( "+  voteCounts[1] +")";
                                    }
                                          if(numberChosen==3) {
                                
                                    voteCounts[2]= Integer.parseInt(radioButtons[2].getText());
                                    insertString = "Insert into poly (np) "+
            " VALUES ( "+  voteCounts[2] +")";
                                    }
                                      if(numberChosen==4) {
                                
                                    voteCounts[3]= Integer.parseInt(radioButtons[3].getText());
                                    insertString = "Insert into poly (rp) "+
            " VALUES ( "+  voteCounts[3] +")";
                                    }      
                                                if(numberChosen==5) {
                                
                                    voteCounts[4]= Integer.parseInt(radioButtons[4].getText());
                                    insertString = "Insert into poly (tp) "+
            " VALUES ( "+  voteCounts[4] +")";
                                    }                              
                                                                                          

                            //voteCounts[1]= Integer.parseInt(radioButtons[1].getText());
                            //voteCounts[2]= Integer.parseInt(radioButtons[2].getText());
                            //voteCounts[3]= Integer.parseInt(radioButtons[3].getText());
                            //voteCounts[4]= Integer.parseInt(radioButtons[4].getText());
                          
                                
                          
                    //      insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
            //" VALUES ( "+  voteCounts[0] +", "+  voteCounts[1] +", "+  voteCounts[2] +", "+  voteCounts[3] +","+  voteCounts[4] +")";
            
            try{
           statement = connection.createStatement();
           statement.executeUpdate(insertString);
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
     
                            
                                                                  
                // ok dialog
                if (command == defaultMessageCommand)
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                         
                return;                
        }                  
      }      
       
});

      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }
      
                            
                
                            
    public static void main(String[] args) {
        JFrame frame = new JFrame("VoteDialog");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new VoteDialog(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}
0
 

Author Comment

by:ProfJL
ID: 10869286
Hi CEHJ..

can u just help mi 1 last time by telling mi how to insert string from JRadioButtons into the microsoft access database??

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10869900
>>I don't wat u mean by this..can u show it out to me..??

>>String defaultMessageCommand = "1";

All action commands on all buttons get set to this value, which is wrong
0
 

Author Comment

by:ProfJL
ID: 10870214
ok thanks//

can u tell mi how to insert string from JRadioButtons into the microsoft access database??

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10870361
>>can u tell mi how to insert string from JRadioButtons into the microsoft access database??

Depends on whether you want to make an insert for each vote, or only at the end on the basis of cumulative votes
0
 

Author Comment

by:ProfJL
ID: 10870387
I want to insert string into the microsoft access database for each vote like if i click on the first option of the JRadioButton with text "nyp" , it will add it into the NYP column of the database only.

Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10870541
As i mentioned before, that's not how your interface should work or you'll get garbage in the database. What you should have is something like 'Choose your vote by clicking on....' then 'When you have done that, confirm your vote by clicking on the 'Confirm' button...'

Then you need an update statement to increase the count for that particular institution
0
 

Author Comment

by:ProfJL
ID: 10870738
That mean i don't use JRadioButtons for my voting system?
I will need to create 4 buttons for my choices
and add actionlistener in them..
Lastly then add into access database?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10870780
Yes, of course you use radio buttons - that's how they'd make their exclusive choice. You only need *one* confirm button. The database gets updated on confirmation
0
 

Author Comment

by:ProfJL
ID: 10870959
can i know the coding for the event handler of the confirm button??

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10871009
String command = group.getSelection().getActionCommand();
updateDatabase(Integer.parseInt(command));

Then write a small method to do the update called 'updateDatabase'
0
 

Author Comment

by:ProfJL
ID: 10871248

Hi CEHJ..would you please help me to finish up the rest of the coding because I am really urgently need this system to be done and It quite waste of time doing it step by step by myself which i don't know.Would really appreciate if yopu could help me out this time round.


import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.filechooser.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Event;
import javax.swing.text.*;
import java.sql.*;
import java.text.*;

public class VoteDialog extends JPanel implements ActionListener {
      JButton voteButton;
      JButton confirmButton;
    JLabel label;
    JFrame frame;
    String simpleDialogDesc = "The candidates";
      
      
       DataBase db = new DataBase();
    Connection connection = db.getConnection();

    public VoteDialog(JFrame frame) {
        this.frame = frame;
        JLabel title;
       
        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();
       
        System.out.println("passed createSimpleDialogBox");
               
        title = new JLabel("Click the \"Vote\" button"
                           + " once you have selected a candidate.",
                           JLabel.CENTER);
       
        label = new JLabel("Vote now!", JLabel.CENTER);
            label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);  
        add(label, BorderLayout.SOUTH);        
        add(choicePanel, BorderLayout.CENTER);
    }

    void setLabel(String newText) {
        label.setText(newText);
    }

    private JPanel createSimpleDialogBox() {
      final int numButtons = 5;
      final  JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

        final JButton voteButton;
            final JButton confirmButton;
            
       
        radioButtons[0] = new JRadioButton("1");

       
            radioButtons[1] = new JRadioButton("2");


        radioButtons[2] = new JRadioButton("3");


        radioButtons[3] = new JRadioButton("4");

            
            radioButtons[4] = new JRadioButton("5");


     
        // Select the first button by default.
        radioButtons[0].setSelected(true);

      for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);
            }
           
            voteButton = new JButton("Vote");
        confirmButton = new JButton("confirm");
        confirmButton.addActionListener(this);
        voteButton.addActionListener(this);
              
              
               int[] voteCounts = new int[radioButtons.length];  
              
            public void actionPerformed(ActionEvent e) {
                  String command = group.getSelection().getActionCommand();
                  updateDatabase(Integer.parseInt(command));

                      
                  int numberChosen = Integer.parseInt(command);
      voteCounts[--numberChosen]++;

                  if(e.getSource()==voteButton)  {
                        
                        String insertString;
                    Statement statement;
                          
                    
                                                                          
                                                                                          
              voteCounts[0]= Integer.parseInt(radioButtons[0].getText());  
              voteCounts[1]= Integer.parseInt(radioButtons[1].getText());
              voteCounts[2]= Integer.parseInt(radioButtons[2].getText());
              voteCounts[3]= Integer.parseInt(radioButtons[3].getText());
              voteCounts[4]= Integer.parseInt(radioButtons[4].getText());
                          
                                
                          
                          insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
            " VALUES ( "+  voteCounts[0] +", "+  voteCounts[1] +", "+  voteCounts[2] +", "+  voteCounts[3] +","+  voteCounts[4] +")";
            
            try{
           statement = connection.createStatement();
           statement.executeUpdate(insertString);
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
     
                            
                                                                  
               
        }                  
      }      
       


      System.out.println("calling createPane");  
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          confirmButton,
                          voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }
      
                            
                
                            
    public static void main(String[] args) {
        JFrame frame = new JFrame("VoteDialog");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new VoteDialog(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10871785
I'm not going to code it for you, but i'll supervise your doing it in easy steps if you want. The first step will be this:

1. Post the code that simply displays a panel containing the radio buttons *only* in a group, with their action commands set to the right numbers. Make sure it is runnable
0
 

Author Comment

by:ProfJL
ID: 10878386
Hi CEHJ..
thanks for your help my vote is more or less done..However I can't seem to add actionListener to my vote application.

It will prompt errors whenever I want to implements actionlistener to a JButton like: Identifier needed etc..Below are my codes..

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;

public class VoteDialog extends JPanel {
      JButton voteButton;
    JLabel label;
    JFrame frame;
    String simpleDialogDesc = "The Polythenics";
      JButton backButton;
      
       DataBase db = new DataBase();
    Connection connection = db.getConnection();
   
      private boolean DEBUG = true;

    public VoteDialog(JFrame frame) {
          
        this.frame = frame;
        JLabel title;    
       
        ArrayList nameList =new ArrayList();
           
        String[] columnNames = {"NYP",
                                            "SP",
                                            "NP",
                                            "RP",       
                                "TP"};

        String query;
        Statement statement;
        ResultSet rs;        
       
        String nyp="";
        String sp="";
        String np="";
        String rp="";
        String tp="";
       
        voteinfo s1;
        int i=0;
       
        query = "SELECT * FROM poly";
   
        try{
           statement = connection.createStatement();
           rs = statement.executeQuery(query);
     
           while (rs.next()){
              
                    
                    nyp = rs.getString("NYP");
                  sp = rs.getString("SP");
                  np = rs.getString("NP");
                  rp = rs.getString("RP");
                  tp = rs.getString("TP");
                  s1= new voteinfo(nyp,sp,np,rp,tp);
                  nameList.add(s1);
                  i++;                     
              
           }
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
           

        Object[][] data =  new Object[i][5];
        int count=0;
        while (count<i){
                s1 = (voteinfo) nameList.get(count);
                System.out.println("poly:" + s1.getp1());
                System.out.println("poly:" + s1.getp2());
                System.out.println("poly:" + s1.getp3());
                System.out.println("poly:" + s1.getp4());
                System.out.println("poly:" + s1.getp5());
                
                data[count][0] = s1.getp1();
                data[count][1] = s1.getp2();
                data[count][2] = s1.getp3();
                data[count][3] = s1.getp4();
                data[count][4] = s1.getp5();
             
              count++;
   
        }
                 
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(100, 100));
//
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

       

       
   

       
        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();
       
        System.out.println("passed createSimpleDialogBox");
               
        title = new JLabel("Click the \"Vote\" button"
                           + " once you have selected a poly.",
                           JLabel.CENTER);
       
        label = new JLabel("Vote now!", JLabel.CENTER);
            label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(20,20,5,20));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);  
        add(label, BorderLayout.SOUTH);        
        add(choicePanel, BorderLayout.CENTER);
        //Add the scroll pane to this window.
        add(scrollPane, BorderLayout.EAST);
    }

    void setLabel(String newText) {
        label.setText(newText);
    }

      private JPanel createSimpleDialogBox() {
      final int numButtons = 5;
      final  JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

        final JButton voteButton;

        final String action1 = "1";
        final String action2 = "2";
        final String action3 = "3";
        final String action4 = "4";
        final String action5 = "5";
       
       
       
        radioButtons[0] = new JRadioButton("1");
        radioButtons[0].setActionCommand(action1);
        JLabel nyp = new JLabel("<html><font color=red>NanYang Polythenic</font></html>");
            nyp.setBounds(60,33,150,60);
            add (nyp);
            
        radioButtons[1] = new JRadioButton("2");
        radioButtons[1].setActionCommand(action2);
        JLabel sp = new JLabel("<html><font color=blue>Singapore Polythenic</font></html>");
            sp.setBounds(60,58,150,60);
            add (sp);
            

        radioButtons[2] = new JRadioButton("3");
        radioButtons[2].setActionCommand(action3);
        JLabel np = new JLabel("<html><font color=yellow>Ngee Ann Polythenic</font></html>");
            np.setBounds(60,81,150,60);
            add (np);
            

        radioButtons[3] = new JRadioButton("4");
        radioButtons[3].setActionCommand(action4);
        JLabel rp = new JLabel("<html><font color=green>Republic Polythenic</font></html>");
            rp.setBounds(60,107,150,60);
            add (rp);
            
            
            radioButtons[4] = new JRadioButton("5");
        radioButtons[4].setActionCommand(action5);
        JLabel tp = new JLabel("<html><font color=navy>Singapore Polythenic</font></html>");
            tp.setBounds(60,130,150,60);
            add (tp);
            

     
        // Select the first button by default.
        radioButtons[0].setSelected(true);

      for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);
            }

        voteButton = new JButton("Vote");
        backButton = new JButton("Back");
       
        voteButton.addActionListener(new ActionListener() {
              
              
               int[] voteCounts = new int[radioButtons.length];
              
                 String insertString;
                
            public void actionPerformed(ActionEvent e) {
                  String command = group.getSelection().getActionCommand();
                  

                  
                      
                  int numberChosen = Integer.parseInt(command);
                        voteCounts[numberChosen]++;

                  if(e.getSource()==voteButton)  {
                        
                        
                          Statement statement;
                          
                            if (command == action1)   {
                voteCounts[0]= Integer.parseInt(radioButtons[0].getText());
                insertString = "Insert into poly (nyp) "+
            " VALUES ( "+  voteCounts[0] +")";
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                            }
                          
                           // ok dialog
                if (command == action2)   {
                voteCounts[1]= Integer.parseInt(radioButtons[1].getText());
                insertString = "Insert into poly (sp) "+
            " VALUES ( "+  voteCounts[1] +")";
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                            }
                           if (command == action3)   {
                voteCounts[2]= Integer.parseInt(radioButtons[2].getText());
                insertString = "Insert into poly (np) "+
            " VALUES ( "+  voteCounts[2] +")";
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                            }
                            if (command == action4)   {
                voteCounts[3]= Integer.parseInt(radioButtons[3].getText());
                insertString = "Insert into poly (rp) "+
            " VALUES ( "+  voteCounts[3] +")";
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                            }
                              if (command == action5)   {
                voteCounts[4]= Integer.parseInt(radioButtons[4].getText());
                insertString = "Insert into poly (tp) "+
            " VALUES ( "+  voteCounts[4] +")";
                      JOptionPane.showMessageDialog(frame,
                                "Your Vote Han Been Counted");
                            }
                                                                                                                                                                                                                                      
            try{
           statement = connection.createStatement();
           statement.executeUpdate(insertString);
           statement.close();
        }
        catch ( SQLException sqlex ) {
                sqlex.printStackTrace();
        }
  }                  
        
       
              
     
      }    
       
});



      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }
      
                      
                
                            
    public static void main(String[] args) {
        JFrame frame = new JFrame("VoteDialog");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new VoteDialog(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 74
changePi Challenge 15 76
groupSum6 challenge 6 38
java set up 1 46
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now