How to update a Jtextarea with information from Jcheckboxes?

Asked

Viewed 435 times

2

I am in need of help in the following, I have a window that the user can activate the Jcheckboxes to write in the Jtextarea that is below, as the following image:

Frame do meu programa

What I wanted is that when you pressed the Jcheckbox of the Keyboard appeared a message saying "the keyboard is damaged" and if I removed the check of Jcheckbox not appear anything below, as for example:

inserir a descrição da imagem aqui

What I’ve been able to do is get Jcheckbox to write what I want, I just wanted to say that Jcheckbox s are not the only thing that I have in the program,I mean, in addition to this Jtextarea being shown the content of the Jcheckbox s actions is shown some notes that will be made in the next frame,

1-"the keyboard is damaged"
2-"the rat is damaged"
3-"the monitor is damaged"
4-"the tower is damaged"

5 and another for the details

All this is written to store in a document . txt.

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’s the code of this frame. I was doing the Jcheckbox experiments on the keyboard but I couldn’t. I really need help I can’t find anything anywhere.

  • sorry but apart from editing knows how to solve my problem ?

  • John, tried to pass the value of texto1 straight to the textArea1? Thus: textArea1.append(texto1);?

  • If you’re going to do as I said, the best thing would be to instantiate texto, and all its cargo, and the textArea1 at the beginning of class.

  • Not yet tried,I will try, thanks for the suggestion! If you can’t already I have an idea how I will do

  • I ended up taking your codes and creating a functional replica here in mine. I would have a few more points to take into account, so I’ll put an answer, @Joao.

1 answer

2

As I said, in the method listener the correct would be to add the value of texto1 in 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 from there. My solution was to replace Writer for BufferedWriter, for the BufferedWriter works as I want, in addition to having the functions append() and newLine(), where, respectively, they concatenate a string to the original value, and insert a line break in the code, thus leaving more or less 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 I could see the other way around was that he wrote on the file even if the selection was false, the checkbox was deselected. Then I put the whole part of the try...catch within an if, which checked the boolean value of the selection, thus:

if (Objeto.isSelected()){
    try {
        //Conteudo
    } catch (IOException e1) {
    }
}
  • thank you very much!

  • I had to see and put your suggestion the problem is that I agr it does not record as it should record

  • Hm... and how are you saving?

  • is not recording at all

  • Um... this is strange... The initiation of the components File file, String texto, and JTextArea textArea1 are just below the startup of the contentPane?

  • I’ll do the following I can edit my doubt to put the code I already have to show you?

  • Of course, but as long as you don’t run away from the original question.

Show 3 more comments

Browser other questions tagged

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