Related
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 am trying to update the text of a Jlabel from another class. I tried using the setText method, which results in the program compiling fine, and running, however when I press the button nothing happens.
Here is the code from my Menu.class, where the Jlabel resides.
public class Menu extends JFrame {
private JPanel contentPane;
private JLabel lblOthello;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
Menu frame = new Menu();
frame.setVisible(true);
}
});
}
public Menu() {
//Sets program icon
ImageIcon img = new ImageIcon("icon.png");
//Creates the frame
setForeground(Color.RED);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 384, 475);
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblOthello = new JLabel("Othello");
lblOthello.setForeground(Color.YELLOW);
lblOthello.setFont(new Font("Franklin Gothic Medium Cond", Font.BOLD, 33));
lblOthello.setHorizontalAlignment(SwingConstants.CENTER);
lblOthello.setBounds(0, 0, 366, 59);
contentPane.add(lblOthello);
JButton btnNewButton = new JButton("New Singleplayer Game");
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setBackground(UIManager.getColor("Button.background"));
btnNewButton.setBounds(10, 72, 344, 45);
btnNewButton.addActionListener(new ButtonListener());
contentPane.add(btnNewButton);
JButton btnNewMultiplayerGame = new JButton("New Dualplayer Game");
btnNewMultiplayerGame.setBounds(10, 130, 344, 45);
btnNewMultiplayerGame.addActionListener(new ButtonListener());
contentPane.add(btnNewMultiplayerGame);
JSeparator separator = new JSeparator();
separator.setBounds(0, 57, 366, 2);
contentPane.add(separator);
JButton btnNewNetworkGame = new JButton("New Network Game");
btnNewNetworkGame.setBounds(10, 188, 344, 45);
btnNewNetworkGame.addActionListener(new ButtonListener());
contentPane.add(btnNewNetworkGame);
JButton btnAbout = new JButton("AI vs AI Game");
btnAbout.setBounds(10, 246, 344, 45);
btnAbout.addActionListener(new ButtonListener());
contentPane.add(btnAbout);
JButton btnNewButton_1 = new JButton("QUIT");
btnNewButton_1.setBounds(10, 446, 344, 25);
btnNewButton_1.addActionListener(new ButtonListener());
contentPane.add(btnNewButton_1);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\User\\workspace\\Othello\\src\\Othello.jpg"));
lblNewLabel.setBounds(0, -36, 366, 507);
contentPane.add(lblNewLabel);
JButton button = new JButton("QUIT");
button.setBounds(10, 390, 344, 25);
button.addActionListener(new ButtonListener());
contentPane.add(button);
JSeparator separator_1 = new JSeparator();
separator_1.setBounds(0, 375, 366, 2);
contentPane.add(separator_1);
}
public void setText(String text) {
lblOthello.setText(text);
System.out.println("executed");
contentPane.validate();
contentPane.repaint();
}
}
And here is the part of the code that executes the settext method from my buttonlistener class
case PLAYER_VS_AI:
Menu men = new Menu();
men.setText("Now");
men.revalidate();
men.repaint();
break;
Can anyone tell me why this doesn't work?
You are creating a new Menu object whenever the button is pressed instead of modifying the already existing Menu object.
I'm trying to call a method that opens a new existing frame, but all I get is an empty frame without content.
This is the code that calls the function when a button is preesed:
JButton btnPersonalInfo = new JButton("Personal Info");
btnPersonalInfo.setBounds(10, 5, 120, 23);
btnPersonalInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame PersonalInfo = new JFrame("Personal Info");
PersonalInfo content = new PersonalInfo();
PersonalInfo.setContentPane(content);
PersonalInfo.setSize(700,700);
PersonalInfo.setLocation(100,15);
PersonalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
PersonalInfo.setResizable(false);
PersonalInfo.setVisible(true);
}
});
panel_1.add(btnPersonalInfo);
This is how I initialize the PersonalInfo function:
public PersonalInfo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblPersonalInfo = new JLabel("Personal Information");
lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
lblPersonalInfo.setBounds(110, 11, 185, 14);
panel.add(lblPersonalInfo);
JLabel lblFullName = new JLabel("Full Name");
lblFullName.setBounds(10, 36, 58, 14);
panel.add(lblFullName);
JLabel lblNationality = new JLabel("Nationality");
lblNationality.setBounds(10, 61, 78, 14);
panel.add(lblNationality);
JLabel lblDateBirth = new JLabel("Date of Birth");
lblDateBirth.setBounds(10, 86, 78, 14);
panel.add(lblDateBirth);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(10, 111, 46, 14);
panel.add(lblGender);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(10, 164, 58, 14);
panel.add(lblAddress);
JLabel lblMobile = new JLabel("Mobile");
lblMobile.setBounds(10, 189, 46, 14);
panel.add(lblMobile);
JLabel lblEmail = new JLabel("E-mail");
lblEmail.setBounds(10, 214, 46, 14);
panel.add(lblEmail);
JRadioButton rdbtnM_2 = new JRadioButton("M");
rdbtnM_2.setBounds(74, 133, 109, 23);
panel.add(rdbtnM_2);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setBounds(74, 107, 109, 23);
panel.add(rdbtnF);
}
}
Basically I'm expecting to view the frame from the PersonalInfo method when I press the btnPersonalInfo button and right now I only get an empty frame.
Thank you and sorry if this is a duplicate, but it's my first question here.
Remove frame = new JFrame(); from your initialize()
Simply:
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);//It will be added to the ContentPane by default.
panel.setLayout(null);
...
Then when calling, remove the following block:
JFrame PersonalInfo = new JFrame("Personal Info");
PersonalInfo content = new PersonalInfo();
PersonalInfo.setContentPane(content);
Replace it by:
PersonalInfo personalInfo = new PersonalInfo();
personalInfo.setSize(700,700);
personalInfo.setLocation(100,15);
....
So this should be your method call:
public void actionPerformed(ActionEvent e) {
PersonalInfo personalInfo = new PersonalInfo();
personalInfo.setSize(700,700);
personalInfo.setLocation(100,15);
personalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
personalInfo.setResizable(false);
personalInfo.setVisible(true);
}
And this should be your Class:
public class PersonalInfo extends JFrame
{
public PersonalInfo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblPersonalInfo = new JLabel("Personal Information");
lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
lblPersonalInfo.setBounds(110, 11, 185, 14);
panel.add(lblPersonalInfo);
JLabel lblFullName = new JLabel("Full Name");
lblFullName.setBounds(10, 36, 58, 14);
panel.add(lblFullName);
JLabel lblNationality = new JLabel("Nationality");
lblNationality.setBounds(10, 61, 78, 14);
panel.add(lblNationality);
JLabel lblDateBirth = new JLabel("Date of Birth");
lblDateBirth.setBounds(10, 86, 78, 14);
panel.add(lblDateBirth);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(10, 111, 46, 14);
panel.add(lblGender);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(10, 164, 58, 14);
panel.add(lblAddress);
JLabel lblMobile = new JLabel("Mobile");
lblMobile.setBounds(10, 189, 46, 14);
panel.add(lblMobile);
JLabel lblEmail = new JLabel("E-mail");
lblEmail.setBounds(10, 214, 46, 14);
panel.add(lblEmail);
JRadioButton rdbtnM_2 = new JRadioButton("M");
rdbtnM_2.setBounds(74, 133, 109, 23);
panel.add(rdbtnM_2);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setBounds(74, 107, 109, 23);
panel.add(rdbtnF);
}
}
I have a question. Im using WindowBuilder in Eclipse and when i type in the code,my action listener doesnt work. I tried everything.
Also,another question,when using WindowBuilder,what is the name of my frame object? I see there thtat it is made in my class,but it doesnt have name.
public OrdinacijaGui()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(600 , 300 , 450 , 300);
setTitle("Dr Idrizovic");
getContentPane().setLayout(null);
JButton regUslugaButt = new JButton("Registar usluga");
regUslugaButt.setBounds(10, 11, 150, 30);
getContentPane().add(regUslugaButt);
JButton regMaterijalaButt = new JButton("Registar materijala");
regMaterijalaButt.setBounds(10, 71, 150, 30);
getContentPane().add(regMaterijalaButt);
JButton regIntervencijaButt = new JButton("Registar intervencija");
regIntervencijaButt.setBounds(10, 131, 150, 30);
getContentPane().add(regIntervencijaButt);
JButton regDijagnozaButt = new JButton("Registar dijagnoza");
regDijagnozaButt.setBounds(10, 191, 150, 30);
getContentPane().add(regDijagnozaButt);
JButton exitButt = new JButton("Zavrsetak rada");
exitButt.setBounds(143, 232, 150, 30);
getContentPane().add(exitButt);
JButton evidencijaPacButt = new JButton("Evidencija pacijenata");
evidencijaPacButt.setBounds(230, 11, 200, 30);
getContentPane().add(evidencijaPacButt);
JButton zakazivanjePacButt = new JButton("Zakazivanje pacijenata");
zakazivanjePacButt.setBounds(230, 71, 200, 30);
getContentPane().add(zakazivanjePacButt);
JButton evidencijaStomatologaButt = new JButton("Evidencija stomatologa");
evidencijaStomatologaButt.setBounds(230, 131, 200, 30);
getContentPane().add(evidencijaStomatologaButt);
JButton izvrseneUslugeButt = new JButton("Izvrsene usluge");
izvrseneUslugeButt.setBounds(230, 191, 200, 30);
getContentPane().add(izvrseneUslugeButt);
thehandler handler = new thehandler();
regUslugaButt.addActionListener(handler);
regMaterijalaButt.addActionListener(handler);
regIntervencijaButt.addActionListener(handler);
regDijagnozaButt.addActionListener(handler);
exitButt.addActionListener(handler);
evidencijaPacButt.addActionListener(handler);
zakazivanjePacButt.addActionListener(handler);
evidencijaStomatologaButt.addActionListener(handler);
izvrseneUslugeButt.addActionListener(handler);
}
public class thehandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == regMaterijalaButt )
{
RegistarMaterijala regMat = new RegistarMaterijala();
}
if (event.getSource() == exitButt)
{
System.exit(0);
}
}
}
You button is defined twice, once as a local variable and once as an instance variable
JButton regMaterijalaButt = new JButton("Registar materijala");
Above is where you are defining the button as a local variable
if(event.getSource() == regMaterijalaButt )
Your ActionListener is referencing the instance variable.
Your code should be:
//JButton regMaterijalaButt = new JButton("Registar materijala");
regMaterijalaButt = new JButton("Registar materijala");
Following is my Java Swing applet code in which I have first field as employee code and text box for entering employee code, I want to retrieve data of employee from MySql database when I will enter employee code in text box after hitting Enter key on keyboard and set retrieve data in respective text boxes and combo boxes. and same to embed it in JSP as applet.
Please help.
Here is my code:
import javax.swing.*;
import java.applet.*;
import java.awt.*;
public class App extends JApplet{
public void start()
{
JFrame frame = new JFrame("Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
//JButton button2 = new JButton("Cancel");
Container c;
c=frame.getContentPane();
c.setLayout(null);
JLabel name=new JLabel("Name :");
JLabel compcode=new JLabel("Company Code :");
JLabel cardno=new JLabel("Card Number: ");
JLabel cardtype=new JLabel("Card Type :");
JLabel pin=new JLabel("Pin :");
JLabel bldgrp=new JLabel("Blood Group :");
JLabel empcode=new JLabel("Employee Code :");
JLabel dob=new JLabel("DOB :");
JLabel valupto=new JLabel("Valid Upto :");
JLabel jdate=new JLabel("Joining Date :");
JLabel dept=new JLabel("Department :");
JLabel uid=new JLabel("UID :");
String data []={"A","AB","B","B +","A +","O +","O -"};
JTextField nametxt=new JTextField(10);
JComboBox compcodetxt=new JComboBox();
JTextField cardnumtxt=new JTextField(10);
JTextField cardtypetxt=new JTextField(10);
JTextField pintxt=new JTextField(10);
JComboBox bldgrptxt=new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt=new JTextField(10);
JTextField dobtxt=new JTextField(10);
JTextField valuptotxt=new JTextField(10);
JTextField jdatetxt=new JTextField(10);
JTextField depttxt=new JTextField(10);
JTextField uidtxt=new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
//pin.setBounds(10, 110, 100, 25);
//pintxt.setBounds(110, 110, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
//button2.setBounds(10, 70, 75, 25);
c.add(name); c.add(nametxt);
c.add(compcode); c.add(compcodetxt);
c.add(cardno); c.add(cardnumtxt);
c.add(pin); c.add(pintxt);
c.add(bldgrp); c.add(bldgrptxt);
c.add(empcode); c.add(empcodetxt);
c.add(dob); c.add(dobtxt);
c.add(valupto); c.add(valuptotxt);
c.add(jdate); c.add(jdatetxt);
c.add(dept); c.add(depttxt);
c.add(uid); c.add(uidtxt);
c.add(but_per);
//panel.add(button1);
//panel.add(button2);
//frame.add(panel);
frame.setSize(350,400);
//frame.pack();
frame.setVisible(true);
}
public void stop(){}
}
In your ActionListener for that button retrieve the text of the textfield using textfield.getText() and then use this value to query database. I'm assuming that you know well how to query database.
Wait! you haven't implemented ActionListener!
Do the following:
but_per.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
/// write your code here!
String a= field.getText();
System.out.print(a);
///or DB code
}
});
Here is a LINK if you aren't familiar with Action Listener
UPDATE:
If you want all processing after a key press, you have to implement keybord events,
you can find a tutorial link here
This source is the closest workable version of your code I could produce, but it still has many serious problems.
Using the start() method incorrectly. The method will be called each time the browser is restored from minimized.
Use of a null layout and setBounds().
A free floating, non modal GUI element will cause focus problems..
Having said that, type a number in the employee code field and hit enter to see something like (screenshot shortened vertically)..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* <applet code=App width=400 height=600></applet> */
public class App extends JApplet {
public void start() {
final JFrame frame = new JFrame("Form");
ActionListener doDB = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "Query the DB off the EDT!");
}
};
// This cannot be done in a sand-boxed applet
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// hides the bottom field..
//frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
Container c;
c = frame.getContentPane();
c.setLayout(null);
JLabel name = new JLabel("Name :");
JLabel compcode = new JLabel("Company Code :");
JLabel cardno = new JLabel("Card Number: ");
JLabel cardtype = new JLabel("Card Type :");
JLabel pin = new JLabel("Pin :");
JLabel bldgrp = new JLabel("Blood Group :");
JLabel empcode = new JLabel("Employee Code :");
JLabel dob = new JLabel("DOB :");
JLabel valupto = new JLabel("Valid Upto :");
JLabel jdate = new JLabel("Joining Date :");
JLabel dept = new JLabel("Department :");
JLabel uid = new JLabel("UID :");
String data[] = {"A", "AB", "B", "B +", "A +", "O +", "O -"};
JTextField nametxt = new JTextField(10);
JComboBox compcodetxt = new JComboBox();
JTextField cardnumtxt = new JTextField(10);
JTextField cardtypetxt = new JTextField(10);
JTextField pintxt = new JTextField(10);
JComboBox bldgrptxt = new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt = new JTextField(10);
empcodetxt.addActionListener(doDB);
JTextField dobtxt = new JTextField(10);
JTextField valuptotxt = new JTextField(10);
JTextField jdatetxt = new JTextField(10);
JTextField depttxt = new JTextField(10);
JTextField uidtxt = new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
c.add(name);
c.add(nametxt);
c.add(compcode);
c.add(compcodetxt);
c.add(cardno);
c.add(cardnumtxt);
c.add(pin);
c.add(pintxt);
c.add(bldgrp);
c.add(bldgrptxt);
c.add(empcode);
c.add(empcodetxt);
c.add(dob);
c.add(dobtxt);
c.add(valupto);
c.add(valuptotxt);
c.add(jdate);
c.add(jdatetxt);
c.add(dept);
c.add(depttxt);
c.add(uid);
c.add(uidtxt);
c.add(but_per);
frame.setSize(350, 400);
frame.setVisible(true);
}
public void stop() {
}
}