I have the following 4 JChecboxes in my form. If the user clicks all the four, or any choices of JCheckboxes how do I save the values from the checboxes in mysql database in one sigle column? When I click my Add button, it should stored all the values from the selected checkboxes Please help.. Thanks
My codes :
foreign = new JCheckBox("Foreign");
foreign.setFont(new Font("Tahoma", Font.BOLD, 12));
foreign.setForeground(new Color(240, 255, 240));
foreign.setBounds(16, 25, 97, 23);
foreign.setOpaque(false);
panel.add(foreign);
travelling = new JCheckBox("Travelling");
travelling.setFont(new Font("Tahoma", Font.BOLD, 12));
travelling.setForeground(new Color(240, 255, 240));
travelling.setBounds(150, 26, 97, 23);
travelling.setOpaque(false);
panel.add(travelling);
danger = new JCheckBox("Danger Pay");
danger.setFont(new Font("Tahoma", Font.BOLD, 12));
danger.setForeground(new Color(240, 255, 240));
danger.setBounds(16, 68, 97, 23);
danger.setOpaque(false);
panel.add(danger);
medical = new JCheckBox("Medical Scheme");
medical.setFont(new Font("Tahoma", Font.BOLD, 12));
medical.setForeground(new Color(240, 255, 240));
medical.setBounds(150, 69, 121, 23);
medical.setOpaque(false);
panel.add(medical);
add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id1 = Integer.parseInt(id.getText());
String fn = fname.getText();
String ln = lname.getText();
String po = pos.getText();
String sa = sal.getText();
String all = "";
if(foreign.isSelected() && travelling.isSelected() && danger.isSelected() && medical.isSelected()){
all = "Foreign, Travelling, Danger, Medical";
}else if(foreign.isSelected() && travelling.isSelected() && danger.isSelected()){
all = "Foreign, Travelling, Danger";
}else if(foreign.isSelected() && travelling.isSelected()){
all = "Foreign, Travelling";
}else if()
}
});
You could save the data in a separate table. What I mean by this is that suppose you are storing the data in a table temp which is having 2 columns - id and selection. Now my suggestion is that you create 2 tables one for id and the other for selection. the selection table will have 2 columns id and selection. the id column is to be a foreign key to the id table's id column.
Now when the user having id say 01 selects "Foreign" & "Travelling", then you make 2 entries in the selection table that are 01 - Foreign and 01 - Travelling
Now when the user having id say 02 selects all the options then you are to make 4 entries in the selection table against the id of the user:
02-Foreign,
02-Travelling,
02-Danger,
02-Medical
This way you could save each selection separately.
After getting you comment
After your comment I could only suggest that you create 4 columns of boolean type in the table and store the state of each checkBox in each column.
Related
JComboBox cBox = new JComboBox();
cBox.addItem("Food"); String x = "Food";
cBox.addItem("Shirt");
cBox.addItem("Computer");
cBox.setSelectedItem(null);
cBox.setBounds(56, 127, 336, 27);
contentPane.add(cBox);
tPName = new JTextArea();
tPName.setBounds(38, 227, 130, 26);
contentPane.add(tPName);
tPName.setColumns(10);
tSName = new JTextArea();
tSName.setBounds(262, 227, 130, 26);
contentPane.add(tSName);
tSName.setColumns(10);
JButton btn = new JButton("Further Details");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = (String) cBox.getSelectedItem(); //gets value of combo box
tPName.setText(name); //text area displays value
String x = (String) tPName.getText();
name = "Food"; tSName.setText(F);
name = "Shirt"; tSName.setText(S);
name ="Computer"; tSName.setText(C);
}
});
btn.setBounds(162, 166, 117, 29);
contentPane.add(btn);
how would I make tSName display a text corresponding to a specific value of tPName which is copied from a combobox where F, S and C strings stand for those corresponding values I want to use.
I'm not sure if I understand you correctly. You try to make one textarea display something, when the text of another textarea changes, am I right? If yes, you should look for an Event of the first Textbox (something like tPNameKeyTyped) in this Event you can check the tPName.getText() simply with
if(tPName.getText().equals("whatever")){
tSName.setText("whatever u want");
}
Is that what you are looking for?
I right now dont have pc so I cant check errors but.
this should work
tPName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tSName.setText(tPName.getText()); // put the value of tPName to tSName.
// i think this is all you want. i actually cant get your question.
// you can put a switch case to put coreesponding values in tSName. like:
switch(tPName.getText()){
case "a" : tSName.setText("1");
break;
//..... so on
default:
tSName.setText("stack overflow is great");
break;
}
}
});
EDIT:- Onece you do this try to refresh and validate the textareas.
tPName.validate();
tSName.validate();
tPName.repaint(); // refresh still not sure as i dont have pc if this not works try <your jframe>.repaint(); this should work
Ok, so I am stuck with these loops. I have a tabbedPane and I add tabs to it, via data from database which I get with the line con.getKat() - (list of strings that represent categories). Then every tab should contain a JTable with contents from my database with all the records that contain the category(String) from the selected tab. The problem is that every tab triggers the ChangeListener() (which i check with System.out.println(kategorija)) but only the last tab displays data (no matter how i add or remove tabs it's always the last tab). Might be something silly here but any help appreciated.
ArrayList<String> ls = con.getKat();
for(String s : ls){
pane = new JScrollPane();
tabbedPane.addTab(s, null, pane, null);
}
tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
kategorija = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
System.out.println(kategorija);
ArrayList<Ceni> artikli = con.getHuroCeni(kategorija);
dtm.setRowCount(0);
for(Ceni c : artikli){
dtm.addRow(new Object[]{c.sifra, c.name, String.valueOf(c.cena1), c.tarifa});
table = new JTable();
table.setFont(new Font("Times New Roman", Font.PLAIN, 16));
table.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128, 128, 128), null, null, null));
pane.setViewportView(table);
table.setModel(dtm);
}
}
});
Hello fellow programmers across the world I am currently having a problem with one of my frames in my GUI. I have managed to change the font of my messeges to itallic and the color to grey and i have set my textfield to editable(true). The problem is that when I delete the string in gray and italic and try to write something useful in the textfield, rather that plain font and a color of black for the string it always comes out italic and gray. Is there any way that a string in a text message can change font and color and then then next string you write to have the normal font and black color? Would appreciate any sugggestions at this point. Here is my code:
void makeSecondFrame(){
frame2 = new JFrame("Step one!Vertification!");
Container contentPane2 = frame2.getContentPane();
JLabel label2 = new JLabel("Welcome to step 1 of our algorithm, Verification!" + "To varify yourself of owning an account please enter the following:");
JLabel label3 = new JLabel("First Name:");
JLabel label4 = new JLabel("Middle Name:");
JLabel label5 = new JLabel("Surname:");
JLabel label6 = new JLabel("Account Number:");
JLabel label7 = new JLabel ("Registration Number:");
firstName = new TextField("Enter your name here");
firstName.setForeground(Color.gray);
firstName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
firstName.setEditable(true);
middleName = new TextField("Enter your middle name here");
middleName.setForeground(Color.gray);
middleName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
middleName.setEditable(true);
surname = new TextField("Enter your surname here");
surname.setForeground(Color.gray);
surname.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
surname.setEditable(true);
accountNumber = new TextField("Enter your main account number");
accountNumber.setForeground(Color.gray);
accountNumber.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
accountNumber.setEditable(true);
registrationNumber = new TextField("Enter your registration number");
registrationNumber.setForeground(Color.gray);
registrationNumber.setFont(new java.awt.Font("Arail", Font.ITALIC, 12));
registrationNumber.setEditable(true);
JPanel p2 = new JPanel(new GridLayout(1,1));
p2.add(label2);
contentPane2.add(p2, BorderLayout.NORTH);
JPanel p3 = new JPanel(new GridLayout(10,1));
p3.add(label3);
p3.add(firstName);
p3.add(label4);
p3.add(middleName);
p3.add(label5);
p3.add(surname);
p3.add(label7);
p3.add(registrationNumber);
p3.add(label6);
p3.add(accountNumber);
contentPane2.add(p3, BorderLayout.WEST);
JButton next = new JButton("Next!");
next.addActionListener(this);
JButton cancel = new JButton("Cancel");
cancel.addActionListener(this);
JPanel p4 = new JPanel(new GridLayout(1,1));
p4.add(next);
p4.add(cancel);
contentPane2.add(p4, BorderLayout.SOUTH);
frame2.pack();
frame2.setVisible(true);
}
You need to act on an appropriate action on the JTextField. The best I can think of is the FocusListener.focusGained():
firstName = new TextField("Enter your name here");
firstName.setForeground(Color.gray);
firstName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
firstName.setEditable(true);
firstName.addFocuListener(new FocusAdapter(){
#Overwrite
public void focusGained(FocusEvent e){
firstName.setText("");// remove help message, will also remove previously entered text!
firstName.setFont(new JTextField().getFont()); // reset to default
}
}
Well that did remove the helper message – Христо Петков
as I mentioned...
and I want to keep it – Христо Петков
Then you might want to use the other method in the FocusListener interface too:
#Overwrite
public void focusLost(FocusEvent e){
if(userDidNotEnterTextIn(firstName){ // hopefully you know how to find out...
firstName.setText("Enter your name here");
// do formatting again
}
}
I have the following method which pops up a window when a button is clicked.
public void cardPopUp() {
String[] cardTypes = {"Visa", "Mastercard", "Electron", "Cashcard"};
JComboBox combo = new JComboBox(cardTypes);
JTextField field1 = new JTextField("");
JTextField field2 = new JTextField("");
JTextField field3 = new JTextField("");
JTextField field4 = new JTextField("");
JTextField field5 = new JTextField("");
JPanel panel = new JPanel(new GridLayout(0, 2));
panel.add(new JLabel("Card Type:"));
panel.add(combo);
panel.add(new JLabel("Cardholder Name:"));
panel.add(field1);
panel.add(new JLabel("Card Number:"));
panel.add(field2);
panel.add(new JLabel("Month Expiry:"));
panel.add(field3);
panel.add(new JLabel("Year Expiry:"));
panel.add(field4);
panel.add(new JLabel("CVC:"));
panel.add(field5);
int input = JOptionPane.showConfirmDialog(MainActivity.this, panel, "Card Payment",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (input == JOptionPane.OK_OPTION) {
Basket.orderNo+=1;
dispose();
new OrderConfirmationScreen().setVisible(true);
} else {
System.out.println("Payment Cancelled");
}
}
How can i add validation so that the fields are checked to see if correct data was entered for a card payment. For example, field1 should only allow text, field2 should only allow a 16 digit number and so on.
I'm guessing i would need to create more methods that validate each field and then just call the method in cardPopUp() method.
For example :
field1 should only allow text
if(field1.getText().matches("\\w+\\.?")){
...
}else{...}
field2 should only allow a 16 digit number
if(field2.getText().matches("(\\d{16})")){
...
}else{...}
And so on.
as i understand your question what u want is to match the character types that are inputted, for this i would suggest to take a look at the String.matches see javaDoc using this you can setup a function that checks the input for any regrex epression and returns true or false, if this is NOT what you wanted i'm afraid i don't understand the question
Hi I am trying to retrieve data from the database and display in the jtable but I am unable to do it. The code is given below. Can anyone please help me.
//Dialog Creation for Finding Data
jdlgFind=new JDialog(frame,"FIND DATA",true);
jdlgFind.setFont(new Font("Times New Roman", Font.PLAIN, 12));
jdlgFind.setSize(650,500);
findpanel=new JPanel();
findpanel.setBackground(new Color(144, 238, 144));
jdlgFind.setContentPane(findpanel);
findpanel.setLayout(new FlowLayout());
findpanel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
txtGetMobileno=new JTextField(15);
txtGetLandlineno=new JTextField(15);
txtGetPlaceofBirth=new JTextField(15);
JTable jtblFindDate=new JTable(tbldata,columnhead);
jspFindPanel=new JScrollPane(jtblFindDate);
jtblFindDate.setRowSelectionAllowed(true);
jtblFindDate.setFont(new Font("Times New Roman", Font.PLAIN, 11));
jtblFindDate.setPreferredSize(new Dimension(600, 500));
findpanel.add(new JLabel("Mobile No. please : ")).setFont(new Font("Times New Roman", Font.BOLD, 12));
findpanel.add(txtGetMobileno).setFont(new Font("Times New Roman", Font.PLAIN, 12));
findpanel.add(new JLabel("Landline No. please : ")).setFont(new Font("Times New Roman", Font.BOLD, 12));
findpanel.add(txtGetLandlineno).setFont(new Font("Times New Roman", Font.PLAIN, 12));
findpanel.add(new JLabel("Place Of Birth please : ")).setFont(new Font("Times New Roman", Font.BOLD, 12));
findpanel.add(txtGetPlaceofBirth).setFont(new Font("Times New Roman", Font.PLAIN, 12));
findpanel.add(jspFindPanel);
txtGetMobileno.setDocument(new TextFieldLimit(11));
txtGetMobileno.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
findMobileno=null;
findMobileno=txtGetMobileno.getText();
txtGetLandlineno.setEditable(false);
txtGetPlaceofBirth.setEditable(false);
if(findMobileno.length()!=0)
{
try
{
dbcon=new DB_Connection();
PreparedStatement pstmt=dbcon.DB_Connection("//F://eclipse_Luna_64_Development_Workspace//Project JAVA//LIC_AGENCY_TRACKER//DATABASE//LIC_DATA_TRACKER.accdb").prepareStatement("select Inquiry_Master_Data.Inquiry_Master_Name as [Inquirer Name],Inquiry_Master_Data.Inquiry_Master_Mobile_Number as [Inquirer Mobile No],Inquiry_Master_Data.Inquiry_Master_Landline_Number as [Inquirer Landline No],"
+ "Inquiry_Master_Data.Inquiry_Master_Place_Of_Work as [Inquirer Work Place] from Inquiry_Master_Data where Inquiry_Master_Data.Inquiry_Master_Mobile_Number='"+findMobileno+"'");
ResultSet rset=pstmt.executeQuery();
ResultSetMetaData rsetmetadata=rset.getMetaData();
int col=rsetmetadata.getColumnCount();
columnhead=new Vector(col);
columnhead.add("Inquirer Name");
columnhead.add("Inquirer Mobile No");
columnhead.add("Inquirer Landline No");
columnhead.add("Inquirer Work Place");
tbldata=new Vector();
row=new Vector();
while(rset.next())
{
row=new Vector(col);
for(int i=1;i<=col;i++)
{
row.add(rset.getString(i));
}
tbldata.add(row);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Data not found.");
txtGetMobileno.setText("");
txtGetLandlineno.setEditable(true);
txtGetPlaceofBirth.setEditable(true);
txtGetMobileno.requestFocus();
}
}
else
{
txtGetLandlineno.requestFocus();
}
}
});
I am using MS Access. I have used Prepared Statement to retrieve the data but it is not displaying the data.
Regards
Sandeep
JTable jtblFindDate=new JTable(tbldata,columnhead);
What is that statement supposed to do? Do you actually have valid data in both of those variables? If not then the table will be created with no rows or columns.
columnhead=new Vector(col);
columnhead.add("Inquirer Name");
columnhead.add("Inquirer Mobile No");
columnhead.add("Inquirer Landline No");
columnhead.add("Inquirer Work Place");
That code doesn't do anything because you don't then use the columnHead to create a new TableModel.
tbldata.add(row);
That may add data to the Vector, but again you don't use the Vector to create a TableModel
Check out the TableFromDatabaseExample code found in Table From Database for generic code to load a JTable from a database.
Hi I have solved the issue. I have changed the code. Below is the modification part, I have used the following codes: -
dbcon=new DB_Connection();
PreparedStatement pstmt=dbcon.DB_Connection("//F://eclipse_Luna_64_Development_Workspace//Project JAVA//LIC_AGENCY_TRACKER//DATABASE//LIC_DATA_TRACKER.accdb").prepareStatement("select Inquiry_Master_Data.Inquiry_Master_Name as [Inquirer Name],Inquiry_Master_Data.Inquiry_Master_Mobile_Number as [Inquirer Mobile No],Inquiry_Master_Data.Inquiry_Master_Landline_Number as [Inquirer Landline No],"
+ "Inquiry_Master_Data.Inquiry_Master_Place_Of_Work as [Inquirer Work Place] from Inquiry_Master_Data where Inquiry_Master_Data.Inquiry_Master_Place_Of_Work='"+findPlaceofBirth+"'");
ResultSet rset=pstmt.executeQuery();
jtblFindData.setModel(DbUtils.resultSetToTableModel(rset));
For this I have used rs2xml.jar file.
Regards
Sandeep