How do I remove a band by its name?

Asked

Viewed 65 times

0

I’m not getting the bands inside the arraylist. For this, I read the.txt file where the information(name,country,nrointegrantes) is stored. I made a split to access the name, but when I try to remove one of the bands by the name I’m not getting.

I believe the path is something like this but I’m not succeeding.

The bands are displayed within a textarea(taSaida) and inform the band that I wish to remove within tfNomeBandaRemove.

I’ll post all the code down here:

Band file.txt

Rush,Canada,3
The Who,Inglaterra,4
The Beatles,Inglaterra,4
Guns,Eua,5

Banda Class

public class Banda {

    //Atributos
    private String nome;
    private String pais;
    private int nroIntegrantes;

    //construtor
    public Banda(String nome, String pais, int nroIntegrantes) {
        super();
        this.nome = nome;
        this.pais = pais;
        this.nroIntegrantes = nroIntegrantes;
    }

    //GET e SET
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getPais() {
        return pais;
    }

    public void setPais(String pais) {
        this.pais = pais;
    }

    public int getNroIntegrantes() {
        return nroIntegrantes;
    }

    public void setNroIntegrantes(int nroIntegrantes) {
        this.nroIntegrantes = nroIntegrantes;
    }





}

Main class

public class Exercicio1 {

    private JFrame frmBandas;
    ArrayList<Banda> bandas = new ArrayList<Banda>();
    private JTextField tfPaisBanda;
    private JTextField tfNomeBanda;
    private JPanel panel2;
    private JPanel panel1;
    private JTextField tfNroIntegrantes;
    private JTextField tfNomeBandaRemover;
    private JPanel panel3;
    private JPanel panel4;
    private JTextArea taSaida;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Exercicio1 window = new Exercicio1();
                    window.frmBandas.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Exercicio1() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmBandas = new JFrame();
        frmBandas.setTitle("BANDAS");
        frmBandas.setBounds(100, 100, 450, 300);
        frmBandas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmBandas.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel principal = new JPanel();
        frmBandas.getContentPane().add(principal);
        principal.setLayout(new CardLayout(0, 0));

        panel1 = new JPanel();
        principal.add(panel1, "name_47122994121324");
        panel1.setLayout(null);

        JLabel lblCadastroDeBandas = new JLabel("MENU BANDAS");
        lblCadastroDeBandas.setFont(new Font("Tahoma", Font.BOLD, 18));
        lblCadastroDeBandas.setBounds(134, 27, 163, 22);
        panel1.add(lblCadastroDeBandas);

        JButton btnCadastrarBanda = new JButton("Cadastrar Banda");
        btnCadastrarBanda.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //código MENU -> Cadastrar Banda

                panel1.setVisible(false);
                panel2.setVisible(true);
            }
        });
        btnCadastrarBanda.setBounds(138, 78, 137, 23);
        panel1.add(btnCadastrarBanda);

        JButton btnRemoverBanda = new JButton("Remover Banda");
        btnRemoverBanda.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //Código MENU -> Remover Banda
                panel1.setVisible(false);
                panel3.setVisible(true);
            }
        });
        btnRemoverBanda.setBounds(138, 112, 137, 23);
        panel1.add(btnRemoverBanda);

        JButton btnConsultarBandas = new JButton("Consultar Bandas");
        btnConsultarBandas.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //Código MENU -> Consultar Bandas
                panel1.setVisible(false);
                panel4.setVisible(true);
                taSaida.setText("");  //ASSIM só exibe o arquivo TXT uma única vez

                // LEITURA TXT

                try {

                    FileReader ler = new FileReader("Pasta/bandas.txt");
                    BufferedReader br = new BufferedReader(ler);

                    String nomeB = null;
                    String[] x;

                    while ((nomeB = br.readLine()) != null) {

                        x = nomeB.split(",");

                        for (int i = 0; i < x.length; i++) {

                            String paisB = (x[1]);

                            int nroIntegrantesb = Integer.parseInt(x[2]);   

                            //Objeto
                            bandas.add(new Banda(nomeB, paisB, nroIntegrantesb));
                        }

                        taSaida.append(nomeB + "\n");

                    }


                    //fechar
                    br.close();
                    ler.close();

                } catch (IOException e) {
                    // TODO: handle exception
                }
            }

        });
        btnConsultarBandas.setBounds(138, 146, 137, 23);
        panel1.add(btnConsultarBandas);

        JButton btnSair = new JButton("Sair");
        btnSair.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //SAIR
                System.exit(0);
            }
        });
        btnSair.setBounds(138, 180, 137, 23);
        panel1.add(btnSair);

        panel2 = new JPanel();
        principal.add(panel2, "name_47123033318588");
        panel2.setLayout(null);

        tfPaisBanda = new JTextField();
        tfPaisBanda.setColumns(10);
        tfPaisBanda.setBounds(219, 123, 128, 20);
        panel2.add(tfPaisBanda);

        tfNomeBanda = new JTextField();
        tfNomeBanda.setBounds(219, 84, 166, 20);
        panel2.add(tfNomeBanda);
        tfNomeBanda.setColumns(10);

        tfNroIntegrantes = new JTextField();
        tfNroIntegrantes.setColumns(10);
        tfNroIntegrantes.setBounds(219, 166, 128, 20);
        panel2.add(tfNroIntegrantes);

        JLabel lblCadastroDeBandas_1 = new JLabel("Cadastro de Bandas");
        lblCadastroDeBandas_1.setForeground(Color.RED);
        lblCadastroDeBandas_1.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblCadastroDeBandas_1.setBounds(129, 25, 195, 30);
        panel2.add(lblCadastroDeBandas_1);

        JButton btnCadastrar = new JButton("Cadastrar");
        btnCadastrar.addActionListener(new ActionListener() {
            private String nomeB;
            private String paisB;
            private int nroIntegrantesB;

            public void actionPerformed(ActionEvent arg0) {

                nomeB = tfNomeBanda.getText();
                paisB = tfPaisBanda.getText();
                nroIntegrantesB = Integer.parseInt(tfNroIntegrantes.getText());

                //Objeto Banda
                bandas.add(new Banda(nomeB, paisB, nroIntegrantesB));

                taSaida.append(nomeB + "," + paisB + "," + nroIntegrantesB + "\n");
                //Acho que aqui termina assim


                //Aqui SALVAR TXT
                File arquivo = new File("Pasta/bandas.txt");

                try {
                    arquivo.createNewFile();

                    FileWriter escrever = new FileWriter(arquivo, false);
                    BufferedWriter bw = new BufferedWriter(escrever);


                    bw.write(taSaida.getText());

                    //Fechar
                    bw.close();
                    escrever.close();


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //Aqui mostra msg de Cadastro Efetuado
                JOptionPane.showMessageDialog(null, "Banda Cadastrada com Sucesso!");
            }
        });
        btnCadastrar.setBounds(173, 211, 115, 23);
        panel2.add(btnCadastrar);

        JLabel lblNomeBanda = new JLabel("Nome Banda");
        lblNomeBanda.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblNomeBanda.setBounds(90, 85, 109, 14);
        panel2.add(lblNomeBanda);

        JLabel lblPasBanda = new JLabel("Pa\u00EDs Banda");
        lblPasBanda.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblPasBanda.setBounds(109, 124, 109, 14);
        panel2.add(lblPasBanda);

        JLabel lblNmerosIntegrantes = new JLabel("N\u00FAmeros Integrantes");
        lblNmerosIntegrantes.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblNmerosIntegrantes.setBounds(39, 167, 208, 19);
        panel2.add(lblNmerosIntegrantes);

        JButton btnCadastroMenuVoltar = new JButton("Menu");
        btnCadastroMenuVoltar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //Código Cadastro -> Menu
                panel2.setVisible(false);
                panel1.setVisible(true);
            }
        });
        btnCadastroMenuVoltar.setBounds(39, 211, 115, 23);
        panel2.add(btnCadastroMenuVoltar);

        JButton btnLimparCadastro = new JButton("Limpar");
        btnLimparCadastro.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //Código Limpar dados
                tfNomeBanda.setText("");
                tfPaisBanda.setText("");
                tfNroIntegrantes.setText("");
            }
        });
        btnLimparCadastro.setBounds(298, 211, 115, 23);
        panel2.add(btnLimparCadastro);

        panel3 = new JPanel();
        principal.add(panel3, "name_3607965415174");
        panel3.setLayout(null);

        JLabel lblNewLabel = new JLabel("Nome Banda Remover");
        lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 15));
        lblNewLabel.setBounds(44, 97, 173, 14);
        panel3.add(lblNewLabel);

        tfNomeBandaRemover = new JTextField();
        tfNomeBandaRemover.setBounds(211, 96, 197, 20);
        panel3.add(tfNomeBandaRemover);
        tfNomeBandaRemover.setColumns(10);

        JLabel lblRemoverBanda = new JLabel("Remover Banda");
        lblRemoverBanda.setForeground(Color.RED);
        lblRemoverBanda.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblRemoverBanda.setBounds(131, 11, 195, 30);
        panel3.add(lblRemoverBanda);

        JButton btnRemoverMenuVoltar = new JButton("Menu");
        btnRemoverMenuVoltar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //Código Remover -> Menu
                panel3.setVisible(false);
                panel1.setVisible(true);
            }
        });
        btnRemoverMenuVoltar.setBounds(29, 202, 115, 23);
        panel3.add(btnRemoverMenuVoltar);

        JButton btnRemover = new JButton("Remover");
        btnRemover.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent arg0) {


                // LEITURA TXT

                try {

                    FileReader ler = new FileReader("Pasta/bandas.txt");
                    BufferedReader br = new BufferedReader(ler);

                    String nomeB = null;
                    String[] x;

                    while ((nomeB = br.readLine()) != null) {

                        x = nomeB.split(",");

                        for (int i = 0; i < x.length; i++) {

                            String paisB = (x[1]);

                            int nroIntegrantesb = Integer.parseInt(x[2]);   

                            //Objeto
                            bandas.add(new Banda(nomeB, paisB, nroIntegrantesb));
                        }

                        taSaida.append(nomeB + "\n");

                    }

                    //fechar
                    br.close();
                    ler.close();

                } catch (IOException e) {
                    // TODO: handle exception
                }

                String[] linha = bandas.get(0).getNome().split(",");
                System.out.println(linha[0]); // AQUI MOSTRA SÓ RUSH(nome banda) posicao ZERO

                //Código Remover Banda              
                for (int i = 0; i < bandas.size(); i++) {   

                    if (bandas.get(i).getNome().equals(tfNomeBandaRemover.getText())) {
                        bandas.remove(i);
                        JOptionPane.showMessageDialog(null, "Banda Removida com Sucesso!");
                    }

                }  
            }
        });
        btnRemover.setBounds(169, 202, 115, 23);
        panel3.add(btnRemover);

        JButton btnLimparRemover = new JButton("Limpar");
        btnLimparRemover.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //código LIMPAR
                tfNomeBandaRemover.setText("");
            }
        });
        btnLimparRemover.setBounds(294, 202, 115, 23);
        panel3.add(btnLimparRemover);

        panel4 = new JPanel();
        principal.add(panel4, "name_4277677604689");
        panel4.setLayout(null);

        JLabel lblConsultarBandas = new JLabel("Consultar Bandas");
        lblConsultarBandas.setBounds(146, 5, 142, 20);
        lblConsultarBandas.setForeground(Color.RED);
        lblConsultarBandas.setFont(new Font("Tahoma", Font.BOLD, 16));
        panel4.add(lblConsultarBandas);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(32, 29, 379, 179);
        panel4.add(scrollPane);

        taSaida = new JTextArea();
        scrollPane.setViewportView(taSaida);

        JButton btnConsultaMenuVoltar = new JButton("Menu");
        btnConsultaMenuVoltar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //código Consulta -> Menu
                panel4.setVisible(false);
                panel1.setVisible(true);
            }
        });
        btnConsultaMenuVoltar.setBounds(188, 219, 115, 23);
        panel4.add(btnConsultaMenuVoltar);
    }
}
  • This code is not executable, lack Band class.

  • Good night Articuno I made her, already put together here.

  • It is a sample of who in the file bands.txt too, but this code does not even compile.

  • Now I think it’s complete.

2 answers

3

The main problem of your code does not remove is that you just remove the band from the list, but nothing does in the file data, that where the query is searching and populating the text field. However there are several problems in this code that may hinder until the correction of this method, but I will only list to you to check and correct.

  • improve the organization of your code, everything that needs to be repeated many times in the code can be simplified by turning into a method (if it is a set of actions) or a variable of broader scope, if it is a recurring assignment. You read a file path several times, and if you need to change it, you will have to go hunting in the entire code. Create a class scope variable and put the path there, then just invoke that variable when you need to access the information. Since it is a data that does not need to be changed during execution, you can leave as final. Example:
private final String  FILE_PATH = "Pasta\bandas.txt";
  • When you register a band, you are declaring variables as private, and their scope is only within the Listener, this is not necessary since you are using them as a local variable, just start the variables within the method, no need to go out putting modifier everywhere, just where it is really needed.

  • Another problem here is that you save what is displayed on JTextArea, then what’s the use of creating a class called Banda? There is a problem of understanding the concepts of language here, the class Banda is completely useless in this code as it was done, just read the file and remove the line corresponding to the searched name. I suggest that, first of all, follow the orientation of the above topic and organize the code, separating specific actions into methods such as "register band" and "save band", and then just use these actions. If the goal is always to display the updated file, first record it and then display its contents.

  • In your code you create 3 panels in a giant block of code, which again hints at my first topic. I recommend that you separate the panels, your code is being done as if it were structured programming and losing the chance to take advantage of the best that java provides you to better organize your code. Before writing code, it is necessary to understand what is being done, to organize the ideas in order to create something that is, first of all, readable and well structured.

Topics I recommend for reading:

  • Thanks for the tips Articuno, I will read calmly each of the links you passed above.

0

Friends I was able to solve the problem with the help of a colleague, because I was reading and writing the.txt file in the wrong places, follow below the new code if someone goes through a similar doubt.

public class Exercicio1 {

private JFrame frmBandas;
ArrayList<Banda> bandas = new ArrayList<Banda>();
private JTextField tfPaisBanda;
private JTextField tfNomeBanda;
private JPanel panel2;
private JPanel panel1;
private JTextField tfNroIntegrantes;
private JTextField tfNomeBandaRemover;
private JPanel panel3;
private JPanel panel4;
private JTextArea taSaida;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Exercicio1 window = new Exercicio1();
                window.frmBandas.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Exercicio1() {
    initialize();
}





/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    //LER TXT
    File arquivo = new File("Pasta/bandas.txt");

    try {
        arquivo.createNewFile();

        FileReader ler = new FileReader(arquivo);
        BufferedReader br = new BufferedReader(ler);

        String linha = null;

        while ((linha = br.readLine()) != null) {

            String[] x = linha.split(",");

            bandas.add(new Banda(x[0], x[1], Integer.parseInt(x[2])));
        }

        //FECHAR
        br.close();
        ler.close();

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }



    frmBandas = new JFrame();
    frmBandas.setTitle("BANDAS");
    frmBandas.setBounds(100, 100, 450, 312);
    frmBandas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmBandas.getContentPane().setLayout(new BorderLayout(0, 0));

    JPanel principal = new JPanel();
    frmBandas.getContentPane().add(principal);
    principal.setLayout(new CardLayout(0, 0));

    panel1 = new JPanel();
    principal.add(panel1, "name_47122994121324");
    panel1.setLayout(null);

    JLabel lblCadastroDeBandas = new JLabel("MENU BANDAS");
    lblCadastroDeBandas.setFont(new Font("Tahoma", Font.BOLD, 18));
    lblCadastroDeBandas.setBounds(134, 27, 163, 22);
    panel1.add(lblCadastroDeBandas);

    JButton btnCadastrarBanda = new JButton("Cadastrar Banda");
    btnCadastrarBanda.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //código MENU -> Cadastrar Banda

            panel1.setVisible(false);
            panel2.setVisible(true);
        }
    });
    btnCadastrarBanda.setBounds(138, 78, 137, 23);
    panel1.add(btnCadastrarBanda);

    JButton btnRemoverBanda = new JButton("Remover Banda");
    btnRemoverBanda.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //Código MENU -> Remover Banda
            panel1.setVisible(false);
            panel3.setVisible(true);
        }
    });
    btnRemoverBanda.setBounds(138, 112, 137, 23);
    panel1.add(btnRemoverBanda);

    JButton btnConsultarBandas = new JButton("Consultar Bandas");
    btnConsultarBandas.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //Código MENU -> Consultar Bandas
            panel1.setVisible(false);
            panel4.setVisible(true);
            taSaida.setText("");  //ASSIM só exibe o arquivo TXT uma única vez

            for (int i = 0; i < bandas.size(); i++) {
                taSaida.append("Nome: " + bandas.get(i).getNome() + " País: "+bandas.get(i).getPais()+ 
                        " Nro Integrantes: "+bandas.get(i).getNroIntegrantes() + "\n");
            }
        }

    });
    btnConsultarBandas.setBounds(138, 146, 137, 23);
    panel1.add(btnConsultarBandas);

    JButton btnSair = new JButton("Sair");
    btnSair.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //SALVAR TXT
            File arquivo = new File("Pasta/bandas.txt");

            try {
                arquivo.createNewFile();

                FileWriter escrever = new FileWriter(arquivo, false);
                BufferedWriter bw = new BufferedWriter(escrever);

                for (int i = 0; i < bandas.size(); i++) {
                    bw.write(bandas.get(i).getNome() + "," + bandas.get(i).getPais() + "," +
                            String.valueOf(bandas.get(i).getNroIntegrantes()) + "\n");
                }

                //FECHAR
                bw.close();
                escrever.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //SAIR
            System.exit(0);
        }
    });
    btnSair.setBounds(138, 180, 137, 23);
    panel1.add(btnSair);

    panel2 = new JPanel();
    principal.add(panel2, "name_47123033318588");
    panel2.setLayout(null);

    tfPaisBanda = new JTextField();
    tfPaisBanda.setColumns(10);
    tfPaisBanda.setBounds(219, 123, 128, 20);
    panel2.add(tfPaisBanda);

    tfNomeBanda = new JTextField();
    tfNomeBanda.setBounds(219, 84, 166, 20);
    panel2.add(tfNomeBanda);
    tfNomeBanda.setColumns(10);

    tfNroIntegrantes = new JTextField();
    tfNroIntegrantes.setColumns(10);
    tfNroIntegrantes.setBounds(219, 166, 128, 20);
    panel2.add(tfNroIntegrantes);

    JLabel lblCadastroDeBandas_1 = new JLabel("Cadastro de Bandas");
    lblCadastroDeBandas_1.setForeground(Color.RED);
    lblCadastroDeBandas_1.setFont(new Font("Tahoma", Font.BOLD, 16));
    lblCadastroDeBandas_1.setBounds(129, 25, 195, 30);
    panel2.add(lblCadastroDeBandas_1);

    JButton btnCadastrar = new JButton("Cadastrar");
    btnCadastrar.addActionListener(new ActionListener() {
        private String nomeB;
        private String paisB;
        private int nroIntegrantesB;

        public void actionPerformed(ActionEvent arg0) {

            nomeB = tfNomeBanda.getText();
            paisB = tfPaisBanda.getText();
            nroIntegrantesB = Integer.parseInt(tfNroIntegrantes.getText());

            //Objeto Banda
            bandas.add(new Banda(nomeB, paisB, nroIntegrantesB));


            //Aqui mostra msg de Cadastro Efetuado
            JOptionPane.showMessageDialog(null, "Banda Cadastrada com Sucesso!");
        }
    });
    btnCadastrar.setBounds(173, 211, 115, 23);
    panel2.add(btnCadastrar);

    JLabel lblNomeBanda = new JLabel("Nome Banda");
    lblNomeBanda.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblNomeBanda.setBounds(90, 85, 109, 14);
    panel2.add(lblNomeBanda);

    JLabel lblPasBanda = new JLabel("Pa\u00EDs Banda");
    lblPasBanda.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblPasBanda.setBounds(109, 124, 109, 14);
    panel2.add(lblPasBanda);

    JLabel lblNmerosIntegrantes = new JLabel("N\u00FAmeros Integrantes");
    lblNmerosIntegrantes.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblNmerosIntegrantes.setBounds(39, 167, 208, 19);
    panel2.add(lblNmerosIntegrantes);

    JButton btnCadastroMenuVoltar = new JButton("Menu");
    btnCadastroMenuVoltar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //Código Cadastro -> Menu
            panel2.setVisible(false);
            panel1.setVisible(true);
        }
    });
    btnCadastroMenuVoltar.setBounds(39, 211, 115, 23);
    panel2.add(btnCadastroMenuVoltar);

    JButton btnLimparCadastro = new JButton("Limpar");
    btnLimparCadastro.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //Código Limpar dados
            tfNomeBanda.setText("");
            tfPaisBanda.setText("");
            tfNroIntegrantes.setText("");
        }
    });
    btnLimparCadastro.setBounds(298, 211, 115, 23);
    panel2.add(btnLimparCadastro);

    panel3 = new JPanel();
    principal.add(panel3, "name_3607965415174");
    panel3.setLayout(null);

    JLabel lblNewLabel = new JLabel("Nome Banda Remover");
    lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 15));
    lblNewLabel.setBounds(44, 97, 173, 14);
    panel3.add(lblNewLabel);

    tfNomeBandaRemover = new JTextField();
    tfNomeBandaRemover.setBounds(211, 96, 197, 20);
    panel3.add(tfNomeBandaRemover);
    tfNomeBandaRemover.setColumns(10);

    JLabel lblRemoverBanda = new JLabel("Remover Banda");
    lblRemoverBanda.setForeground(Color.RED);
    lblRemoverBanda.setFont(new Font("Tahoma", Font.BOLD, 16));
    lblRemoverBanda.setBounds(131, 11, 195, 30);
    panel3.add(lblRemoverBanda);

    JButton btnRemoverMenuVoltar = new JButton("Menu");
    btnRemoverMenuVoltar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //Código Remover -> Menu
            panel3.setVisible(false);
            panel1.setVisible(true);
        }
    });
    btnRemoverMenuVoltar.setBounds(29, 202, 115, 23);
    panel3.add(btnRemoverMenuVoltar);

    JButton btnRemover = new JButton("Remover");
    btnRemover.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent arg0) {

            //Código Remover Banda
            for (int i = 0; i < bandas.size(); i++) {

                if (bandas.get(i).getNome().equals(tfNomeBandaRemover.getText())) {
                    bandas.remove(i);

                    //Aqui mostra msg de Remoção Efetuada
                    JOptionPane.showMessageDialog(null, "Banda Removida com Sucesso!");
                }
            }


        }
    });
    btnRemover.setBounds(169, 202, 115, 23);
    panel3.add(btnRemover);

    JButton btnLimparRemover = new JButton("Limpar");
    btnLimparRemover.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //código LIMPAR
            tfNomeBandaRemover.setText("");
        }
    });
    btnLimparRemover.setBounds(294, 202, 115, 23);
    panel3.add(btnLimparRemover);

    panel4 = new JPanel();
    principal.add(panel4, "name_4277677604689");
    panel4.setLayout(null);

    JLabel lblConsultarBandas = new JLabel("Consultar Bandas");
    lblConsultarBandas.setBounds(146, 5, 142, 20);
    lblConsultarBandas.setForeground(Color.RED);
    lblConsultarBandas.setFont(new Font("Tahoma", Font.BOLD, 16));
    panel4.add(lblConsultarBandas);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(32, 29, 379, 179);
    panel4.add(scrollPane);

    taSaida = new JTextArea();
    scrollPane.setViewportView(taSaida);

    JButton btnConsultaMenuVoltar = new JButton("Menu");
    btnConsultaMenuVoltar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //código Consulta -> Menu
            panel4.setVisible(false);
            panel1.setVisible(true);
        }
    });
    btnConsultaMenuVoltar.setBounds(188, 219, 115, 23);
    panel4.add(btnConsultaMenuVoltar);

    JMenuBar menuBar = new JMenuBar();
    frmBandas.getContentPane().add(menuBar, BorderLayout.NORTH);

    JMenu mnNewMenu = new JMenu("Arquivo");
    menuBar.add(mnNewMenu);

    JMenuItem mntmCadastrar = new JMenuItem("Cadastrar");
    mntmCadastrar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel1.setVisible(false);
            panel2.setVisible(true);
            panel3.setVisible(false);
            panel4.setVisible(false);

        }
    });
    mnNewMenu.add(mntmCadastrar);

    JMenuItem mntmRemover = new JMenuItem("Remover");
    mntmRemover.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel1.setVisible(false);
            panel2.setVisible(false);
            panel3.setVisible(true);
            panel4.setVisible(false);

        }
    });
    mnNewMenu.add(mntmRemover);

    JMenuItem mntmConsultar = new JMenuItem("Consultar");
    mntmConsultar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel1.setVisible(false);
            panel2.setVisible(false);
            panel3.setVisible(false);
            panel4.setVisible(true);
        }
    });
    mnNewMenu.add(mntmConsultar);

    JMenuItem mntmMenu = new JMenuItem("Menu");
    mntmMenu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel1.setVisible(true); 
            panel2.setVisible(false);
            panel3.setVisible(false);
            panel4.setVisible(false);
        }
    });
    mnNewMenu.add(mntmMenu);

    JMenuItem mntmSair = new JMenuItem("Sair");
    mntmSair.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //SALVAR TXT
            File arquivo = new File("Pasta/bandas.txt");

            try {
                arquivo.createNewFile();

                FileWriter escrever = new FileWriter(arquivo, false);
                BufferedWriter bw = new BufferedWriter(escrever);

                for (int i = 0; i < bandas.size(); i++) {
                    bw.write(bandas.get(i).getNome() + "," + bandas.get(i).getPais() + "," +
                            String.valueOf(bandas.get(i).getNroIntegrantes()) + "\n");
                }

                //FECHAR
                bw.close();
                escrever.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //SAIR
            System.exit(0);
        }
    });
    mnNewMenu.add(mntmSair);
 }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.