import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
initComponents(); // Netbeans defined function non-editable
setTitle("Adding and Removing Components Dynamically From a JPanel in Java");
combo1_combo2_text1_array(); // declaring array for new row addition
}
@SuppressWarnings("unchecked")
private javax.swing.JComboBox[] combo1; //array of JComboBoxes
private javax.swing.JComboBox[] combo2; //array of JComboBoxes
private javax.swing.JTextField[] text1; //array of JTextFields
private int count = -1;
private int max_row = 10;
private int empty_count = 0;
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel3 = new javax.swing.JPanel();
removerow = new javax.swing.JButton();
addrow = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
jTextField1 = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
reset = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
removerow.setText("removerow");
removerow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removerowActionPerformed(evt);
}
});
addrow.setText("addrow");
addrow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addrowActionPerformed(evt);
}
});
private void removerowActionPerformed(java.awt.event.ActionEvent evt) {
if(count > -1){ // Deleting one row at a time
jPanel2.remove(combo1[count]);
jPanel2.remove(combo2[count]);
jPanel2.remove(text1[count]);
count--;
jPanel2.revalidate();
jPanel2.repaint();
}
}
private void addrowActionPerformed(java.awt.event.ActionEvent evt) {
if(count == max_row-1){
JOptionPane.showMessageDialog(null, "Maximum of 10 rows can be added","Failed!!",JOptionPane.ERROR_MESSAGE);
return;
}
count++;
combo1[count] = new javax.swing.JComboBox();
for(int i=1;i<=5;i++){
combo1[count].addItem("Item " + i);
}
combo2[count] = new javax.swing.JComboBox();
for(int i=1;i<=5;i++){
combo2[count].addItem("Item " + i);
}
text1[count] = new javax.swing.JTextField();
jPanel2.setLayout(new GridLayout(0,3,20,20));
jPanel2.add(combo1[count]);
jPanel2.add(combo2[count]);
jPanel2.add(text1[count]);
jPanel2.revalidate();
jPanel2.repaint();
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void resetActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
for(int i=count;i>-1;i--){
jPanel2.remove(combo1[i]);
jPanel2.remove(combo2[i]);
jPanel2.remove(text1[i]);
jPanel2.revalidate();
jPanel2.repaint();
}
count = -1;
}
private void combo1_combo2_text1_array(){
combo1 = new JComboBox[10];
combo2 = new JComboBox[10];
text1 = new JTextField[10];
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt) { // Cancel Button Action
System.exit(0);
}
private void submitActionPerformed(java.awt.event.ActionEvent evt) {
if(jTextField1.getText().length()==0) // Submit button for the register form
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields","Failed!!",JOptionPane.ERROR_MESSAGE);
else if(count > -1){
for(int i=0;i<=count;i++){
if(text1[count].getText().length()==0)
empty_count++;
}
if(empty_count > 0){
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields","Failed!!",JOptionPane.ERROR_MESSAGE);
empty_count = 0; // resetting the count
}
else
JOptionPane.showMessageDialog(null, "No database connection for this application","Failed!!",JOptionPane.ERROR_MESSAGE);
}
else
JOptionPane.showMessageDialog(null, "No database connection for this application","Failed!!",JOptionPane.ERROR_MESSAGE);
}
public static void main(String args[]) {
});
}
// Variables declaration - do not modify
private javax.swing.JButton addrow;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton removerow;
private javax.swing.JButton reset;
// End of variables declaration
}
This is the code i used. when adding rows it should add a row. It works but not adding those in a proper order.how can i correct that. i think there's a problem with panels. i can't find the fault. I want to add fields when clicking add rows button and deleting when clicking delete rows button. Deleting process is working good. but adding is not.
http://www.lionblogger.com/adding-and-removing-components-dynamically-from-jpanel/ this is where i got this code. can some one tell me where do i add these buttons and feilds? to which jpanel? In a one Jpanel or in two panels?
Can i use date pickers instead of text fields?