I am learning and new to GUI and in this program used ECLIPSE IDE (drag and drop windows builder)
I declared two radio buttons for Gender: Male and Female
If its clicks to male or female button it should only (click one) and go to that button and not both, so therefore I want it automatically the other one to be unselected to avoid duplicate selection
So I already did some research and import javax.swing.ButtonGroup; but I'm still confused as to why it doesn't work because I can still click both of the radio buttons. I think it has something to do with my panel?
Why does it happen and how can I fix it?
Here is my program
public AddRecord() {
setUndecorated(true);
setBackground(Color.WHITE);
setVisible(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 1063, 640);
contentPane = new JPanel();
contentPane.setBounds(new Rectangle(5, 0, 0, 0));
contentPane.setBackground(new Color(255, 255, 240));
contentPane.setBorder(new LineBorder(new Color(0, 0, 28), 1, true));
setLocationRelativeTo(null);
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel pnlInfo = new JPanel();
pnlInfo.setBounds(24, 20, 1015, 597);
contentPane.add(pnlInfo);
pnlInfo.setLayout(null);
JLabel lblGender = new JLabel("Gender:");
lblGender.setBounds(32, 174, 50, 10);
pnlInfo.add(lblGender);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 11));
ButtonGroup btnBg = new ButtonGroup();
btnBg.add(rdbtnMale);
btnBg.add(rdbtnFemale);
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBounds(79, 169, 55, 21);
pnlInfo.add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBounds(136, 169, 76, 21);
pnlInfo.add(rdbtnFemale);
}
Your code does not even compile,
move the lines
ButtonGroup btnBg = new ButtonGroup();
btnBg.add(rdbtnMale);
btnBg.add(rdbtnFemale);
to the end of your code
like this
setUndecorated(true);
setBackground(Color.WHITE);
setVisible(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 1063, 640);
JPanel contentPane = new JPanel();
contentPane.setBounds(new Rectangle(5, 0, 0, 0));
contentPane.setBackground(new Color(255, 255, 240));
contentPane.setBorder(new LineBorder(new Color(0, 0, 28), 1, true));
setLocationRelativeTo(null);
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel pnlInfo = new JPanel();
pnlInfo.setBounds(24, 20, 1015, 597);
contentPane.add(pnlInfo);
pnlInfo.setLayout(null);
JLabel lblGender = new JLabel("Gender:");
lblGender.setBounds(32, 174, 50, 10);
pnlInfo.add(lblGender);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 11));
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBounds(79, 169, 55, 21);
pnlInfo.add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBounds(136, 169, 76, 21);
pnlInfo.add(rdbtnFemale);
ButtonGroup btnBg = new ButtonGroup();
btnBg.add(rdbtnMale);
btnBg.add(rdbtnFemale);
Related
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)
I'm working on a group project with my friends for a school assignment. Unfortunately, one of my group members made this program which was much worse before (I tried to fix most of the main issues). Only problem now is, the program doesn't load all of the text on the right, instead leaving ellipses (...).
I've tried modifying and removing setBounds, setSize, and contentPane, yet all those proved ineffective. I'm fairly new to Java all round, so please do go easy on my inexperience.
public class HomeMenu extends JFrame {
private JPanel contentPane;
private JTextField textField;
public HomeMenu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1366, 768);
setBounds(100, 100, 954, 712);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setBackground(new Color(255, 255, 255));
getContentPane().setLayout(null);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setForeground(Color.WHITE);
panel.setBounds(0, 0, 236, 671);
getContentPane().add(panel);
panel.setLayout(null);
JPanel panel_3 = new JPanel();
panel_3.setBounds(44, 5, 1, 1);
panel.add(panel_3);
panel_3.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(0, 0, 102));
panel_1.setBounds(233, 240, 705, 431);
getContentPane().add(panel_1);
panel_1.setLayout(null);
JPanel panel_6 = new JPanel();
panel_6.setBackground(SystemColor.controlDkShadow);
panel_6.setBounds(307, 235, 285, 164);
panel_1.add(panel_6);
JLabel lblWED = new JLabel("Weddings\r\n");
panel_6.add(lblWED);
lblWED.setForeground(Color.WHITE);
lblWED.setFont(new Font("Sitka Subheading", Font.PLAIN, 23));
lblWED.setBackground(Color.WHITE);
JLabel lblNewLabel_4 = new JLabel("");
panel_6.add(lblNewLabel_4);
lblNewLabel_4.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_index2.jpg"));
JPanel panel_4 = new JPanel();
panel_4.setBackground(SystemColor.controlDkShadow);
panel_4.setBounds(43, 25, 189, 298);
panel_1.add(panel_4);
JLabel lblBirthdayPartiesl = new JLabel("Birthday Partiesl");
panel_4.add(lblBirthdayPartiesl);
lblBirthdayPartiesl.setFont(new Font("Snap ITC", Font.PLAIN, 18));
lblBirthdayPartiesl.setForeground(Color.WHITE);
lblBirthdayPartiesl.setBackground(Color.WHITE);
JLabel lblNewLabel_3 = new JLabel("");
panel_4.add(lblNewLabel_3);
lblNewLabel_3.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_index.jpg"));
JLabel lblWeDoIt = new JLabel("WE DO nothing at ALL !");
lblWeDoIt.setBounds(68, 359, 212, 40);
panel_1.add(lblWeDoIt);
lblWeDoIt.setForeground(Color.WHITE);
lblWeDoIt.setFont(new Font("Tahoma", Font.PLAIN, 28));
lblWeDoIt.setBackground(Color.WHITE);
JPanel panel_5 = new JPanel();
panel_5.setBounds(297, 25, 358, 185);
panel_1.add(panel_5);
panel_5.setBackground(SystemColor.controlDkShadow);
JLabel lblMeetings = new JLabel("Meetings");
panel_5.add(lblMeetings);
lblMeetings.setForeground(Color.WHITE);
lblMeetings.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblMeetings.setBackground(Color.WHITE);
JLabel lblNewLabel_6 = new JLabel("");
panel_5.add(lblNewLabel_6);
lblNewLabel_6.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_index(1)"));
JLabel lblNewLabel_5 = new JLabel("");
panel_5.add(lblNewLabel_5);
lblNewLabel_5.setIcon(new ImageIcon("C:\\Users\\18060327\\Desktop\\index 3.jpg"));
JPanel panel_2 = new JPanel();
panel_2.setBounds(235, 54, 703, 188);
contentPane.add(panel_2);
panel_2.setBackground(new Color(204, 204, 204));
panel_2.setLayout(null);
JLabel lblNewLabel = new JLabel("ShadEvents");
lblNewLabel.setFont(new Font("Segoe UI Light", Font.PLAIN, 36));
lblNewLabel.setBounds(61, 24, 286, 58);
panel_2.add(lblNewLabel);
JLabel label = new JLabel("Here to make your life harder");
label.setFont(new Font("Segoe UI Light", Font.PLAIN, 13));
label.setBounds(78, 84, 188, 16);
panel_2.add(label);
textField = new JTextField();
textField.setBounds(297, 11, 123, 24);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("");
lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\icons8-user-24.png"));
lblNewLabel_2.setBounds(851, 11, 29, 32);
contentPane.add(lblNewLabel_2);
JLabel lblUser = new JLabel("Welcome!"); //fix this user shit //unfixable
lblUser.setBounds(890, 19, 38, 24);
contentPane.add(lblUser);
lblUser.setFont(new Font("Segoe UI Light", Font.PLAIN, 15));
JLabel lblNewLabel_1 = new JLabel();
lblNewLabel_1.setBounds(258, 11, 29, 24);
contentPane.add(lblNewLabel_1);
lblNewLabel_1.setBackground(Color.BLUE);
lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\HP\\Downloads\\groupwork_src_icons8-search-24.png"));
}
}
Instead of "WE DO nothin..." and "Wel..." (at the top right), it should display "WE DO nothing at ALL!" (Also, do let me know if the code doesn't work.)
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);
}
}
basically I have a very simple GUI, but for some reason when I run the code the JButtons only appear when I mouse over them, and the JTextField only appear if I click on them. How do I fix this so that they are visible when the program runs? Thanks.
Here is my code:
//JFrame + settings
JFrame frmFormSubmission = new JFrame();
frmFormSubmission.setSize(new Dimension(350, 165));
frmFormSubmission.setTitle("Form Submission - Client");
frmFormSubmission.setLocationRelativeTo(null);
frmFormSubmission.setVisible(true);
frmFormSubmission.setResizable(false);
frmFormSubmission.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel
JPanel panel = new JPanel();
frmFormSubmission.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
//JLabels
//Name
JLabel lblName = new JLabel("Name:");
lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblName.setBounds(46, 12, 40, 14);
panel.add(lblName);
//Address
JLabel lblAddress = new JLabel("Address:");
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblAddress.setBounds(33, 37, 53, 14);
panel.add(lblAddress);
//Phone
JLabel lblPhone = new JLabel("Phone #:");
lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblPhone.setBounds(28, 62, 58, 14);
panel.add(lblPhone);
//Email
JLabel lblEmail = new JLabel("Email:");
lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEmail.setBounds(46, 87, 36, 14);
panel.add(lblEmail);
//JTextFields
//Name
nameField = new JTextField();
nameField.setBounds(93, 11, 137, 20);
panel.add(nameField);
nameField.setColumns(10);
//Address
addressField = new JTextField();
addressField.setColumns(10);
addressField.setBounds(93, 36, 137, 20);
panel.add(addressField);
//Phone
phoneField = new JTextField();
phoneField.setColumns(10);
phoneField.setBounds(93, 61, 137, 20);
panel.add(phoneField);
//Email
emailField = new JTextField();
emailField.setColumns(10);
emailField.setBounds(93, 86, 137, 20);
panel.add(emailField);
//JButtons
//Submit
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(240, 10, 89, 23);
panel.add(btnSubmit);
//Cancel
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(240, 44, 89, 23);
panel.add(btnCancel);
//Flush
JButton btnFlush = new JButton("Flush");
btnFlush.setBounds(240, 76, 89, 23);
panel.add(btnFlush);
//Checkbox
JCheckBox chckbxPromotions = new JCheckBox("Email me with new and promotions!");
chckbxPromotions.setBounds(25, 108, 205, 23);
panel.add(chckbxPromotions);
Any help is very much appreciated!
Call frmFormSubmission.setVisible(true); as the last thing you do. setVisible will layout your components so they can be properly drawn. If you add components without laying them out, then you'll have graphical issues.
For more information on what that means, check out the documentation on the validate method.
Also, try
frmFormSubmission.invalidate();
frmFormSubmission.validate();
frmFormSubmission.repaint();
If this still does not work, you can try this:
SwingUtilities.updateComponentTreeUI(frmFormSubmission);
In following code that component are display properly but we cant write on the JTextField.
public class CurrentOPDDetail extends JInternalFrame
{
JLabel[] line=new JLabel[10];
JLabel[] lbl=new JLabel[33];
JTextField txtName=new JTextField();
JTextField txtAge=new JTextField();
JTextField txtSex=new JTextField();
JTextField txtWeight=new JTextField();
public CurrentOPDDetail(int n)
{
super("OPD Detail Form",false,false);//Title, Resizable, closable
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize.width-500,650);
setLocation(170,10);
setVisible(true);
setLayout(null);
lbl[0]=new JLabel("NAME");
lbl[1]=new JLabel("AGE");
lbl[2]=new JLabel("SEX");
lbl[3]=new JLabel("WEIGHT");
lbl[4]=new JLabel("HISTORY");
for(int i=0;i<10;i++)
{
line[i]=new JLabel("");
line[i].setBackground(Color.BLACK);
line[i].setOpaque(true);
}
lbl[0].setBounds(1, 5, 40, 25);
txtName.setBounds(41, 5, 200, 25);
lbl[1].setBounds(262, 5, 30, 25);
txtAge.setBounds(293, 5, 50, 25);
lbl[2].setBounds(370, 5, 30, 25);
txtSex.setBounds(400, 5, 50, 25);
lbl[3].setBounds(470, 5, 50, 25);
txtWeight.setBounds(520, 5, 100, 25);
line[0].setBounds(1, 33, screenSize.width-499, 5);
lbl[4].setBounds(1, 40, 150, 25);
add(line[0]);
add(lbl[0]);
add(txtName);
add(lbl[1]);
add(txtAge);
add(lbl[2]);
add(txtSex);
add(lbl[3]);
add(txtWeight);
}
}
Just use JFrame and getContentPane().add().
The text fields of your internal window work fine then:
JFrame frame = new JFrame();
frame.getContentPane().add(new CurrentOPDDetail(5));
frame.setVisible(true);