ProfJL
asked on
How to enter JRadioButtons into Access Database and retrieve it out?
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(BorderFact ory.create EmptyBorde r(10,10,10 ,10));
choicePanel.setBorder(Bord erFactory. createEmpt yBorder(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>Polyth enic 1: <font color=red>Nanyang Poly</font></html>");
radioButtons[0].setActionC ommand(def aultMessag eCommand);
radioButtons[1] = new JRadioButton("<html>Polyth enic 2: <font color=green>Singapore Poly</font></html>");
radioButtons[1].setActionC ommand(def aultMessag eCommand);
radioButtons[2] = new JRadioButton("<html>Polyth enic 3: <font color=blue>Temasek Poly</font></html>");
radioButtons[2].setActionC ommand(def aultMessag eCommand);
radioButtons[3] = new JRadioButton("<html>Polyth enic 4: <font color=maroon>Republic Poly</font></html>");
radioButtons[3].setActionC ommand(def aultMessag eCommand);
radioButtons[4] = new JRadioButton("<html>Polyth enic 5: <font color=yellow>Ngee Ann Poly</font></html>");
radioButtons[4].setActionC ommand(def aultMessag eCommand);
// Select the first button by default.
radioButtons[0].setSelecte d(true);
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;
}
voteButton = new JButton("Vote");
voteButton.addActionListen er(new ActionListener() {
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
if(e.getSource()==voteButt on) {
String insertString;
Statement statement;
if (radioButtons[i]=radioButt ons[0]) {
int p1= Integer.parseInt(radioButt ons[0].get Text());
int p2= Integer.parseInt(radioButt ons[1].get Text());
int p3= Integer.parseInt(radioButt ons[2].get Text());
int p4= Integer.parseInt(radioButt ons[3].get Text());
int p5= Integer.parseInt(radioButt ons[4].get Text());
insertString = "Insert into poly (nyp, sp, tp, rp, np) "+
" VALUES ( '"+ p1 +"', '"+ p2 +"', '"+ p3 +"', '"+ p4 +"','"+ p5 +"')";
try{
statement = connection.createStatement ();
statement.executeUpdate(in sertString );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
}
// ok dialog
if (command == defaultMessageCommand) {
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
}
return;
}
});
System.out.println("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing 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.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.pack();
frame.setVisible(true);
}
}
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
title = new JLabel("Click the \"Vote\" button"
+ " once you have selected a candidate.",
JLabel.CENTER);
label = new JLabel("Vote now!", JLabel.CENTER);
label.setBorder(BorderFact
choicePanel.setBorder(Bord
// 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>Polyth
radioButtons[0].setActionC
radioButtons[1] = new JRadioButton("<html>Polyth
radioButtons[1].setActionC
radioButtons[2] = new JRadioButton("<html>Polyth
radioButtons[2].setActionC
radioButtons[3] = new JRadioButton("<html>Polyth
radioButtons[3].setActionC
radioButtons[4] = new JRadioButton("<html>Polyth
radioButtons[4].setActionC
// Select the first button by default.
radioButtons[0].setSelecte
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
}
voteButton = new JButton("Vote");
voteButton.addActionListen
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
if(e.getSource()==voteButt
String insertString;
Statement statement;
if (radioButtons[i]=radioButt
int p1= Integer.parseInt(radioButt
int p2= Integer.parseInt(radioButt
int p3= Integer.parseInt(radioButt
int p4= Integer.parseInt(radioButt
int p5= Integer.parseInt(radioButt
insertString = "Insert into poly (nyp, sp, tp, rp, np) "+
" VALUES ( '"+ p1 +"', '"+ p2 +"', '"+ p3 +"', '"+ p4 +"','"+ p5 +"')";
try{
statement = connection.createStatement
statement.executeUpdate(in
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
}
// ok dialog
if (command == defaultMessageCommand) {
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
}
return;
}
});
System.out.println("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
frame.pack();
frame.setVisible(true);
}
}
If you're inserting ints then they shouldn't be quoted
e.g.
" VALUES ("+ p1 + ","+ p2
etc.
" VALUES ("+ p1 + ","+ p2
etc.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or
if (radioButtons[i]==radioBut tons[0])
if (radioButtons[i]==radioBut
ASKER
Hi CEHJ...
I had changed my codes but when i used if (radioButtons[i]==radioBut tons[0]) ....it say
C:\Documents and Settings\JL\Desktop\filech ooser\Vote Dialog.jav a:99: cannot resolve symbol
symbol: variable i
if (radioButtons[i]==radioBut tons[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]==radioBut tons[0]) {
p1=0+1;
p1++;
}
I had changed my codes but when i used if (radioButtons[i]==radioBut
C:\Documents and Settings\JL\Desktop\filech
symbol: variable i
if (radioButtons[i]==radioBut
}
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]==radioBut
p1=0+1;
p1++;
}
>>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
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
ASKER
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(BorderFact ory.create EmptyBorde r(10,10,10 ,10));
choicePanel.setBorder(Bord erFactory. createEmpt yBorder(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>Polyth enic 1: <font color=red>Nanyang Poly</font></html>");
radioButtons[0].setActionC ommand(def aultMessag eCommand);
radioButtons[1] = new JRadioButton("<html>Polyth enic 2: <font color=green>Singapore Poly</font></html>");
radioButtons[1].setActionC ommand(def aultMessag eCommand);
radioButtons[2] = new JRadioButton("<html>Polyth enic 3: <font color=blue>Temasek Poly</font></html>");
radioButtons[2].setActionC ommand(def aultMessag eCommand);
radioButtons[3] = new JRadioButton("<html>Polyth enic 4: <font color=maroon>Republic Poly</font></html>");
radioButtons[3].setActionC ommand(def aultMessag eCommand);
radioButtons[4] = new JRadioButton("<html>Polyth enic 5: <font color=yellow>Ngee Ann Poly</font></html>");
radioButtons[4].setActionC ommand(def aultMessag eCommand);
// Select the first button by default.
radioButtons[0].setSelecte d(true);
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;}
voteButton = new JButton("Vote");
voteButton.addActionListen er(new ActionListener() {
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
if(e.getSource()==voteButt on) {
int p1,p2,p3,p4,p5;
String insertString;
Statement statement;
while (radioButtons[i]==radioBut tons[0]) {
p1=0+1;
p1++;
p1= Integer.parseInt(radioButt ons[0].get Text());
p2= Integer.parseInt(radioButt ons[1].get Text());
p3= Integer.parseInt(radioButt ons[2].get Text());
p4= Integer.parseInt(radioButt ons[3].get Text());
p5= Integer.parseInt(radioButt ons[4].get Text());
}
insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
try{
statement = connection.createStatement ();
statement.executeUpdate(in sertString );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
return;
}
});
System.out.println("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing 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.setDefaultCloseOpera tion(JFram e.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..
>>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
title = new JLabel("Click the \"Vote\" button"
+ " once you have selected a candidate.",
JLabel.CENTER);
label = new JLabel("Vote now!", JLabel.CENTER);
label.setBorder(BorderFact
choicePanel.setBorder(Bord
// 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>Polyth
radioButtons[0].setActionC
radioButtons[1] = new JRadioButton("<html>Polyth
radioButtons[1].setActionC
radioButtons[2] = new JRadioButton("<html>Polyth
radioButtons[2].setActionC
radioButtons[3] = new JRadioButton("<html>Polyth
radioButtons[3].setActionC
radioButtons[4] = new JRadioButton("<html>Polyth
radioButtons[4].setActionC
// Select the first button by default.
radioButtons[0].setSelecte
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
voteButton = new JButton("Vote");
voteButton.addActionListen
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
if(e.getSource()==voteButt
int p1,p2,p3,p4,p5;
String insertString;
Statement statement;
while (radioButtons[i]==radioBut
p1=0+1;
p1++;
p1= Integer.parseInt(radioButt
p2= Integer.parseInt(radioButt
p3= Integer.parseInt(radioButt
p4= Integer.parseInt(radioButt
p5= Integer.parseInt(radioButt
}
insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
try{
statement = connection.createStatement
statement.executeUpdate(in
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
return;
}
});
System.out.println("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
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..
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]++;
int[] voteCounts = new int[radioButtons.length];
// (if first button selected)
voteCounts[0]++;
ASKER
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.NumberFormatExce ption: For input string: "<html>Polythenic 1: <font co
lor=red>Nanyang Poly</font></html>"
at java.lang.NumberFormatExce ption.forI nputString (NumberFor matExcepti on.
java:48)
at java.lang.Integer.parseInt (Integer.j ava:468)
at java.lang.Integer.parseInt (Integer.j ava:518)
at VoteDialog$1.actionPerform ed(VoteDia log.java:9 7)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :17
86)
at javax.swing.AbstractButton $ForwardAc tionEvents .actionPer formed(Abs tra
ctButton.java:1839)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del
.java:420)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 258
)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (BasicButt onL
istener.java:245)
at java.awt.Component.process MouseEvent (Component .java:5100 )
at java.awt.Component.process Event(Comp onent.java :4897)
at java.awt.Container.process Event(Cont ainer.java :1569)
at java.awt.Component.dispatc hEventImpl (Component .java:3615 )
at java.awt.Container.dispatc hEventImpl (Container .java:1627 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3477)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:3 483
)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:31 98)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:3128)
at java.awt.Container.dispatc hEventImpl (Container .java:1613 )
at java.awt.Window.dispatchEv entImpl(Wi ndow.java: 1606)
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3477)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:456)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hTh
read.java:201)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:151)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 45)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 37)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:100)
what is happening..plz enlighten me..thanks.
java.lang.NumberFormatExce
lor=red>Nanyang Poly</font></html>"
at java.lang.NumberFormatExce
java:48)
at java.lang.Integer.parseInt
at java.lang.Integer.parseInt
at VoteDialog$1.actionPerform
at javax.swing.AbstractButton
86)
at javax.swing.AbstractButton
ctButton.java:1839)
at javax.swing.DefaultButtonM
.java:420)
at javax.swing.DefaultButtonM
)
at javax.swing.plaf.basic.Bas
istener.java:245)
at java.awt.Component.process
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
)
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
read.java:201)
at java.awt.EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
what is happening..plz enlighten me..thanks.
You're trying to convert that unconvertable radio button text to a number
ASKER
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(BorderFact ory.create EmptyBorde r(10,10,10 ,10));
choicePanel.setBorder(Bord erFactory. createEmpt yBorder(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].setActionC ommand(def aultMessag eCommand);
radioButtons[1] = new JRadioButton("2");
radioButtons[1].setActionC ommand(def aultMessag eCommand);
radioButtons[2] = new JRadioButton("3");
radioButtons[2].setActionC ommand(def aultMessag eCommand);
radioButtons[3] = new JRadioButton("4");
radioButtons[3].setActionC ommand(def aultMessag eCommand);
radioButtons[4] = new JRadioButton("5");
radioButtons[4].setActionC ommand(def aultMessag eCommand);
// Select the first button by default.
radioButtons[0].setSelecte d(true);
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;}
voteButton = new JButton("Vote");
voteButton.addActionListen er(new ActionListener() {
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
if(e.getSource()==voteButt on) {
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(radioButt ons[0].get Text());
p2= Integer.parseInt(radioButt ons[1].get Text());
p3= Integer.parseInt(radioButt ons[2].get Text());
p4= Integer.parseInt(radioButt ons[3].get Text());
p5= Integer.parseInt(radioButt ons[4].get Text());
insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
try{
statement = connection.createStatement ();
statement.executeUpdate(in sertString );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
return;
}
}
});
System.out.println("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing 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.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.pack();
frame.setVisible(true);
}
}
"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
title = new JLabel("Click the \"Vote\" button"
+ " once you have selected a candidate.",
JLabel.CENTER);
label = new JLabel("Vote now!", JLabel.CENTER);
label.setBorder(BorderFact
choicePanel.setBorder(Bord
// 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].setActionC
radioButtons[1] = new JRadioButton("2");
radioButtons[1].setActionC
radioButtons[2] = new JRadioButton("3");
radioButtons[2].setActionC
radioButtons[3] = new JRadioButton("4");
radioButtons[3].setActionC
radioButtons[4] = new JRadioButton("5");
radioButtons[4].setActionC
// Select the first button by default.
radioButtons[0].setSelecte
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
voteButton = new JButton("Vote");
voteButton.addActionListen
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
if(e.getSource()==voteButt
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(radioButt
p2= Integer.parseInt(radioButt
p3= Integer.parseInt(radioButt
p4= Integer.parseInt(radioButt
p5= Integer.parseInt(radioButt
insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
try{
statement = connection.createStatement
statement.executeUpdate(in
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
return;
}
}
});
System.out.println("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
frame.pack();
frame.setVisible(true);
}
}
>>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().getAc tionComman d();
you need to do
int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen] ++;
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().getAc
you need to do
int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen]
ASKER
It doesn't seem to work and now it can't add into database..
an error occurs like this shown:
java.lang.NumberFormatExce ption: For input string: "default"
at java.lang.NumberFormatExce ption.forI nputString (NumberFor matExcepti on.
java:48)
at java.lang.Integer.parseInt (Integer.j ava:468)
at java.lang.Integer.parseInt (Integer.j ava:518)
at VoteDialog$1.actionPerform ed(VoteDia log.java:8 9)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :17
86)
at javax.swing.AbstractButton $ForwardAc tionEvents .actionPer formed(Abs tra
ctButton.java:1839)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del
.java:420)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 258
)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (BasicButt onL
istener.java:245)
at java.awt.Component.process MouseEvent (Component .java:5100 )
at java.awt.Component.process Event(Comp onent.java :4897)
at java.awt.Container.process Event(Cont ainer.java :1569)
at java.awt.Component.dispatc hEventImpl (Component .java:3615 )
at java.awt.Container.dispatc hEventImpl (Container .java:1627 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3477)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:3 483
)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:31 98)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:3128)
at java.awt.Container.dispatc hEventImpl (Container .java:1613 )
at java.awt.Window.dispatchEv entImpl(Wi ndow.java: 1606)
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3477)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:456)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hTh
read.java:201)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:151)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 45)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 37)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:100)
an error occurs like this shown:
java.lang.NumberFormatExce
at java.lang.NumberFormatExce
java:48)
at java.lang.Integer.parseInt
at java.lang.Integer.parseInt
at VoteDialog$1.actionPerform
at javax.swing.AbstractButton
86)
at javax.swing.AbstractButton
ctButton.java:1839)
at javax.swing.DefaultButtonM
.java:420)
at javax.swing.DefaultButtonM
)
at javax.swing.plaf.basic.Bas
istener.java:245)
at java.awt.Component.process
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
)
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
read.java:201)
at java.awt.EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
Make sure one button is always selected
And the action commands should all be numbers - at the moment you're using "default'
ASKER
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(BorderFact ory.create EmptyBorde r(10,10,10 ,10));
choicePanel.setBorder(Bord erFactory. createEmpt yBorder(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].setActionC ommand(def aultMessag eCommand);
radioButtons[1] = new JRadioButton("2");
radioButtons[1].setActionC ommand(def aultMessag eCommand);
radioButtons[2] = new JRadioButton("3");
radioButtons[2].setActionC ommand(def aultMessag eCommand);
radioButtons[3] = new JRadioButton("4");
radioButtons[3].setActionC ommand(def aultMessag eCommand);
radioButtons[4] = new JRadioButton("5");
radioButtons[4].setActionC ommand(def aultMessag eCommand);
// Select the first button by default.
radioButtons[0].setSelecte d(true);
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;}
voteButton = new JButton("Vote");
voteButton.addActionListen er(new ActionListener() {
int[] voteCounts = new int[radioButtons.length];
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen] ++;
if(e.getSource()==voteButt on) {
int p1,p2,p3,p4,p5;
String insertString;
Statement statement;
p1= Integer.parseInt(radioButt ons[0].get Text());
p2= Integer.parseInt(radioButt ons[1].get Text());
p3= Integer.parseInt(radioButt ons[2].get Text());
p4= Integer.parseInt(radioButt ons[3].get Text());
p5= Integer.parseInt(radioButt ons[4].get Text());
insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
try{
statement = connection.createStatement ();
statement.executeUpdate(in sertString );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
return;
}
}
});
System.out.println("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing 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.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.pack();
frame.setVisible(true);
}
}
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
title = new JLabel("Click the \"Vote\" button"
+ " once you have selected a candidate.",
JLabel.CENTER);
label = new JLabel("Vote now!", JLabel.CENTER);
label.setBorder(BorderFact
choicePanel.setBorder(Bord
// 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].setActionC
radioButtons[1] = new JRadioButton("2");
radioButtons[1].setActionC
radioButtons[2] = new JRadioButton("3");
radioButtons[2].setActionC
radioButtons[3] = new JRadioButton("4");
radioButtons[3].setActionC
radioButtons[4] = new JRadioButton("5");
radioButtons[4].setActionC
// Select the first button by default.
radioButtons[0].setSelecte
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
voteButton = new JButton("Vote");
voteButton.addActionListen
int[] voteCounts = new int[radioButtons.length];
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen]
if(e.getSource()==voteButt
int p1,p2,p3,p4,p5;
String insertString;
Statement statement;
p1= Integer.parseInt(radioButt
p2= Integer.parseInt(radioButt
p3= Integer.parseInt(radioButt
p4= Integer.parseInt(radioButt
p5= Integer.parseInt(radioButt
insertString = "Insert into poly (nyp, sp, np, rp, tp) "+
" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
try{
statement = connection.createStatement
statement.executeUpdate(in
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
return;
}
}
});
System.out.println("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
frame.pack();
frame.setVisible(true);
}
}
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
So
>>" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
should be
" VALUES ( "+ voteCounts[0] +", "+ voteCounts1] +", "+ voteCounts[2] +", "+ voteCounts[3] +","+ voteCounts[4] +")";
>>" VALUES ( "+ p1 +", "+ p2 +", "+ p3 +", "+ p4 +","+ p5 +")";
should be
" VALUES ( "+ voteCounts[0] +", "+ voteCounts1] +", "+ voteCounts[2] +", "+ voteCounts[3] +","+ voteCounts[4] +")";
ASKER
>>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.
I don't wat u mean by this..can u show it out to me..?? thanks..Sorry for the inconvenience.
ASKER
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(BorderFact ory.create EmptyBorde r(10,10,10 ,10));
choicePanel.setBorder(Bord erFactory. createEmpt yBorder(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].setActionC ommand(def aultMessag eCommand);
radioButtons[1] = new JRadioButton("2");
radioButtons[1].setActionC ommand(def aultMessag eCommand);
radioButtons[2] = new JRadioButton("3");
radioButtons[2].setActionC ommand(def aultMessag eCommand);
radioButtons[3] = new JRadioButton("4");
radioButtons[3].setActionC ommand(def aultMessag eCommand);
radioButtons[4] = new JRadioButton("5");
radioButtons[4].setActionC ommand(def aultMessag eCommand);
// Select the first button by default.
radioButtons[0].setSelecte d(true);
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;}
voteButton = new JButton("Vote");
voteButton.addActionListen er(new ActionListener() {
int[] voteCounts = new int[radioButtons.length];
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen] ++;
if(e.getSource()==voteButt on) {
String insertString;
Statement statement;
if(numberChosen==1) {
voteCounts[0]= Integer.parseInt(radioButt ons[0].get Text());
insertString = "Insert into poly (nyp) "+
" VALUES ( "+ voteCounts[0] +")";
}
if(numberChosen==2) {
voteCounts[1]= Integer.parseInt(radioButt ons[1].get Text());
insertString = "Insert into poly (sp) "+
" VALUES ( "+ voteCounts[1] +")";
}
if(numberChosen==3) {
voteCounts[2]= Integer.parseInt(radioButt ons[2].get Text());
insertString = "Insert into poly (np) "+
" VALUES ( "+ voteCounts[2] +")";
}
if(numberChosen==4) {
voteCounts[3]= Integer.parseInt(radioButt ons[3].get Text());
insertString = "Insert into poly (rp) "+
" VALUES ( "+ voteCounts[3] +")";
}
if(numberChosen==5) {
voteCounts[4]= Integer.parseInt(radioButt ons[4].get Text());
insertString = "Insert into poly (tp) "+
" VALUES ( "+ voteCounts[4] +")";
}
//voteCounts[1]= Integer.parseInt(radioButt ons[1].get Text());
//voteCounts[2]= Integer.parseInt(radioButt ons[2].get Text());
//voteCounts[3]= Integer.parseInt(radioButt ons[3].get Text());
//voteCounts[4]= Integer.parseInt(radioButt ons[4].get Text());
// 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(in sertString );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
return;
}
}
});
System.out.println("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing 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.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.pack();
frame.setVisible(true);
}
}
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
title = new JLabel("Click the \"Vote\" button"
+ " once you have selected a candidate.",
JLabel.CENTER);
label = new JLabel("Vote now!", JLabel.CENTER);
label.setBorder(BorderFact
choicePanel.setBorder(Bord
// 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].setActionC
radioButtons[1] = new JRadioButton("2");
radioButtons[1].setActionC
radioButtons[2] = new JRadioButton("3");
radioButtons[2].setActionC
radioButtons[3] = new JRadioButton("4");
radioButtons[3].setActionC
radioButtons[4] = new JRadioButton("5");
radioButtons[4].setActionC
// Select the first button by default.
radioButtons[0].setSelecte
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
voteButton = new JButton("Vote");
voteButton.addActionListen
int[] voteCounts = new int[radioButtons.length];
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen]
if(e.getSource()==voteButt
String insertString;
Statement statement;
if(numberChosen==1) {
voteCounts[0]= Integer.parseInt(radioButt
insertString = "Insert into poly (nyp) "+
" VALUES ( "+ voteCounts[0] +")";
}
if(numberChosen==2) {
voteCounts[1]= Integer.parseInt(radioButt
insertString = "Insert into poly (sp) "+
" VALUES ( "+ voteCounts[1] +")";
}
if(numberChosen==3) {
voteCounts[2]= Integer.parseInt(radioButt
insertString = "Insert into poly (np) "+
" VALUES ( "+ voteCounts[2] +")";
}
if(numberChosen==4) {
voteCounts[3]= Integer.parseInt(radioButt
insertString = "Insert into poly (rp) "+
" VALUES ( "+ voteCounts[3] +")";
}
if(numberChosen==5) {
voteCounts[4]= Integer.parseInt(radioButt
insertString = "Insert into poly (tp) "+
" VALUES ( "+ voteCounts[4] +")";
}
//voteCounts[1]= Integer.parseInt(radioButt
//voteCounts[2]= Integer.parseInt(radioButt
//voteCounts[3]= Integer.parseInt(radioButt
//voteCounts[4]= Integer.parseInt(radioButt
// 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(in
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
// ok dialog
if (command == defaultMessageCommand)
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
return;
}
}
});
System.out.println("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
frame.pack();
frame.setVisible(true);
}
}
ASKER
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
can u just help mi 1 last time by telling mi how to insert string from JRadioButtons into the microsoft access database??
Thanks
>>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
>>String defaultMessageCommand = "1";
All action commands on all buttons get set to this value, which is wrong
ASKER
ok thanks//
can u tell mi how to insert string from JRadioButtons into the microsoft access database??
Thanks
can u tell mi how to insert string from JRadioButtons into the microsoft access database??
Thanks
>>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
Depends on whether you want to make an insert for each vote, or only at the end on the basis of cumulative votes
ASKER
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.
Thanks.
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
Then you need an update statement to increase the count for that particular institution
ASKER
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?
I will need to create 4 buttons for my choices
and add actionlistener in them..
Lastly then add into access database?
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
ASKER
can i know the coding for the event handler of the confirm button??
String command = group.getSelection().getAc tionComman d();
updateDatabase(Integer.par seInt(comm and));
Then write a small method to do the update called 'updateDatabase'
updateDatabase(Integer.par
Then write a small method to do the update called 'updateDatabase'
ASKER
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
title = new JLabel("Click the \"Vote\" button"
+ " once you have selected a candidate.",
JLabel.CENTER);
label = new JLabel("Vote now!", JLabel.CENTER);
label.setBorder(BorderFact
choicePanel.setBorder(Bord
// 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].setSelecte
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
}
voteButton = new JButton("Vote");
confirmButton = new JButton("confirm");
confirmButton.addActionLis
voteButton.addActionListen
int[] voteCounts = new int[radioButtons.length];
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
updateDatabase(Integer.par
int numberChosen = Integer.parseInt(command);
voteCounts[--numberChosen]
if(e.getSource()==voteButt
String insertString;
Statement statement;
voteCounts[0]= Integer.parseInt(radioButt
voteCounts[1]= Integer.parseInt(radioButt
voteCounts[2]= Integer.parseInt(radioButt
voteCounts[3]= Integer.parseInt(radioButt
voteCounts[4]= Integer.parseInt(radioButt
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(in
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
}
System.out.println("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
frame.pack();
frame.setVisible(true);
}
}
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
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
ASKER
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(que ry);
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.setPreferredScrollab leViewport Size(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(BorderFact ory.create EmptyBorde r(10,10,10 ,10));
choicePanel.setBorder(Bord erFactory. createEmpt yBorder(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].setActionC ommand(act ion1);
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].setActionC ommand(act ion2);
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].setActionC ommand(act ion3);
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].setActionC ommand(act ion4);
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].setActionC ommand(act ion5);
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].setSelecte d(true);
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;
}
voteButton = new JButton("Vote");
backButton = new JButton("Back");
voteButton.addActionListen er(new ActionListener() {
int[] voteCounts = new int[radioButtons.length];
String insertString;
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
int numberChosen = Integer.parseInt(command);
voteCounts[numberChosen]++ ;
if(e.getSource()==voteButt on) {
Statement statement;
if (command == action1) {
voteCounts[0]= Integer.parseInt(radioButt ons[0].get Text());
insertString = "Insert into poly (nyp) "+
" VALUES ( "+ voteCounts[0] +")";
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
}
// ok dialog
if (command == action2) {
voteCounts[1]= Integer.parseInt(radioButt ons[1].get Text());
insertString = "Insert into poly (sp) "+
" VALUES ( "+ voteCounts[1] +")";
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
}
if (command == action3) {
voteCounts[2]= Integer.parseInt(radioButt ons[2].get Text());
insertString = "Insert into poly (np) "+
" VALUES ( "+ voteCounts[2] +")";
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
}
if (command == action4) {
voteCounts[3]= Integer.parseInt(radioButt ons[3].get Text());
insertString = "Insert into poly (rp) "+
" VALUES ( "+ voteCounts[3] +")";
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
}
if (command == action5) {
voteCounts[4]= Integer.parseInt(radioButt ons[4].get Text());
insertString = "Insert into poly (tp) "+
" VALUES ( "+ voteCounts[4] +")";
JOptionPane.showMessageDia log(frame,
"Your Vote Han Been Counted");
}
try{
statement = connection.createStatement ();
statement.executeUpdate(in sertString );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
}
});
System.out.println("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing 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.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.pack();
frame.setVisible(true);
}
}
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(que
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:"
System.out.println("poly:"
System.out.println("poly:"
System.out.println("poly:"
System.out.println("poly:"
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.setPreferredScrollab
//
//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
title = new JLabel("Click the \"Vote\" button"
+ " once you have selected a poly.",
JLabel.CENTER);
label = new JLabel("Vote now!", JLabel.CENTER);
label.setBorder(BorderFact
choicePanel.setBorder(Bord
// 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].setActionC
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].setActionC
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].setActionC
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].setActionC
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].setActionC
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].setSelecte
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
}
voteButton = new JButton("Vote");
backButton = new JButton("Back");
voteButton.addActionListen
int[] voteCounts = new int[radioButtons.length];
String insertString;
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
int numberChosen = Integer.parseInt(command);
voteCounts[numberChosen]++
if(e.getSource()==voteButt
Statement statement;
if (command == action1) {
voteCounts[0]= Integer.parseInt(radioButt
insertString = "Insert into poly (nyp) "+
" VALUES ( "+ voteCounts[0] +")";
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
}
// ok dialog
if (command == action2) {
voteCounts[1]= Integer.parseInt(radioButt
insertString = "Insert into poly (sp) "+
" VALUES ( "+ voteCounts[1] +")";
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
}
if (command == action3) {
voteCounts[2]= Integer.parseInt(radioButt
insertString = "Insert into poly (np) "+
" VALUES ( "+ voteCounts[2] +")";
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
}
if (command == action4) {
voteCounts[3]= Integer.parseInt(radioButt
insertString = "Insert into poly (rp) "+
" VALUES ( "+ voteCounts[3] +")";
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
}
if (command == action5) {
voteCounts[4]= Integer.parseInt(radioButt
insertString = "Insert into poly (tp) "+
" VALUES ( "+ voteCounts[4] +")";
JOptionPane.showMessageDia
"Your Vote Han Been Counted");
}
try{
statement = connection.createStatement
statement.executeUpdate(in
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
}
});
System.out.println("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
frame.pack();
frame.setVisible(true);
}
}