Issue
I want the button "Submit" to open the form with the value set to the textfields, checked item, selected radio button to be in the jLabels and the jLabel in the desktoppane for the image in the second form
src="https://i.stack.imgur.com/ZldbF.png" alt="(Click this link to see the screenshot of the forms)" />
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates
- and open the template in the editor. */ package employeeapplication; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JFileChooser;
/** * * @author irecu */ public class Empmenu extends javax.swing.JFrame {
/** * Creates new form Empmenu */ public Empmenu() { initComponents(); } Submitted submit = new Submitted(); public class Empmennu extends Submitted{ } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jDesktopPane1 = new javax.swing.JDesktopPane(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox<>(); jScrollPane2 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList<>(); jCheckBox1 = new javax.swing.JCheckBox(); jTextField3 = new javax.swing.JTextField(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jRadioButton3 = new javax.swing.JRadioButton(); jRadioButton4 = new javax.swing.JRadioButton(); jScrollPane3 = new javax.swing.JScrollPane(); jTextArea2 = new javax.swing.JTextArea(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setAutoRequestFocus(false); setBounds(new java.awt.Rectangle(0, 0, 600, 600)); setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); setMaximumSize(new java.awt.Dimension(600, 600)); setResizable(false); setSize(new java.awt.Dimension(800, 800)); setType(java.awt.Window.Type.POPUP); jButton1.setText("Upload"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); jLabel1.setText("jLabel1"); jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255)); jDesktopPane1.setMaximumSize(new java.awt.Dimension(280, 280)); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel2.setMaximumSize(new java.awt.Dimension(280, 280)); jLabel2.setPreferredSize(new java.awt.Dimension(280, 280)); jDesktopPane1.setLayer(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER); javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1); jDesktopPane1.setLayout(jDesktopPane1Layout); jDesktopPane1Layout.setHorizontalGroup( jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 224,
javax.swing.GroupLayout.PREFERRED_SIZE) ); jDesktopPane1Layout.setVerticalGroup( jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE) );
jLabel3.setText("Name"); jLabel4.setText("Occupation"); jLabel5.setText("Age Category:"); jLabel6.setText("Employment:"); jLabel7.setText("Filipino?:"); jLabel8.setText("TIN:"); jLabel9.setText("Gender:"); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Employed", "Self-Employed", "Un-Employed" })); jScrollPane2.setName(""); // NOI18N jList1.setModel(new javax.swing.AbstractListModel<String>() { String[] strings = { "Minor: Under 18", "Adult: 18 to 59", "Senior: 60 Above" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jScrollPane2.setViewportView(jList1); jCheckBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCheckBox1ActionPerformed(evt); } }); jTextField3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField3ActionPerformed(evt); } }); jTextField3.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { jTextField3KeyTyped(evt); } }); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jRadioButton3.setText("Male"); jRadioButton4.setText("Female"); jRadioButton4.setToolTipText(""); jTextArea2.setColumns(20); jTextArea2.setRows(5); jScrollPane3.setViewportView(jTextArea2); jButton3.setText("Submit"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Preview Details"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton2.setText("Clear"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false) .addGroup(layout.createSequentialGroup() .addComponent(jLabel4) .addGap(18, 18, 18) .addComponent(jTextField2)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6) .addComponent(jLabel8) .addComponent(jLabel7) .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jCheckBox1) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField1) .addComponent(jTextField3))) .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(75, 75, 75) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton3) .addComponent(jRadioButton4)))) .addComponent(jLabel3) .addGroup(layout.createSequentialGroup() .addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addGap(65, 65, 65) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addGap(18, 18, 18) .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(45, 45, 45) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(21, 21, 21) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel5) .addGap(31, 31, 31))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(24, 24, 24))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addComponent(jLabel9)) .addGroup(layout.createSequentialGroup() .addComponent(jRadioButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton4))) .addGap(18, 18, 18) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(16, 16, 16) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 5, Short.MAX_VALUE)) .addComponent(jScrollPane3)) .addGap(22, 22, 22)) );
pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: JFileChooser chooser= new JFileChooser(); chooser.showOpenDialog(null); File f=chooser.getSelectedFile(); jLabel2.setIcon(new ImageIcon(f.toString())); filename=f.getAbsolutePath(); jLabel1.setText(filename); } private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(jCheckBox1.isSelected()){ jTextField3.setEnabled(true); } else{ jTextField3.setEnabled(false); } } private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jTextField3KeyTyped(java.awt.event.KeyEvent evt) { // TODO add your handling code here: char character = evt.getKeyChar(); if (((character < '0') || (character > '9')) && (character != '\b')) { evt.consume(); } } private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jTextArea2.append("Name: " + jTextField1.getText()+"\n" ); jTextArea2.append("Occupation: " + jTextField2.getText()+"\n"); if(jList1.isSelectionEmpty()){ jTextArea2.append("Age Category: null \n"); } else{ jTextArea2.append("Age Category: " + jList1.getSelectedValuesList().toString()+"\n"); } jTextArea2.append("Employment: " + jComboBox1.getSelectedItem().toString()+"\n"); if(jCheckBox1.isSelected()){ jTextArea2.append("Filipino: Yes"+ "\n"); jTextArea2.append("TIN: "+ jTextField3.getText() + "\n"); } else{ jTextArea2.append("Filipino: No" + "\n"); } if(jRadioButton3.isSelected()){ jTextArea2.append("Gender: " + jRadioButton3.getText() + "\n" + "\n"); } else{ jTextArea2.append("Gender: " + jRadioButton4.getText() + "\n" +"\n"); } jTextArea2.append("\n"); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jTextArea2.setText(""); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { submit.setVisible(true); // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //
/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Empmenu().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JDesktopPane jDesktopPane1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JList<String> jList1; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JRadioButton jRadioButton4; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTextArea jTextArea2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; // End of variables declaration
byte[] photo=null; String filename=null; }
Solution
There are several ways to do this sort of thing. Off hand, a simple way would be to create another class which extends JFrame named SubmissionForm then Create a constructor for this new class that will accept all the required information to display, for example (non-runnable):
// Your first form's class (just an example)
public class DataEntryForm extends JFrame {
public DataEntryForm() {
initializeForm();
}
// ... all your other class code ...
private void initializeForm() {
// Create an ActionListener
submitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonActionPerformed(evt);
}
});
// ... all the other code required to create your form ...
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DataEntryForm().setVisible(true);
}
});
}
// The Submit Button ActionPerformed event
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Gather the supplied Data...
String name = nameTextField.getText();
String occupation = occupationTextField.getText();
String ageCategory = ageCategoryList.getSelectedValue().toString();
String employment = employmentCombo.getSelectedItem().toString();
boolean filipino = filipinoCheckBox.isSelected();
String tin = tinTextField.getText();
String gender = genderRadioButton1.isSelected() ? "Male" :
genderRadioButton2.isSelected() ? "Female" : "Unknown";
// Display this gathered Data in the other form...
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SubmissionForm(name, occupation, ageCategory, employment, filipino,
tin, gender).setVisible(true);
}
});
}
}
Now the SubmissionForm class. Notice the constructor and the code within it. Also notice that there is no main() method in this class:
public class SubmissionForm extends javax.swing.JFrame {
// Class Constructor...
public SubmissionForm(String name, String occupation, String ageCategory,
String employment, boolean isFilipino, String tin,
String gender) {
initializeForm();
// JLabel components on this form
lblName.setText(name);
lblOccupation.setText(occupation);
lblAgeCategory.setText(ageCategory);
lblEmployment.setText(employment);
lblPhilipino.setText(String.valueOf(isFilipino).toUpperCase());
lblTIN.setText(tin);
lblGender.setText(gender);
}
private void initializeForm() {
// Your code to initialize form components here...
}
}
You could place all the gathered data into an Array, ArrayList, or List Interface and pass that through the constructor and then work with that collection of data before the form is displayed.
The code above is not a runnable. It was supplied off the top of my head and therefore may contain some small errors.
In other words ... create a constructor similar to what was done here for your second form.
Answered By - DevilsHnd
Answer Checked By - Senaida (JavaFixing Volunteer)