Witam ponownie!
Słuchajcie zmagam się z kolejnym problemem, a mianowicie zapis tekstu z JTextAre do pliku tekstowego.
Za każdym razem zapisując do pliku aplikacja zapisuje pierwszą linijkę tekstu a kolejnych już nie.
Screen w załącznikach
Nie przeraźcie się importami itp. to GUI Builder Netbeansa tak szarżuje
a kod tutaj:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package menutestowe;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import static java.time.Clock.system;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author jarek
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
setResizable(false);
setLocation(500, 200);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
notatnik = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
menuPlik = new javax.swing.JMenu();
mOtworz = new javax.swing.JMenuItem();
mZapisz = new javax.swing.JMenuItem();
mWyjscie = new javax.swing.JMenuItem();
menuOpcje = new javax.swing.JMenu();
mNarz1 = new javax.swing.JMenuItem();
mNarz2 = new javax.swing.JMenuItem();
chOpcja1 = new javax.swing.JCheckBoxMenuItem();
menuAbout = new javax.swing.JMenu();
mAbout = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setEnabled(false);
notatnik.setColumns(20);
notatnik.setRows(5);
jScrollPane1.setViewportView(notatnik);
menuPlik.setText("Plik");
mOtworz.setText("Otwórz");
mOtworz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mOtworzActionPerformed(evt);
}
});
menuPlik.add(mOtworz);
mZapisz.setText("Zapisz");
mZapisz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mZapiszActionPerformed(evt);
}
});
menuPlik.add(mZapisz);
menuPlik.addSeparator();
mWyjscie.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
mWyjscie.setText("Wyjdź");
mWyjscie.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mWyjscieActionPerformed(evt);
}
});
menuPlik.add(mWyjscie);
jMenuBar1.add(menuPlik);
menuOpcje.setText("Opcje");
mNarz1.setText("Narzędzie 1");
menuOpcje.add(mNarz1);
mNarz2.setText("Narzędzie 2");
menuOpcje.add(mNarz2);
chOpcja1.setSelected(true);
chOpcja1.setText("Opcja1");
chOpcja1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chOpcja1ActionPerformed(evt);
}
});
menuOpcje.add(chOpcja1);
jMenuBar1.add(menuOpcje);
jMenuBar1.add(Box.createHorizontalGlue());
menuAbout.setText("O Programie");
mAbout.setText("O Programie");
mAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mAboutActionPerformed(evt);
}
});
menuAbout.add(mAbout);
jMenuBar1.add(menuAbout);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(149, 149, 149)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(151, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(108, 108, 108)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(119, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void mAboutActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, "Program testowy \n Wersja 1.0");
}
private void mOtworzActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File plik = fc.getSelectedFile();
try
{
//JOptionPane.showMessageDialog(null, "Zapisany plik to: " + plik);
Scanner skaner = new Scanner(plik);
while (skaner.hasNextLine())
{
notatnik.append(skaner.nextLine() + "\n");
}
} catch (FileNotFoundException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void mZapiszActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
{
File plik = fc.getSelectedFile();
try
{
PrintWriter pw = new PrintWriter(plik);
Scanner skaner = new Scanner(notatnik.getText());
while (skaner.hasNextLine())
{
pw.println(skaner.nextLine() + "\n");
pw.close();
}
}
catch (FileNotFoundException ex)
{
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void mWyjscieActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void chOpcja1ActionPerformed(java.awt.event.ActionEvent evt) {
if (chOpcja1.isSelected())
{
mNarz1.setEnabled(true);
}
else
{
mNarz1.setEnabled(false);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JCheckBoxMenuItem chOpcja1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JMenuItem mAbout;
private javax.swing.JMenuItem mNarz1;
private javax.swing.JMenuItem mNarz2;
private javax.swing.JMenuItem mOtworz;
private javax.swing.JMenuItem mWyjscie;
private javax.swing.JMenuItem mZapisz;
private javax.swing.JMenu menuAbout;
private javax.swing.JMenu menuOpcje;
private javax.swing.JMenu menuPlik;
private javax.swing.JTextArea notatnik;
// End of variables declaration
}
- zapis.png (454 KB) - ściągnięć: 111