How to update a JTextArea with JCheckBoxes information?

2

I'm in need of help on the following, I have a window that the user can enable the JCheckBoxes to write in the JTextArea that lies below, like the following image:

WhatIwantedisthatwhenIpressedtheJCheckBoxonthekeyboarditwouldappearamessagesaying"the keyboard is corrupted" and if I did not check the JCheckBox, nothing appeared below, such as:

WhatI'vealreadybeenabletodowasbyJCheckBoxwritingwhatIwant,butIwantittoupdateandwhenIpresstheJCheckBoxitappearsinJTextAreaandwhenItakethe"right" out of JCheckBox it deletes what had been written that was "the keyboard is damaged" just wanted to say that the JCheckBox's are not the only thing I have in the program, ie in addition to this JTextArea be shown the content of the JCheckBox's actions are shown some notes that will be made in the frame next, that is, I wanted a line to be

  

1- "The keyboard is damaged"
  2- "The mouse is damaged"
  3- "The monitor is damaged"
  4- "The tower is damaged"

     

5 and another for the details

This is all written to save in a .txt document.

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                Aluno7_Ruben frame = new Aluno7_Ruben();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Aluno7_Ruben() {
    setIconImage(Toolkit.getDefaultToolkit().getImage("C:\Users\Jo\u00E3o Gil\workspace\sala de aula\pic\icon\Science-Classroom-icon.png"));

    setTitle("PC7-Ruben Gato");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Detalhes/Notas:");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
    lblNewLabel.setBounds(10, 106, 194, 23);
    contentPane.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Localiza\u00E7\u00E3o da avaria:");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 17));
    lblNewLabel_1.setBounds(10, 11, 169, 20);
    contentPane.add(lblNewLabel_1);

    JCheckBox Teclado = new JCheckBox ("Teclado");
    Teclado.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            PrintWriter writer;
            try {
                String texto1="O teclado esta danificado";
                writer = new PrintWriter("C:\Users\João Gil\workspace\sala de aula\pic\notas.txt");
                writer.println(texto1 );

                writer.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();

            }

        }
    });
    Teclado.setToolTipText("Selecionar caso esteja danificado");
    Teclado.setBounds(6, 54, 94, 23);
    contentPane.add(Teclado);

    JCheckBox  Rato = new JCheckBox ("Rato");
    Rato.setToolTipText("Selecionar caso esteja danificado");
    Rato.setBounds(98, 54, 81, 23);
    contentPane.add(Rato);

    JCheckBox  Monitor = new JCheckBox ("Monitor");
    Monitor.setToolTipText("Selecionar caso esteja danificado");
    Monitor.setBounds(190, 54, 96, 23);
    contentPane.add(Monitor);

    JCheckBox Torre = new JCheckBox ("Torre");
    Torre.setToolTipText("Selecionar caso esteja danificado");
    Torre.setBounds(288, 54, 109, 23);
    contentPane.add(Torre);

    JButton Adicionar_notas = new JButton("Escrever novas notas");
    Adicionar_notas.addActionListener(new ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent evt) {
            FAZJA(evt);
        }
    });

    Adicionar_notas.setBounds(137, 224, 159, 23);
    contentPane.add(Adicionar_notas);

    File file = new File("C:\Users\João Gil\workspace\sala de aula\pic\notas.txt");
    FileInputStream fis = null;
    String texto = "";

    try {
        fis = new FileInputStream(file);
        int content;
        while ((content = fis.read()) != -1) {
            texto += (char) content;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    JTextArea textArea1 = new JTextArea(texto);

    textArea1.setEditable(false);
    textArea1.setWrapStyleWord(true);
    textArea1.setBounds(10, 128, 414, 85);
    contentPane.add(textArea1); 
}

protected void FAZJA(ActionEvent evt) {
    this.dispose();
    new Gravação_Dados().setVisible(true);
}     

Here is the code for this frame. I was experimenting with JCheckBox from the keyboard but I could not. I really need help, I can not find anything anywhere.

    
asked by anonymous 24.11.2014 / 00:45

1 answer

2

As I said, in the listener method the correct would be to add the value of texto1 to textArea1 , getting something like this:

public void actionPerformed(ActionEvent e) {
    Writer writer;
        try {
            String texto1 = "O Teclado esta danificado";
            writer = new PrintWriter(
                "C:\Users\João Gil\workspace\sala de aula\pic\notas.txt");
            writer.println(texto1);
            textArea1.append(texto1+"\n");
            writer.close();
        } catch (IOException e1) {
        }
    }
}

I found a problem from here ... The Writer class was not adding, but replacing the value of the first line and did not put anything else thereafter. My solution found was to replace Writer with BufferedWriter , since BufferedWriter works as I want, besides having the functions append() and newLine() , where they respectively concatenate a string to the original value, and insert a line break in the code, getting more or less like the parts lacking changes:

BufferedWriter writer;
writer = new BufferedWriter(
    new FileWriter("C:\Users\João Gil\workspace\sala de aula\pic\notas.txt", true));

From here, it was working almost correctly. The only part he could see was that he wrote in the file even if the selection was false , the checkBox was deselected. So I put all of the try...catch inside an if, which checked the Boolean selection value, thus:

if (Objeto.isSelected()){
    try {
        //Conteudo
    } catch (IOException e1) {
    }
}
    
24.11.2014 / 14:27