Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Somethink with ArrayList

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Somethink with ArrayList

    Hello guys. I started learning Java with book for beginners ("Head First Java"). And now I have a problem with code from one task. My compilation program show me that problem with ArrayList (later QuizCard and cardList everywhere). I don't know how fix it. Help me. Thanks.

     
     
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
     
    public class QuizCardBuilder {
     
    	private JTextArea question;
    	private JTextArea answer;
    	private ArrayList<QuizCard> cardList;
    	private JFrame frame;
     
      public static void main(String[] args) {
    	  QuizCardBuilder builder = new QuizCardBuilder();
    	  builder.go();
      }
     
      public void go() {
     
    	  frame = new JFrame("Quiz Card Builder");
    	  JPanel mainPanel = new JPanel();
    	  Font bigFont = new Font("sanserif", Font.BOLD, 24);
    	  question = new JTextArea(6,20);
    	  question.setLineWrap(true);
    	  question.setWrapStyleWord(true);
    	  question.setFont(bigFont);
     
    	  JScrollPane qScroller = new JScrollPane(question);
    	  qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    	  qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     
    	  answer = new JTextArea(6,20);
    	  answer.setLineWrap(true);
    	  answer.setWrapStyleWord(true);
    	  answer.setFont(bigFont);
     
    	  JScrollPane aScroller = new JScrollPane(answer);
    	  aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    	  aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     
    	  JButton nextButton = new JButton("Next Card");
     
    	  cardList = new ArrayList<QuizCard>();
     
    	  JLabel qLabel = new JLabel("Question:");
    	  JLabel aLabel = new JLabel("Answer:");
     
    	  mainPanel.add(qLabel);
    	  mainPanel.add(qScroller);
    	  mainPanel.add(aLabel);
    	  mainPanel.add(aScroller);
    	  mainPanel.add(nextButton);
    	  nextButton.addActionListener(new NextCardListener());
    	  JMenuBar menuBar = new JMenuBar();
    	  JMenu fileMenu = new JMenu("File");
    	  JMenuItem newMenuItem = new JMenuItem("New");
    	  JMenuItem saveMenuItem = new JMenuItem("Save");
    	  newMenuItem.addActionListener(new NewMenuListener());
     
    	  saveMenuItem.addActionListener(new SaveMenuListener());
    	  fileMenu.add(newMenuItem);
    	  fileMenu.add(saveMenuItem);
    	  menuBar.add(fileMenu);
     
          frame.setJMenuBar(menuBar);
          frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    	  frame.setSize(500,600);
    	  frame.setVisible(true);
      }
     
      public class NextCardListener implements ActionListener {
    	public void actionPerformed(ActionEvent ev) {
    		QuizCard card = new QuizCard(question.getText(), answer.getText());
    		cardList.add(card);
    		clearCard();
     
    	}
      }
     
      public class SaveMenuListener implements ActionListener {
    	public void actionPerformed(ActionEvent ev) {
    		QuizCard card = new QuizCard(question.getText(), answer.getText());
    		cardList.add(card);
     
    		JFileChooser fileSave = new JFileChooser();
    		fileSave.showSaveDialog(frame);
    		saveFile(fileSave.getSelectedFile());
     
    	}
      }
     
      public class NewMenuListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    			cardList.clear();
    			clearCard();
     
    		}
    	  }
     
      private void clearCard() {
    	  question.setText(" ");
    	  answer.setText(" ");
    	  question.requestFocus();
      }
     
      private void saveFile(File file) {
    	  try {
    		  BufferedWriter writer = new BufferedWriter(new FileWriter(file));
     
    		  for(QuizCard card:cardList) {
    			  writer.write(card.getQuestion()+"/");
    			  writer.write(card.getAnswer()+"\n");
    		  }
    		  writer.close();
    	  } catch(IOException ex) {
    		  System.out.println("couldn't write the cardList out");
    		  ex.printStackTrace();
    	  }
      }
     
     
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Somethink with ArrayList

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    If you're asking for help with an error, please post the entire text of the error message.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM