Updating Jlabel text from another class - java

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.

Related

I couldnt set location of components in Java for usinf JFrame and contentPane?

I want to create a simple profile information page with JFrame and contentPane. I added all the components correctly. But I cant set their location. How can i set it? I used setBoundary() method bur it doesnt work.
My Anket.java class is:
import java.awt.*;
import javax.swing.*;
public class Anket {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(600, 300));
frame.setTitle("Profile Information");
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.GRAY);
JRadioButton rdbtnNewRadioButton = new JRadioButton("Age(10-20)");
rdbtnNewRadioButton.setBounds(412, 50, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Age(21-30)");
rdbtnNewRadioButton.setBounds(412, 20, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_1);
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Age(31-40)");
rdbtnNewRadioButton.setBounds(412, 30, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_2);
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("Age(41-50)");
rdbtnNewRadioButton.setBounds(412, 40, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_3);
JLabel lblNewLabel = new JLabel("PLEASE ENTER YOUR INFORMATION");
lblNewLabel.setBounds(149, 0, 182, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Enter Your Name");
lblNewLabel_1.setBounds(127, 25, 86, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Enter Your Lastname");
lblNewLabel_2.setBounds(272, 25, 100, 14);
frame.getContentPane().add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("Enter Your Height");
lblNewLabel_3.setBounds(127, 103, 86, 14);
frame.getContentPane().add(lblNewLabel_3);
JTextField textField = new JTextField();
textField.setBounds(216, 100, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JTextField textField_1 = new JTextField();
textField_1.setBounds(127, 48, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JTextField textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(412, 48, 86, 20);
frame.getContentPane().add(textField_2);
JLabel lblNewLabel_4 = new JLabel(new ImageIcon("C:/Users/PC/eclipse-workspace/Project/src/Berguzar.jpg"));
lblNewLabel_4.setBounds(10, 21, 100, 85);
frame.getContentPane().add(lblNewLabel_4);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
The program doesnt have any error. How can i fix this problem?
My output screen is:
output
You should replace
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.GRAY);
with
JPanel contentPane = new JPanel(null); // The null is important
contentPane.setBackground(Color.GRAY);
frame.setContentPane(contentPane); // each time you change the content pane you need to revalidate the jframe (frame.setVisible(true) also revalidate the jframe so in this case it is not needed to revalidate the jframe)

JFrame: navigate to another frame using JButton

I'm trying to navigate with buttons in Java Swing.
I want to go to another existing frame when i click on a button.
I will attach first the Menu Panel:
public class MenuPanel extends JFrame implements ActionListener {
JLabel l1,l2;
JButton btn1,btn2,btn3,btn4;
MenuPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Menu Panel For Final Project");
l1 = new JLabel("Menu Panel Bitches: ");
l2 = new JLabel("This Program Made By Ghetto K");
l1.setForeground(Color.RED);
l1.setFont(new Font("Arial", Font.BOLD, 28));
l2.setForeground(Color.RED);
l2.setFont(new Font("Arial", Font.ITALIC,22));
btn1 = new JButton("Add Child");
btn2 = new JButton("Add Worker");
btn3 = new JButton("Add Class");
btn4 = new JButton("Add Task");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
l1.setBounds(150, 30, 400, 30);
l2.setBounds(150, 80, 400, 30);
btn1.setBounds(0, 230, 120, 30);
btn2.setBounds(160, 230, 120, 30);
btn3.setBounds(465, 230, 120, 30);
btn4.setBounds(320, 230, 120, 30);
add(l1);
add(l2);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
public static void main(String[] args) {
new MenuPanel();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
}
else if(e.getSource()==btn2)
{
}
else if(e.getSource()==btn3)
{
}
}
I want to navigate to this panel:
public class AddWorkerPanel extends JFrame implements ActionListener {
static Connection con = DatabaseConnection.getConnection();
JLabel l1, l2, l3;
JTextField tf1, tf2;
JButton btn1, btn2;
AddWorkerPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Add Worker form in Java");
l1 = new JLabel("Add Worker Form - Type Details Below:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Worker-Name:");
l3 = new JLabel("Worker-Class-Number:");
tf1 = new JTextField();
tf2 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
btn1.setBounds(80, 230, 200, 30);
btn2.setBounds(300, 230, 200, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(btn1);
add(btn2);
}
public static void main(String[] args) {
new AddWorkerPanel();
}
}
A JFrame is the actual frame that sits on the screen. So what you want to do is derive your MenuPanel and AddWorkerPanel classes from JPanel, so that they can both sit inside of one frame. Then, in your main frame class, you can have a CardLayout holding both of your panels, and have a button that cycles through the cards on the card layout, using the next() method in the card layout instance that you have stored.
If you just want to close one JFrame and then open the other you should be avble by just simply doing this in the calling method of your button:
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new AddWorkerPanel();
super.dispose();
}
}

Java Swing: Change JButton to an image button

I have been trying this for hours. I want to change each of the JButton components to image buttons (They were radio buttons that's why the variable names don't make sense). The for loop inside is just displaying all the images under the button's. Each image is called Option1, Option2, etc. So what I have been trying to do is get rid of the for loop and just have 10 buttons with images on them that you can click.
If someone could help me figure that out that would be great. All I've been able to do is have the button created with an image but the button appears on a completely different window.
public class JDialog2 extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Create the dialog.
*/
public JDialog2(Quiz quiz) {
setBounds(100, 100, 450, 600);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("2、Favourite subject at Hogwarts");
lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 14));
lblNewLabel_1.setBounds(94, 10, 248, 15);
contentPanel.add(lblNewLabel_1);
ButtonGroup btGroup = new ButtonGroup();
for(int i=1; i<=7; i++) {
ImageIcon image1 = new ImageIcon("Option" + i + ".jpg");
image1.setImage(image1.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
JLabel imageLablel1=new JLabel(image1);
contentPanel.add(imageLablel1);
imageLablel1.setBounds(29, 75 * i - 25, 50, 50);
}
JButton radioButton_1 = new JButton("1.Care of Magical Creatures");
radioButton_1.setBounds(29, 25, 226, 23);
contentPanel.add(radioButton_1);
JButton radioButton_2 = new JButton("2.Charms");
radioButton_2.setBounds(29, 50, 158, 23);
contentPanel.add(radioButton_2);
JButton radioButton_3 = new JButton("3.Defense Against the Dark Arts");
radioButton_3.setBounds(29, 75, 255, 23);
contentPanel.add(radioButton_3);
JButton radioButton_4 = new JButton("4.Divination");
radioButton_4.setBounds(29, 100, 121, 23);
contentPanel.add(radioButton_4);
JButton radioButton_5 = new JButton("5.Herbology");
radioButton_5.setBounds(29, 125, 179, 23);
contentPanel.add(radioButton_5);
JButton radioButton_6 = new JButton("6.History of Magic");
radioButton_6.setBounds(29, 150, 158, 23);
contentPanel.add(radioButton_6);
JButton radioButton_7 = new JButton("7.Muggle Studies");
radioButton_7.setBounds(29, 175, 121, 23);
contentPanel.add(radioButton_7);
JButton radioButton_8 = new JButton("8.Potions");
radioButton_8.setBounds(29, 200, 121, 23);
contentPanel.add(radioButton_8);
JButton radioButton_9 = new JButton("9.Study of Ancient Runes");
radioButton_9.setBounds(29, 220, 255, 23);
contentPanel.add(radioButton_9);
JButton radioButton_10 = new JButton("10.Transfiguration");
radioButton_10.setBounds(29, 245, 179, 23);
contentPanel.add(radioButton_10);
btGroup.add(radioButton_1);
btGroup.add(radioButton_2);
btGroup.add(radioButton_3);
btGroup.add(radioButton_4);
btGroup.add(radioButton_5);
btGroup.add(radioButton_6);
btGroup.add(radioButton_7);
btGroup.add(radioButton_8);
btGroup.add(radioButton_9);
btGroup.add(radioButton_10);
JButton btnCommit = new JButton("Commit");
btnCommit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Question question = quiz.getNextQuestion();
int choice = 0;
Enumeration<AbstractButton> en = btGroup.getElements();
while (en.hasMoreElements()) {
AbstractButton ab = en.nextElement();
choice++;
if (ab.isSelected()) {
break;
}
}
question.setSelectedAnswer(choice - 1);
JDialog3 dialog3 = new JDialog3(quiz);
dialog3.setVisible(true);
exit();
}
});
btnCommit.setBounds(300, 138, 93, 23);
contentPanel.add(btnCommit);
}
public void exit(){
this.setVisible(false);
}
}

Issue in Radio Button Action Listener

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?

why isn't my Jlabels or Jpanels showing?

i've added a title to my Jframe, and now its blocked everything else, what have I done??
public class addressbook
{
public JFrame frame;
public JButton btnadd, btndelete, btnsave, btnprev, btnnext;
public JPanel panel, pTitle;
public JTextField txtname, txtaddress, txthomeno, txtmobno;
public JLabel JlbName , JlbHtn, JlbMtn, JlbAddress, lblTitle;
public addressbook() {
//sets window
frame = new JFrame();
frame.setTitle("Address Book");
frame.setSize(450, 580);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//sets up panel
panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
pTitle = new JPanel();
pTitle.setLayout(new FlowLayout(FlowLayout.CENTER));
lblTitle = new JLabel("Bournemouth University Address Book");
pTitle.add(lblTitle);
frame.add(pTitle);
//Labels
JlbName = new JLabel("Name:");
JlbName.setBounds(10, 50, 100, 20);
panel.add(JlbName);
JlbHtn = new JLabel("Home Number:");
JlbHtn.setBounds(10, 90, 150, 20);
panel.add(JlbHtn);
JlbMtn = new JLabel("Mobile Number:");
JlbMtn.setBounds(10, 130, 200, 20);
panel.add(JlbMtn);
JlbAddress = new JLabel("Address:");
JlbAddress.setBounds(10, 170, 250, 20);
panel.add(JlbAddress);
//Text Fields
txtname = new JTextField("Name");
txtname.setBounds(120, 50, 200, 20);
panel.add(txtname);
txthomeno = new JTextField("Home Number");
txthomeno.setBounds(120, 90, 200, 20);
panel.add(txthomeno);
txtmobno = new JTextField("Mob Number");
txtmobno.setBounds(120, 130, 200, 20);
panel.add(txtmobno);
txtaddress = new JTextField("Address");
txtaddress.setBounds(120, 170, 250, 20);
panel.add(txtaddress);
frame.setVisible(true);
//Buttons && Button Functions
btnadd = new JButton("Add", new ImageIcon("../files/add.png"));
btnadd.setBounds(180, 350, 100, 50);
btnadd.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnadd);
btndelete = new JButton("Delete", new ImageIcon("../files/delete2.png"));
btndelete.setBounds(180, 450, 100, 50);
btndelete.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btndelete);
btnsave = new JButton("Save", new ImageIcon("../files/save.png"));
btnsave.setBounds(180, 400, 100, 50);
btnsave.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnsave);
btnprev = new JButton(new ImageIcon("../files/left.png"));
btnprev.setBounds(180, 300, 100, 50);
btnprev.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnprev);
btnnext = new JButton(new ImageIcon("../files/right.png"));
btnnext.setBounds(180, 250, 100, 50);
btnnext.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
}});
panel.add(btnnext);
frame.setVisible(true);
panel.setVisible(true);
}
If you are not using a LayoutManager on purpose (and it looks that way) make sure that you set a location and a size for your component.
pTitle.setLocation(100, 100);
pTitle.setSize(100, 100);
But you should rather remove this line
panel.setLayout(null);
and replace it with something like this:
panel.setLayout(new BorderLayout());
Also, don’t forget to add your pTitle to panel.
Add the label to the panel, don't create a new Panel for it
lblTitle = new JLabel("Bournemouth University Address Book");
lblTitle.setBounds(100, 0, 400, 20);
panel.add(lblTitle);
Try adding your JPanel pTitle to frame.getContentPane() or to the JPanel panel
Edit
Instead of
frame.add(pTitle);
do:
frame.getContentPanel().add(pTitle);
If this very quick fix doesn't help, stick with the answer you've already accepted.
You have to set a LayoutManager for your frame:
frame = new JFrame();
frame.getContentPane().setLayout(FooLayout());

Categories

Resources