ola pessoal
eu sou novo aki e estou com um problema
eu estou criando um editor de textos
mas nao consigo pegar o texto do JTextArea
sera que algeum pode me ajudar
meu codigo :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package notepad;
/**
*
* @author Jonas
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Content extends JFrame implements ActionListener {
JTabbedPane jtp;
JOptionPane jopane;
JFileChooser fChooser;
JMenuBar menuBar;
JMenu file;
JMenuItem novo;
JMenuItem open;
JMenuItem save;
JMenuItem saveAs;
JScrollPane scrollPane;
JPanel panel;
String fileSaved;
boolean fileIsSaved;
public Content() {
fileIsSaved = false;
jopane = new JOptionPane();
fChooser = new JFileChooser();
menuBar = new JMenuBar();
file = new JMenu("File");
novo = new JMenuItem("New");
open = new JMenuItem("Open..");
save = new JMenuItem("Save");
saveAs = new JMenuItem("SaveAs..");
panel = new JPanel();
jtp = new JTabbedPane();
file.setFont(new Font("Verdana", 10, 10));
file.addActionListener(this);
novo.setFont(new Font("Verdana", 10, 10));
novo.addActionListener(this);
open.setFont(new Font("Verdana", 10, 10));
open.addActionListener(this);
save.setFont(new Font("Verdana", 10, 10));
save.addActionListener(this);
saveAs.setFont(new Font("Verdana", 10, 10));
saveAs.addActionListener(this);
if (!fileIsSaved) {
save.setEnabled(false);
}
file.insert(novo, 0);
file.insert(open, 1);
file.insertSeparator(2);
file.insert(save, 3);
file.insert(saveAs, 4);
file.insertSeparator(5);
menuBar.add(file);
this.setTitle("Jonhkr Editor");
this.setLayout(new BorderLayout());
this.add(menuBar, BorderLayout.NORTH);
this.add(jtp, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);
this.resize(1024, 700);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame window = new Content();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.show();
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj.equals(novo)) {
this.setTitle("Jonhkr Editor");
jtp.addTab("[untitled.txt]", new JScrollPane(new JTextArea()));
System.out.print(jtp);
}
if (obj.equals(open)) {
fChooser.showOpenDialog(this);
try {
String content = JFileWriter.open(fChooser.getSelectedFile());
this.setTitle("Jonhkr Editor");
jtp.addTab(fChooser.getSelectedFile().getName(), new JScrollPane(new JTextArea(content)));
} catch (IOException ex) {
System.out.print(ex.toString());
}
}
if (obj.equals(saveAs)) {
fChooser.setDialogTitle("Save as..");
fChooser.showSaveDialog(this);
System.out.print(fChooser.getSelectedFile());
if (fChooser.getSelectedFile().exists()) {
int messageDialog = JOptionPane.showConfirmDialog(fChooser, "O arquivo " + fChooser.getSelectedFile().getName() + " já existe! \n Deseja substituilo?");
if (messageDialog == 0) {
try {
JScrollPane JSP = (JScrollPane)jtp.getSelectedComponent();
JTextArea textArea = (JTextArea)JSP.getComponent(1);
JFileWriter.write(fChooser.getSelectedFile(), textArea.getText());
fileSaved = fChooser.getSelectedFile().toString();
fileIsSaved = true;
save.setEnabled(true);
} catch (Exception ex) {
System.out.print(ex.toString());
}
}
} else {
try {
JScrollPane JSP = (JScrollPane)jtp.getSelectedComponent();
JTextArea textArea = (JTextArea)JSP.getComponent(1);
JFileWriter.write(fChooser.getSelectedFile(), textArea.getText());
fileSaved = fChooser.getSelectedFile().toString();
fileIsSaved = true;
save.setEnabled(true);
} catch (Exception ex) {
System.out.print(ex.toString());
}
}
}
// if (obj.equals(save)) {
//
// try {
// JFileWriter.write(fileSaved, textArea.getText());
// fileIsSaved = true;
// save.setEnabled(true);
// } catch (Exception ex) {
// System.out.print(ex.toString());
// }
// }
}
/**
public static void write(String outputFile, String content) throws IOException {
File file = new File(outputFile);
FileWriter fileWrt = new FileWriter(file);
PrintWriter out = new PrintWriter(fileWrt);
out.append(content);
out.close();
}
public static void write(File outputFile, String content) throws IOException {
File file = new File(outputFile.toString());
FileWriter fileWrt = new FileWriter(file);
PrintWriter out = new PrintWriter(fileWrt);
out.append(content);
out.close();
}
public static void open(String inputFile) throws IOException {
File file = new File(inputFile);
FileWriter fileWrt = new FileWriter(file);
}
public static void open(File inputFile) throws IOException {
File file = new File(inputFile.toString());
FileReader fileRdr = new FileReader(file);
BufferedReader prt = new BufferedReader(fileRdr);
StringBuffer conteudo = new StringBuffer();
String line;
while((line = prt.readLine())!= null){
conteudo.append(line + "\n");
}
System.out.print(conteudo.toString());
}
**/
}
preciso pegar o texto digitado na JTextArea qeu esta aki:
jtp.addTab("[untitled.txt]", ));new JScrollPane(new JTextArea()
só qeu naum consigo
se puderem me ajudar agradeço…