Related
I am creating a program where I can add and delete items. When added, I want the TOTAL panel to get values from itemprice_textfield when the button ADD is clicked.
private JFrame frame;
private JTextField itemname_textfield;
private JTextField itemprice_textfield;
private JButton add_button;
private JComboBox comboBox;
private JButton del_button;
private JLabel itemprice_label;
private JLabel itemname_label;
private JLabel lblNewLabel_3;
private JLabel totalamount_label;
private JTextField textField_2;
private double amount;
private JLabel bigger_itemname_label;
private JLabel bigger_itemprice_label;
private ArrayList <String> itemnames = new ArrayList();
private ArrayList <Double> itemprices = new ArrayList();
private JTable table;
//DefaultTableModel dtm;
//String header [] = new String [] {itemname_textfield, itemprice_textfield};
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EntapaCCE103LabExam1_Frame1 window = new EntapaCCE103LabExam1_Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class lists{ // user defined method lists class
public String itemname;
public double price;
public lists(String itemname, double price)
{
this.itemname = itemname;
this.price = price;
}
public lists(JTextField textField) {
// TODO Auto-generated constructor stub
}
public lists(JTextField textField, JTextField textField_1) {
// TODO Auto-generated constructor stub
}
} // end
public EntapaCCE103LabExam1_Frame1() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(null);
JLabel big_title = new JLabel("MY ORDERS");
big_title.setFont(new Font("Times New Roman", Font.PLAIN, 18));
big_title.setBounds(245, 11, 116, 52);
frame.getContentPane().add(big_title);
itemname_textfield = new JTextField(10); // name text
itemname_textfield.setBounds(412, 268, 109, 20);
frame.getContentPane().add(itemname_textfield);
itemname_textfield.setColumns(10);
itemprice_textfield = new JTextField(); // price text
itemprice_textfield.setBounds(412, 330, 109, 20);
frame.getContentPane().add(itemprice_textfield);
itemprice_textfield.setColumns(10);
add_button = new JButton("ADD ITEM");
add_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String getname = itemname_textfield.getText(); // used to get the input string from user to transfer to the combo box
comboBox.addItem(getname); // used to display the received string inputs
}
});
add_button.setFont(new Font("Serif", Font.PLAIN, 13));
add_button.setBounds(412, 361, 109, 23);
frame.getContentPane().add(add_button);
comboBox = new JComboBox();
comboBox.setBounds(412, 479, 109, 22);
frame.getContentPane().add(comboBox);
del_button = new JButton("DELETE ITEM");
del_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboBox.removeItem(comboBox.getSelectedItem()); // used to delete the selected index in combo box.
}
});
del_button.setFont(new Font("Serif", Font.PLAIN, 11));
del_button.setBounds(412, 512, 109, 23);
frame.getContentPane().add(del_button);
itemprice_label = new JLabel("Enter Item Price:"); // label for item price
itemprice_label.setBounds(412, 311, 109, 14);
frame.getContentPane().add(itemprice_label); // used to display the text from label
itemname_label = new JLabel("Enter Item Name:"); // label for item name
itemname_label.setBounds(412, 249, 102, 14);
frame.getContentPane().add(itemname_label); // used to display the text from label
lblNewLabel_3 = new JLabel("OPERATION");
lblNewLabel_3.setFont(new Font("Sitka Subheading", Font.PLAIN, 17));
lblNewLabel_3.setBounds(419, 185, 102, 32);
frame.getContentPane().add(lblNewLabel_3);
String getname = itemname_textfield.getText();
totalamount_label = new JLabel("TOTAL: " + amount); // amount here
totalamount_label.setFont(new Font("Serif", Font.PLAIN, 13));
totalamount_label.setBounds(412, 130, 136, 20);
frame.getContentPane().add(totalamount_label);
bigger_itemname_label = new JLabel("ITEM NAME");
bigger_itemname_label.setFont(new Font("Serif", Font.PLAIN, 14));
bigger_itemname_label.setBounds(84, 190, 96, 14);
frame.getContentPane().add(bigger_itemname_label);
bigger_itemprice_label = new JLabel("PRICE");
bigger_itemprice_label.setFont(new Font("Serif", Font.PLAIN, 14));
bigger_itemprice_label.setBounds(245, 192, 46, 14);
frame.getContentPane().add(bigger_itemprice_label);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(39, 225, 322, 285);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
table.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
}
}
But I don't know how or what to do since I am new to Java GUI.
Also, please tell me if there's something weird in my code so that I can make improvement.
Im trying to insert the Jcombobox value into my sqlite database but whenever I run the program, it only inserts '9' in to my database. Please advise. I am trying to import the integer that the user picks into my sqlite database. For some reason, even with the action listener, the default value, 9, is still being inputed into the table and im not sure why. Here is the full code, not including the connection and the Jtable.
public class create extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
create frame = new create();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection connect = null;
private JTextField textField_2;
String uniqueString = UUID.randomUUID().toString();
private JTextField textField_3;
/**
* Create the frame.
*/
public create() {
connect = connection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(new Color(204, 204, 204));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblStudentName = new JLabel("Student ID:");
lblStudentName.setBounds(96, 69, 72, 16);
contentPane.add(lblStudentName);
textField = new JTextField();
textField.setBounds(173, 64, 216, 26);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblGrade = new JLabel("Grade:");
lblGrade.setBounds(125, 107, 47, 16);
contentPane.add(lblGrade);
JComboBox comboBox = new JComboBox();
comboBox.addItem(9);
comboBox.addItem(10);
comboBox.addItem(11);
comboBox.addItem(12);
comboBox.setBounds(173, 102, 72, 29);
contentPane.add(comboBox);
comboBox.setSelectedItem(9);
comboBox.addActionListener(comboBox);
int selectedNumber = (int)comboBox.getSelectedItem();
JLabel lblInputBookOver = new JLabel("Teacher:");
lblInputBookOver.setBounds(111, 146, 61, 21);
contentPane.add(lblInputBookOver);
textField_1 = new JTextField();
textField_1.setBounds(173, 143, 216, 26);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton button = new JButton("<");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int count = 0; count <= 0; count++) {
//dispose();
options sc = new options();
sc.setVisible(true);
}
}
});
button.setBounds(6, 6, 30, 29);
contentPane.add(button);
JLabel lblStudentname = new JLabel("Student Name:");
lblStudentname.setBounds(74, 31, 99, 16);
contentPane.add(lblStudentname);
textField_2 = new JTextField();
textField_2.setBounds(173, 26, 216, 26);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(125, 180, 42, 26);
contentPane.add(lblEmail);
textField_3 = new JTextField();
textField_3.setBounds(173, 180, 216, 26);
contentPane.add(textField_3);
textField_3.setColumns(10);
JButton btnCheckout = new JButton("Checkout");
btnCheckout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String uniqueString = UUID.randomUUID().toString().replace("-", "");
try {
String query = "insert into data ('Name', 'Student ID', 'Teacher', 'Grade', 'Email', 'Ebook') values (?,?,?,?,?,?)";
PreparedStatement pst = connect.prepareStatement(query);
pst.setString(1,textField_2.getText() );
pst.setString(2,textField.getText() );
pst.setString(3,textField_1.getText() );
pst.setInt(4, selectedNumber);
pst.setString(5,textField_3.getText() );
pst.setString(6, uniqueString);
for (int count = 0; count <= 0; count++) {
//dispose();
confirm sc = new confirm();
sc.setVisible(true);
count = 0;
}
pst.execute();
pst.close();
}
catch (Exception w){
w.printStackTrace();
}
}
});
btnCheckout.setBounds(173, 218, 117, 29);
contentPane.add(btnCheckout);
}
}
It inserts 9 to your database because the selected item is always 9, and this because you add it first to your combobox. In order to insert the selected value, you will have to use an ActionListener. Then you can take user's selection and do whatever you want.
An example of its usage:
import java.awt.FlowLayout;
import java.io.FileNotFoundException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class ComboBox extends JFrame {
public ComboBox() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
getContentPane().setLayout(new FlowLayout());
Integer[] numbers = { 4, 5, 8, 123, 42, 634 };
JComboBox<Integer> comboBox = new JComboBox<>(numbers);
comboBox.setSelectedItem(42); // The initial selection is 42.
comboBox.addActionListener(e -> {
int selectedNumber = (int) comboBox.getSelectedItem();
System.out.println("Selected number: " + selectedNumber);
// Do whatever with selected number
});
add(comboBox);
}
public static void main(String[] args) throws FileNotFoundException {
SwingUtilities.invokeLater(() -> new ComboBox().setVisible(true));
}
}
I have developed the calc using swing but i have a problem in equal to button.
I have written the code for addition,subtraction,multiplication and division in the equal to button but the problem is that when i subtract two numbers I get the o/p but when I add or multiply or divide I don't get the o/p. So help me in
the code of equal to button:
package com.dss;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import javax.swing.SwingConstants;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
public class JCalcEx extends JFrame {
private JPanel contentPane;
private JTextField value1;
private JTextField value2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JCalcEx frame = new JCalcEx();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JCalcEx() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 395, 680);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
value1 = new JTextField();
value1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value1.setHorizontalAlignment(SwingConstants.RIGHT);
value1.setFont(new Font("Tahoma", Font.PLAIN, 20));
value1.setBounds(12, 69, 350, 41);
contentPane.add(value1);
value1.setColumns(10);
JTextField value3 = new JTextField();
value3.setHorizontalAlignment(SwingConstants.RIGHT);
value3.setFont(new Font("Tahoma", Font.PLAIN, 20));
value3.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value3.setBounds(12, 111, 353, 41);
contentPane.add(value3);
value3.setColumns(10);
JButton button9 = new JButton("9");
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+9);
}
});
button9.setFont(new Font("Tahoma", Font.PLAIN, 20));
button9.setBounds(12, 192, 69, 70);
contentPane.add(button9);
JButton button8 = new JButton("8");
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0){
value1.setText(value1.getText()+8);
}
});
button8.setFont(new Font("Tahoma", Font.PLAIN, 20));
button8.setBounds(105, 192, 69, 70);
contentPane.add(button8);
JButton button7 = new JButton("7");
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+7);
}
});
button7.setFont(new Font("Tahoma", Font.PLAIN, 20));
button7.setBounds(199, 192, 69, 70);
contentPane.add(button7);
JButton button6 = new JButton("6");
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+6);
}
});
button6.setFont(new Font("Tahoma", Font.PLAIN, 20));
button6.setBounds(293, 192, 69, 70);
contentPane.add(button6);
JButton button5 = new JButton("5");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+5);
}
});
button5.setFont(new Font("Tahoma", Font.PLAIN, 20));
button5.setBounds(12, 293, 69, 70);
contentPane.add(button5);
JButton button4 = new JButton("4");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+4);
}
});
button4.setFont(new Font("Tahoma", Font.PLAIN, 20));
button4.setBounds(105, 293, 69, 70);
contentPane.add(button4);
JButton button3 = new JButton("3");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+3);
}
});
button3.setFont(new Font("Tahoma", Font.PLAIN, 20));
button3.setBounds(199, 293, 69, 70);
contentPane.add(button3);
JButton button2 = new JButton("2");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+2);
}
});
button2.setFont(new Font("Tahoma", Font.PLAIN, 20));
button2.setBounds(293, 293, 69, 70);
contentPane.add(button2);
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+1);
}
});
button1.setFont(new Font("Tahoma", Font.PLAIN, 20));
button1.setBounds(12, 392, 69, 70);
contentPane.add(button1);
JButton buttonc = new JButton("C");
buttonc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText("");
value1.setText("");
actionRecieved.setText("");
value3.setText("");
}
});
buttonc.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonc.setBounds(293, 392, 69, 70);
contentPane.add(buttonc);
JButton button0 = new JButton("0");
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+0);
}
});
button0.setFont(new Font("Tahoma", Font.PLAIN, 20));
button0.setBounds(105, 392, 69, 70);
contentPane.add(button0);
JButton buttoneql = new JButton("=");
buttoneql.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int one = Integer.parseInt(value2.getText());
int two = Integer.parseInt(value1.getText());
if(actionRecieved.getText().equals("sub"))
{
int sub = one - two;
value3.setText(String.valueOf(sub));
}
if(actionRecieved.getText().equals("add"))
{
int add = one + two;
value3.setText(String.valueOf(add));
}
if(actionRecieved.getText().equals("mul"))
{
int mul = one * two;
value1.setText(String.valueOf(mul));
}
if(actionRecieved.getText().equals("div"))
{
int div = one / two;
value1.setText(String.valueOf(div));
}
}
});
buttoneql.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttoneql.setBounds(199, 392, 69, 70);
contentPane.add(buttoneql);
JButton buttonplus = new JButton("+");
buttonplus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("plus");
value1.setText(null);
}
});
buttonplus.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonplus.setBounds(12, 489, 69, 70);
contentPane.add(buttonplus);
JButton buttonminus = new JButton("-");
buttonminus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("sub");
value1.setText(null);
}
});
buttonminus.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonminus.setBounds(105, 489, 69, 70);
contentPane.add(buttonminus);
JButton buttonmultiply = new JButton("x");
buttonmultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("mul");
value1.setText(null);
}
});
buttonmultiply.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonmultiply.setBounds(199, 489, 69, 70);
contentPane.add(buttonmultiply);
JButton buttondivide = new JButton("/");
buttondivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("div");
value1.setText(null);
}
});
buttondivide.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttondivide.setBounds(293, 489, 69, 70);
contentPane.add(buttondivide);
value2 = new JTextField();
value2.setFont(new Font("Tahoma", Font.PLAIN, 20));
value2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value2.setHorizontalAlignment(SwingConstants.RIGHT);
value2.setBounds(229, 29, 133, 41);
contentPane.add(value2);
value2.setColumns(10);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setBounds(12, 29, 350, 81);
contentPane.add(editorPane);
actionRecieved = new JLabel("");
actionRecieved.setBounds(12, 572, 118, 48);
contentPane.add(actionRecieved);
}
private JLabel actionRecieved;
}
You have the following problems
For div int div = one - two; - should be /
For 1. you will have problem with Integer division
Add will never be entered into because you do actionRecieved.setText("plus"); but expect add
Not sure why in some cases you are setting value1 and in others value3
???
I have written some code when one of the option in radio button is clicked
it should display one jlabel and jtext field. And when other option in the radio button is clicked it should hide the previous shown jlabel and jtext field and display new jlabel and jtext field.
In the output when I click on one of the radio button it is displaying nothing unless and until I maximize my Window. After geting my jlabel and jtextfield If I click on other radio button the jlabel and jtextfield is hidden but Im not able to see new jlabel and jtextfield for that radiobutton.
enter code here
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group =new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
jrdbuttonFullTime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jrdbuttonFullTime.isSelected()){
//jrdbtnContract.setSelected(false);
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
jlblEmpHour.setVisible(false);
jtxtHour_Pay.setVisible(false);
jtxtHour_Worked.setVisible(false);
jlblEmpWork.setVisible(false);
}
}
});
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
jrdbtnContract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jrdbtnContract.isSelected()){
//jrdbuttonFullTime.setSelected(false);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
jlblEmpSal.setVisible(false);
jtxtSal.setVisible(false);
}
}
});
jrdbtnContract.setBounds(218, 91, 109, 23);
panel_1.add(jrdbtnContract);
}
}
Insteed of adding and removing components, simply add all and hide/show them on radiobox selection like this:
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
ActionListener myAction = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jlblEmpHour.setVisible(jrdbtnContract.isSelected());
jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
jlblEmpWork.setVisible(jrdbtnContract.isSelected());
jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
jtxtSal.setVisible(jrdbuttonFullTime.isSelected());
}
};
myAction.actionPerformed(null); // to initialize labels first
jrdbuttonFullTime.addActionListener(myAction); // add actionlisteners
jrdbtnContract.addActionListener(myAction);// add actionlisteners
As you can see, you dont even need 2 separate action listeners as one but shared instance is just enough.
So the complete app will look like this:
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group = new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
ActionListener myAction = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jlblEmpHour.setVisible(jrdbtnContract.isSelected());
jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
jlblEmpWork.setVisible(jrdbtnContract.isSelected());
jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
jtxtSal.setVisible(jrdbuttonFullTime.isSelected());
}
};
myAction.actionPerformed(null); // to initialize labels first
jrdbuttonFullTime.addActionListener(myAction);
jrdbtnContract.addActionListener(myAction);
jrdbtnContract.setBounds(218, 91, 109, 23);
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
panel_1.add(jrdbtnContract);
}
}
I've revised your code a little. This should get you going on the right path:
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group = new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
//*******************************************************************
// Add all your salary fields here, not in ActionListeners
// Start them off invisible
//*******************************************************************
jlblEmpSal.setVisible(false);
panel_1.add(jlblEmpSal);
jtxtSal.setVisible(false);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
jlblEmpHour.setVisible(false);
panel_1.add(jtxtHour_Pay);
jtxtHour_Pay.setVisible(false);
panel_1.add(jlblEmpWork);
jlblEmpWork.setVisible(false);
jtxtHour_Worked.setVisible(false);
panel_1.add(jtxtHour_Worked);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
jrdbuttonFullTime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (jrdbuttonFullTime.isSelected()) {
//jrdbtnContract.setSelected(false);
// ****************************************************
// In ActionListeners for radiobuttons, hide the fields you
// don't want to see, make visible the ones you do want to see
// ****************************************************
jlblEmpSal.setVisible(true);
jtxtSal.setVisible(true);
jlblEmpHour.setVisible(false);
jtxtHour_Pay.setVisible(false);
jtxtHour_Worked.setVisible(false);
jlblEmpWork.setVisible(false);
}
}
});
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
jrdbtnContract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (jrdbtnContract.isSelected()) {
//jrdbuttonFullTime.setSelected(false);
// ****************************************************
// In ActionListeners for radiobuttons, hide the fields you
// don't want to see, make visible the ones you do want to see
// ****************************************************
jlblEmpHour.setVisible(true);
jtxtHour_Pay.setVisible(true);
jlblEmpWork.setVisible(true);
jtxtHour_Worked.setVisible(true);
jlblEmpSal.setVisible(false);
jtxtSal.setVisible(false);
}
}
});
jrdbtnContract.setBounds(218, 91, 109, 23);
panel_1.add(jrdbtnContract);
}
}
Your radio buttons start off both unchecked, so you see no salary detail initially. When you click one or the other, the corresponding details appear.
In which case(which radiobutton checked ?) you want to show which controls?
i have two jframes in my app, Addnmember.java and Frame1.java
Frame1 is the main Jframe,
i want when someone presses the Add button, the Addmember will be shown, i have done this with this code, and every thing is fine:
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
But in the new Jframe, i want to come back again when Done Bt is selected, i have done this whit this code:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
but this doesent work! mean the Jframe Loads but there is no content in it! look:
why this happens? here is my codes:
AddMember.java:
public class AddMember extends JFrame {
private JFrame frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JButton btnNewButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddMember frame = new AddMember();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddMember() {
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 331);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u0646\u0627\u0645:");
label.setFont(new Font("Tahoma", Font.BOLD, 11));
label.setBounds(332, 60, 46, 14);
contentPane.add(label);
textField = new JTextField();
textField.setToolTipText("\u0646\u0627\u0645");
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
textField.setText("");
}
});
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setText("\u0646\u0627\u0645");
textField.setBounds(236, 57, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u0634.\u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC:");
label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
label_1.setBounds(328, 103, 85, 14);
contentPane.add(label_1);
textField_1 = new JTextField();
textField_1.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_1.setText("");
}
});
textField_1.setText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
textField_1.setColumns(10);
textField_1.setBounds(236, 100, 86, 20);
contentPane.add(textField_1);
JLabel label_2 = new JLabel("\u0631\u0634\u062A\u0647:");
label_2.setFont(new Font("Tahoma", Font.BOLD, 11));
label_2.setBounds(332, 145, 46, 14);
contentPane.add(label_2);
textField_2 = new JTextField();
textField_2.setToolTipText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_2.setText("");
}
});
textField_2.setText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.setHorizontalAlignment(SwingConstants.RIGHT);
textField_2.setColumns(10);
textField_2.setBounds(236, 142, 86, 20);
contentPane.add(textField_2);
JLabel label_3 = new JLabel("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC:");
label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
label_3.setBounds(144, 61, 82, 14);
contentPane.add(label_3);
textField_3 = new JTextField();
textField_3.setToolTipText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_3.setText("");
}
});
textField_3.setText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.setHorizontalAlignment(SwingConstants.RIGHT);
textField_3.setColumns(10);
textField_3.setBounds(48, 58, 86, 20);
contentPane.add(textField_3);
JLabel label_4 = new JLabel("\u0634. \u062A\u0645\u0627\u0633:");
label_4.setFont(new Font("Tahoma", Font.BOLD, 11));
label_4.setBounds(144, 104, 69, 14);
contentPane.add(label_4);
textField_4 = new JTextField();
textField_4.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_4.setText("");
}
});
textField_4.setText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.setHorizontalAlignment(SwingConstants.RIGHT);
textField_4.setColumns(10);
textField_4.setBounds(48, 101, 86, 20);
contentPane.add(textField_4);
textField_5 = new JTextField();
textField_5.setToolTipText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_5.setText("");
}
});
textField_5.setText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.setHorizontalAlignment(SwingConstants.RIGHT);
textField_5.setColumns(10);
textField_5.setBounds(48, 140, 86, 20);
contentPane.add(textField_5);
JLabel label_5 = new JLabel("\u0648\u0631\u0648\u062F\u06CC:");
label_5.setFont(new Font("Tahoma", Font.BOLD, 11));
label_5.setBounds(144, 143, 46, 14);
contentPane.add(label_5);
textField_6 = new JTextField();
textField_6.setToolTipText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_6.setText("");
}
});
textField_6.setHorizontalAlignment(SwingConstants.RIGHT);
textField_6.setText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.setColumns(10);
textField_6.setBounds(131, 187, 86, 20);
contentPane.add(textField_6);
JLabel label_6 = new JLabel("\u0627\u06CC\u0645\u06CC\u0644:");
label_6.setFont(new Font("Tahoma", Font.BOLD, 11));
label_6.setBounds(227, 190, 46, 14);
contentPane.add(label_6);
btnNewButton = new JButton("Done");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(171, 227, 89, 23);
contentPane.add(btnNewButton);
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
And Frame1.java:
public class Frame1 extends JFrame {
Connection connection = null;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
private JTable table;
public Frame1() {
JOptionPane.showMessageDialog(null, "sazande :|");
this.initialize();
connection = DataBConect.dbConnect();
try {
String query = "select * from Users";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
JOptionPane.showMessageDialog(null, "intialize :|");
frame.setTitle("App");
frame.setBounds(100, 100, 725, 465);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton AddB = new JButton("Add");
AddB.setBackground(Color.WHITE);
AddB.setFont(new Font("Tahoma", Font.BOLD, 11));
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
AddB.setBounds(302, 392, 135, 23);
frame.getContentPane().add(AddB);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 26, 658, 359);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
}
}
thanks.
As to why your second Frame1 shows no data, I'm not 100% sure, but perhaps it has something to do with your failure to close your database connection. Regardless, it seems wasteful and even potentially dangerous to want to gather the database data twice when you've already obtained it once, and to create your Frame1 GUI twice when you've already created it once.
You should re-design your GUI. The main JFrame GUI should remain visible, and the Frame1 window should be a modal JDialog. As a modal dialog, it will prevent users from being able to interact with the parent JFrame until the dialog is no longer visible.
Note that to get started requires only a few changes:
in Frame1.java
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// frame.dispose();
AddMember addmem = new AddMember(Frame1.this); // !!
addmem.setVisible(true);
}
});
in AddMember.java
class AddMember extends JDialog {
public AddMember(JFrame frame) {
super(frame, "", ModalityType.APPLICATION_MODAL);
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
// !! setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// dispose();
dispose(); // !!
// !! Frame1 addmem = new Frame1();
// addmem.setVisible(true);
}
});
Also, you'll want to avoid null layouts and setBounds(...) as this will lead to very rigid, hard to debug and improve programs.