why my array is only letting me list one name ? And how to number the rows
Fantastic news! We've Found the answer you've been seeking!
Question:
why my array is only letting me list one name ? And how to number the rows automatically from 1 > (for priority)
* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template */ package air.canada; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import javax.swing.JOptionPane; import java.util.Arrays; /** * * @author sprince */ public class Waitlist extends javax.swing.JFrame { ArrayList nameList = new ArrayList(); File nameSave = new File("waitList.txt"); /** * Creates new form Waitlist */ public Waitlist() { initComponents(); } /** * 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") // private void initComponents() { titleLbl = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); outputArea = new javax.swing.JTextArea(); addBtn = new javax.swing.JButton(); removeBtn = new javax.swing.JButton(); exitBtn = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); titleLbl.setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N titleLbl.setForeground(new java.awt.Color(255, 0, 51)); titleLbl.setText("Waitlist"); outputArea.setColumns(20); outputArea.setRows(5); jScrollPane1.setViewportView(outputArea); addBtn.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N addBtn.setForeground(new java.awt.Color(255, 0, 0)); addBtn.setText("ADD"); addBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addBtnActionPerformed(evt); } }); removeBtn.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N removeBtn.setForeground(new java.awt.Color(255, 0, 0)); removeBtn.setText("REMOVE"); removeBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { removeBtnActionPerformed(evt); } }); exitBtn.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N exitBtn.setForeground(new java.awt.Color(255, 0, 0)); exitBtn.setText("EXIT"); exitBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitBtnActionPerformed(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) .addComponent(jScrollPane1) .addGroup(layout.createSequentialGroup() .addComponent(addBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(removeBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(exitBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addGap(135, 135, 135) .addComponent(titleLbl) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(7, 7, 7) .addComponent(titleLbl) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(removeBtn) .addComponent(addBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(exitBtn) .addGap(18, 18, 18)) ); pack(); }// private void addBtnActionPerformed(java.awt.event.ActionEvent evt) { //Get Info String firstName = JOptionPane.showInputDialog(null, "Please enter your first name.", "Information Required", JOptionPane.INFORMATION_MESSAGE); if (firstName.isEmpty()) { }else{ String lastName = JOptionPane.showInputDialog(null, "Please enter your last name.", "Information Required", JOptionPane.INFORMATION_MESSAGE); if (firstName.isEmpty()) { }else{ ArrayList tempRow = new ArrayList(); // add info to array tempRow.add(firstName); tempRow.add(lastName); //add temp list to global nameList.add(tempRow); //output outputArea.append(" "+"Added Successfully!");} //print placeholders//clear previous outputArea.setText("First Name\tLast Name\t\tPriority "); //loop through each row//print colums for(int row = 0; row tempRow = nameList.get(row); //print to area outputArea.append(" "+tempRow.get(0)+"\t"); outputArea.append(tempRow.get(1)+"\t"); outputArea.append(tempRow.get(2)+"\t"); outputArea.append(tempRow.get(3)+"\t"); outputArea.append(tempRow.get(4)+"\t"); outputArea.append(tempRow.get(5)+"\t"); outputArea.append(" "+"Added Successfully!"); } } } private void exitBtnActionPerformed(java.awt.event.ActionEvent evt) { //exit program int result = JOptionPane.showConfirmDialog(null,"Would you like to exit the Waitlist?", "Exit?", JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { //exit waitlist without closing the whole program dispose();} } private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) { // if array is empty or index is out of bounds, removal is not possible if (nameList.isEmpty()){ JOptionPane.showMessageDialog(null,"No names to remove.", "Error", JOptionPane.ERROR_MESSAGE); } else{ //Get Info String firstName = JOptionPane.showInputDialog(null, "Please enter your first name.", "Information Required", JOptionPane.INFORMATION_MESSAGE); if (firstName.isEmpty()) { }else{ String lastName = JOptionPane.showInputDialog(null, "Please enter your last name.", "Information Required", JOptionPane.INFORMATION_MESSAGE); if (firstName.isEmpty()) { }else{ } } } } public void checkFile(){ try{ if(nameSave.exists()==false){ nameSave.createNewFile(); } }catch(IOException e){ JOptionPane.showMessageDialog(rootPane, "File not found"); e.printStackTrace(); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* 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(Waitlist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Waitlist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Waitlist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Waitlist.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 Waitlist().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton addBtn; private javax.swing.JButton exitBtn; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea outputArea; private javax.swing.JButton removeBtn; private javax.swing.JLabel titleLbl; // End of variables declaration }
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date: