Issue in Radio Button Action Listener - java

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?

Related

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);
}
}

Adding new JTextFields at the click of a JButton

I am working on an Invoice System and I want to create new fields each time the add new button is clicked.
It needs to add the fields in the code below each time.
The fields need to appear under its respective columns.
JPanel panel_2 = new JPanel();
panel_2.setBounds(10, 256, 990, 303);
panel.add(panel_2);
panel_2.setLayout(null);
code = new JTextField();
code.setBounds(10, 11, 86, 20);
panel_2.add(code);
code.setColumns(10);
code.setEditable(false);
desc = new JTextField();
desc.setBounds(106, 11, 345, 20);
panel_2.add(desc);
desc.setColumns(10);
desc.setEditable(false);
quantity = new JTextField("0");
quantity.setBounds(461, 11, 86, 20);
panel_2.add(quantity);
quantity.setColumns(10);
quantity.setEditable(false);
price = new JTextField("0");
price.setBounds(557, 11, 106, 20);
panel_2.add(price);
price.setColumns(10);
price.setEditable(false);
individualTotal = new JTextField();
individualTotal.setBounds(673, 11, 106, 20);
panel_2.add(individualTotal);
individualTotal.setColumns(10);
individualTotal.setEditable(false);
Below is my buttons that I have set up:
JButton newEntry = new JButton("+");
newEntry.setBackground(Color.PINK);
newEntry.setForeground(Color.BLUE);
newEntry.setFont(new Font("Tahoma", Font.BOLD, 15));
newEntry.setBounds(10, 204, 57, 20);
panel.add(newEntry);
newEntry.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
code.setEditable(true);
desc.setEditable(true);
quantity.setEditable(true);
price.setEditable(true);
individualTotal.setEditable(true);
}
});
newEntry.setEnabled(false);
JButton minusEntry = new JButton("-");
minusEntry.setBackground(Color.PINK);
minusEntry.setForeground(Color.RED);
minusEntry.setFont(new Font("Wide Latin", Font.BOLD, 16));
minusEntry.setBounds(77, 205, 57, 20);
panel.add(minusEntry);
minusEntry.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
code.setEditable(false);
desc.setEditable(false);
quantity.setEditable(false);
price.setEditable(false);
individualTotal.setEditable(false);
}
});
minusEntry.setEnabled(false);
I know there must be an answer somewhere on this site but I cannot seem to find it.
Please also note that I am new at Java development
Create your own subclass of a JPanel which represents one "datasheet" or tablecell or what ever
public DataPanel extends JPanel{
private JTextField field1 = new JTextField();
private JTextField field2 = new JTextField();
// ..... and so on
public DataPanel(YourDataObject data){
field1.setText(data.getValue1());
field2.setText(data.getValue2());
// ... and so on
// then add all of your text fields to the panel
add(field1);
add(field2);
// .... and so on
}
}
Then on button click you add the panel to the component you want to show it on
onClick(SomeEvent event){
yourComponent.add(new DataPanel(yourDataObject));
}

java GUI how to save into text file

i need to save texts from text boxes to text file but any code i found on or made it didn't work, any idea? like any at all? which code would work. i had to use java gui where my teacher teach us about java, but nothing about GUI so ii cant even make save to text file.
public class FlightsPanel extends JPanel {
private Flight flightSelected = null;
private JTextField textFieldDepartureAirport;
private JTextField textFieldArrivalAirport;
private JTextField textFieldDepartureDate;
private JTextField textFieldArrivalDate;
private JRadioButton rdbtnBoarding;
private JRadioButton rdbtnChecking;
private JRadioButton rdbtnAvailable;
private JRadioButton rdbtnClosed;
private JTextField textFieldCost;
private DefaultListModel<String> populateFlights() {
DefaultListModel<String> list = new DefaultListModel<String>();
ArrayList<Flight> FlightList = MainMenu.getAirlineMgr().getFlightList();
for (int i = 0; i < FlightList.size(); i++) {
list.addElement(FlightList.get(i).getFlightNumber());
}
return list;
}
private void displayFlight(String number) {
flightSelected = MainMenu.getAirlineMgr().getFlightByNumber(number);
textFieldDepartureAirport.setText(flightSelected.getDepartureAirport());
textFieldArrivalAirport.setText(flightSelected.getArrivalAirport());
textFieldDepartureDate.setText(flightSelected.getDepartureDate().toString());
textFieldArrivalDate.setText(flightSelected.getArrivalDate().toString());
textFieldCost.setText(Double.toString(flightSelected.getCost()));
if (flightSelected.getFlightStatus() == Flight.Status.AVAILABLE) {
rdbtnAvailable.setSelected(true);
} else {
if (flightSelected.getFlightStatus() == Flight.Status.BOARDING) {
rdbtnBoarding.setSelected(true);
} else {
if (flightSelected.getFlightStatus() == Flight.Status.CHECKING) {
rdbtnChecking.setSelected(true);
} else {
if (flightSelected.getFlightStatus() == Flight.Status.CLOSED) {
rdbtnClosed.setSelected(true);
}
}
}
}
}
private void saveFlight() {
flightSelected.setDepartureAirport(textFieldDepartureAirport.getText());
flightSelected.setArrivalAirport(textFieldArrivalAirport.getText());
flightSelected.setCost(Double.parseDouble(textFieldCost.getText()));
// flightSelected.setDepartureDate(textFieldDepartureDate.getText());
// flightSelected.setArrivalDate(textFieldArrivalDate.getText());
if (rdbtnAvailable.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.AVAILABLE);
} else {
if (rdbtnBoarding.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.BOARDING);
} else {
if (rdbtnChecking.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.CHECKING);
} else {
if (rdbtnClosed.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.CLOSED);
}
}
}
}
}
public FlightsPanel() {
setBackground(new Color(175, 238, 238));
setLayout(null);
JList list = new JList(populateFlights());
list.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
displayFlight(list.getSelectedValue().toString());
}
});
list.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
list.setBounds(141, 161, 89, 81);
add(list);
JLabel lblNewLabel = new JLabel("Available flights:");
lblNewLabel.setBounds(141, 136, 121, 14);
add(lblNewLabel);
textFieldDepartureAirport = new JTextField();
textFieldDepartureAirport.setBounds(206, 36, 112, 20);
add(textFieldDepartureAirport);
textFieldDepartureAirport.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Departure Airport:");
lblNewLabel_1.setBounds(206, 11, 112, 14);
add(lblNewLabel_1);
textFieldArrivalAirport = new JTextField();
textFieldArrivalAirport.setColumns(10);
textFieldArrivalAirport.setBounds(10, 36, 112, 20);
add(textFieldArrivalAirport);
JLabel lblArrivalAirport = new JLabel("Arrival Airport:");
lblArrivalAirport.setBounds(10, 11, 112, 14);
add(lblArrivalAirport);
textFieldDepartureDate = new JTextField();
textFieldDepartureDate.setColumns(10);
textFieldDepartureDate.setBounds(206, 91, 192, 20);
add(textFieldDepartureDate);
JLabel lblDepartureDate = new JLabel("Departure Date:");
lblDepartureDate.setBounds(206, 67, 112, 14);
add(lblDepartureDate);
textFieldArrivalDate = new JTextField();
textFieldArrivalDate.setColumns(10);
textFieldArrivalDate.setBounds(10, 91, 192, 20);
add(textFieldArrivalDate);
JLabel lblArrivalDate = new JLabel("Arrival Date:");
lblArrivalDate.setBounds(10, 66, 112, 14);
add(lblArrivalDate);
rdbtnBoarding = new JRadioButton("BOARDING");
rdbtnBoarding.setBounds(10, 211, 112, 23);
add(rdbtnBoarding);
rdbtnChecking = new JRadioButton("CHECKING");
rdbtnChecking.setBounds(10, 237, 112, 23);
add(rdbtnChecking);
rdbtnAvailable = new JRadioButton("AVAILABLE");
rdbtnAvailable.setBounds(10, 159, 112, 23);
add(rdbtnAvailable);
JLabel lblStatus = new JLabel("Status:");
lblStatus.setBounds(20, 138, 220, 14);
add(lblStatus);
rdbtnClosed = new JRadioButton("CLOSED");
rdbtnClosed.setBounds(10, 185, 112, 23);
add(rdbtnClosed);
ButtonGroup group = new ButtonGroup();
group.add(rdbtnBoarding);
group.add(rdbtnChecking);
group.add(rdbtnAvailable);
group.add(rdbtnClosed);
JButton button = new JButton("Cancel");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MainMenu.hideFlights();
}
});
button.setBounds(10, 269, 89, 23);
add(button);
JButton btnConfirm = new JButton("Confirm");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveFlight();
MainMenu.hideFlights();
}
});
btnConfirm.setBounds(351, 269, 89, 23);
add(btnConfirm);
JLabel lblCost = new JLabel("Cost:");
lblCost.setBounds(341, 11, 112, 14);
add(lblCost);
textFieldCost = new JTextField();
textFieldCost.setColumns(10);
textFieldCost.setBounds(328, 36, 112, 20);
add(textFieldCost);
}
}pic of design
You can use basic file writing from Java. The only difference here from basic file writing is that you need to get the Strings to write from the text attributes of your objects. Here is one example from your code:
import java.io.PrintWriter;
try{
PrintWriter fileWriter = new PrintWriter("myFile.txt", "UTF-8");
fileWriter.println(textFieldDepartureAirport.selectAll());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
Since your teacher did not include much about the UI items, you are able to look up the JavaDoc info about the object type that is holding the data. For example, your text boxes are declared as JTextField, so you can find more info about this class here:
https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html

Java Swing open existing form

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);
}
}

Connecting of Database per each JFrame

I have created this Automated Library. And I need to know how to connect my Database per each JFrame.
This is my main method:
package com.Student.GUI;
public class Student_HomePage extends JFrame {
private JPanel contentPane;
private JTextField searchBox;
private static String results[];
private static Connection conn;
private static String searchText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DBConnect db = new DBConnect();
conn = db.openConnection();
Student_HomePage frame = new Student_HomePage();
frame.setVisible(true);
frame.setBounds(400,150,599,489);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Student_HomePage() {
setBackground(Color.WHITE);
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\ITC302-Core Java\\java_workspace\\Pictures\\lala.png"));
setTitle("LookBook");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 489);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnHome.setEnabled(false);
btnHome.setContentAreaFilled(false);
btnHome.setBorder(null);
btnHome.setBounds(144, 24, 95, 25);
contentPane.add(btnHome);
JButton btnAuthors = new JButton("Authors");
btnAuthors.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnAuthors.setContentAreaFilled(false);
btnAuthors.setBorder(null);
btnAuthors.setBounds(241, 24, 95, 25);
contentPane.add(btnAuthors);
JButton btnBooks = new JButton("Books");
btnBooks.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnBooks.setContentAreaFilled(false);
btnBooks.setBorder(null);
btnBooks.setBounds(338, 24, 95, 25);
contentPane.add(btnBooks);
JTextArea textArea = new JTextArea();
textArea.setText("|");
textArea.setFont(new Font("Monospaced", Font.PLAIN, 35));
textArea.setEditable(false);
textArea.setBounds(227, 11, 26, 57);
contentPane.add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setText("|");
textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 35));
textArea_1.setEditable(false);
textArea_1.setBounds(325, 11, 26, 57);
contentPane.add(textArea_1);
searchBox = new JTextField();
searchBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
searchText = temp;
}
});
searchBox.setColumns(10);
searchBox.setBounds(37, 210, 148, 25);
contentPane.add(searchBox);
JTextArea textArea_2 = new JTextArea();
textArea_2.setText("Search title of books,\r\nauthors or genre");
textArea_2.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 14));
textArea_2.setBounds(37, 248, 188, 40);
contentPane.add(textArea_2);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
btnSearchActionPerformed(e, temp);
}
});
btnSearch.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\searchIcon.png"));
btnSearch.setContentAreaFilled(false);
btnSearch.setBorder(null);
btnSearch.setBackground(Color.WHITE);
btnSearch.setBounds(185, 210, 87, 25);
contentPane.add(btnSearch);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\download.jpg"));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(270, 202, 290, 215);
contentPane.add(label);
JLabel bookIcon = new JLabel("");
bookIcon.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\BookIcon.png"));
bookIcon.setBounds(101, 78, 116, 90);
contentPane.add(bookIcon);
JLabel lookBookIcon = new JLabel("");
lookBookIcon.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\image.png"));
lookBookIcon.setBounds(213, 74, 290, 135);
contentPane.add(lookBookIcon);
}
public void btnSearchActionPerformed (ActionEvent e, String searchText) {
DBConnect db = new DBConnect();
results = db.selectMatchedData(searchText, conn);
//if (results.length != 0) {
this.dispose();
Student_SearchList window = new Student_SearchList();
window.setVisible(true);
//}
//else {
//Student_Warning window = new Student_Warning();
//window.setVisible(true);
//}
}
}
And this is where I want to connect my Database:
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
btnSearchActionPerformed(e, temp);
}
});
This whole code is my Main.java.

Categories

Resources