Sam Kohnson
Sam Kohnson

Reputation: 115

Getting textfile in a combobox

I haven't used comboBox much in java before and I'm having a bit of trouble getting my textfile to appear. I believe I have the files load correctly but it seems like I'm finding difficult to implement it in the code. I do have multiple movie names in the textfile. and when selecting a different movie in the combo box it changes the price,rating etc...

I did this correctly once using an initialize array.

example of the textfile[Taken,PG-13,8:00Am, 7,50

import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;

import javax.swing.*;

public class MovieSelection extends JFrame {
private JPanel ratingPanel;
private JPanel panel;
private JLabel priceLabel;
private JLabel label;
private JButton addCart;    
private JButton backButton;
private JButton resetButton;
private JTextField selectedRatingPanel;
private JTextField amountTextField;
private JComboBox movieBox;

private ArrayList<String> movieName;
private ArrayList<String> movieRating;
private ArrayList<String> movieTime;
private ArrayList<String> moviePrice;

public MovieSelection() {
    super("Please select your movie");
    setSize(575,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    buildMoviePanel();
    buildRatingPanel();
    add(panel);
    setVisible(true);
    movieName = new ArrayList<>();
    movieRating = new ArrayList<>();
    movieTime = new ArrayList<>();
    moviePrice = new ArrayList<>();



}
private void readMovies() {
    Scanner input=null;

try{

     input  = new Scanner(new File("TheMovies.txt"));

     while(input.hasNext()){

        String str = input.nextLine();
        StringTokenizer strT = new StringTokenizer(str, ",");

        movieName.add(strT.nextToken());
        movieRating.add(strT.nextToken());
        moviePrice.add(strT.nextToken());
        movieTime.add(strT.nextToken());


    }

}

catch(Exception element){
    input.close();
    JOptionPane.showMessageDialog(null, "Error");
}


}
private void buildMoviePanel() {

    panel = new JPanel();
    priceLabel = new JLabel("Cost:");
    backButton = new JButton("Back");
    resetButton = new JButton("Rest");

    backButton.addActionListener(new BackButton());
    resetButton.addActionListener(new ResetButton());

    addCart = new JButton("Add to cart");

    JTextField totalTextField = new JTextField(10);
    JTextField priceTextField = new JTextField(5);
    JTextField amountTextField =  new JTextField(4);
    priceTextField.setEditable(false);
    priceTextField.setText(moviePrice);

    totalTextField.setEditable(false);


    JComboBox movieLists = new JComboBox(movieName);



    movieLists.setSelectedIndex(0);
    movieLists.addActionListener(new MovieLists());

    panel.add(movieLists).setBounds(20,52,80,40);
    panel.add(priceLabel).setBounds(375,0,80,40);

    panel.add(priceTextField).setBounds(375,52,75,40);
    panel.add(backButton).setBounds(20,310,80,40);
    panel.add(addCart).setBounds(380,310,100,40);
    panel.add(resetButton).setBounds(200, 310, 80, 40);
    panel.add(amountTextField);

    panel.setLayout(null);
}//buildPanel


private void buildRatingPanel(){
    ratingPanel = new JPanel();
    label = new JLabel("Rating:");
    selectedRatingPanel =  new JTextField(9);
    selectedRatingPanel.setEditable(false);
    selectedRatingPanel.setText("R");

    panel.add(label).setBounds(245, 0, 100,40);

    panel.add(selectedRatingPanel).setBounds(245,52,100,40);
}

private class MovieLists implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        JComboBox cb = (JComboBox) e.getSource();
        String theMovie = (String) cb.getSelectedItem();

        System.out.println(cb.getSelectedIndex());


    }

}
    private class BackButton implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // return back to home page
            if (e.getSource() == backButton)
                new SelectUserWindow();
                setVisible(false);

        }
    }

    private class ResetButton implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // return back to home page
            if (e.getSource() == resetButton);


        }
    }
}

Upvotes: 0

Views: 821

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

  1. Learn to use layout managers
  2. JComboBox does not take an ArrayList (or Collection) as a viable parameter (it can take Object[] or Vector)
  3. movieName, movieRating, movieTime, moviePrice are all uninitialised when you create the UI because you initialise them AFTER the creation of the UI.
  4. JTextField#setText does not take an ArrayList as a viable parameter
  5. Learn to read the output of your application from the console or IDE
  6. Learn to use a debugger - it will save you many hours of frustration and annoyance ;)

Upvotes: 1

Related Questions