I am trying to save the contents of a textarea that is on a panel inside a tabbed pane.
So far I have tried:
bw.write(tabbedPane.getComponent(tabbedPane.getSelectedIndex()).toString());
and have been looking through all the methods for tabbedpane and I can not seem to work it out. I know I must get the selected component from the tabbedPane
, then somehow get the textarea from that and then convert it to string I assume?
The code when I open a file is:
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
JTextArea x = new JTextArea();
JScrollPane scroll = new JScrollPane(x);
p.add(scroll, BorderLayout.CENTER);
x.read( new FileReader( file.getAbsolutePath() ), null );
File selectedFile = fileChooser.getSelectedFile();
String name = selectedFile.getName();
tabbedPane.add(p,name);
tabbedPane.setSelectedComponent(p);
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
I have added the class as you insisted Update Current SaveAs method:
private void btnSaveAsActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File dir1 = fileChooser.getCurrentDirectory();
String dir = dir1.getPath();
String name = fileChooser.getSelectedFile().getName() + ".txt";
try {
File file = new File(dir,name);
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
JPanel no = (JPanel) tabbedPane.getSelectedComponent();
JTextArea bo = (JTextArea) no.get
bw.write(bo.getText());
bw.close();
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), name);
} catch(Exception e) {
System.out.println(e);
}
}
}
current open file method:
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
FilePanel p = new FilePanel(file);
tabbedPane.add(p,p.getName());
tabbedPane.setSelectedComponent(p);
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
Do I need to add the new objects of the new class to an array or is it fine to just have them inserted to the tabbedPane?
There's no magic here, but instead it's all how you code it. Your class that holds the JTextArea should have a public method, something like getTextArea()
that returns the JTextArea. Then when you get the selected tab's component via getSelectedComponent()
, you call this method on the component returned.
Edit
Based on the code you've posted, you need to re-think your program design. Your JTextArea is a local variable and thus not easily accessible, and there-in lies your problem. I suggest:
getTextArea()
method that returns the JTextArea, have a getTextAreaText()
that just returns the text held by the JTextArea. The more limited your code is to outside perturbations and side effects, the better.
Edit 2
For example, you could create a class that holds the JTextArea in a JPanel, something like:
class FilePanel extends JPanel {
private File file;
private JTextArea textArea;
private String name;
public FilePanel(File file) throws FileNotFoundException, IOException {
this.file = file;
setLayout(new BorderLayout());
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
add(scroll, BorderLayout.CENTER);
textArea.read(new FileReader(file.getAbsolutePath()), null);
name = file.getName();
}
public File getFile() {
return file;
}
public JTextArea getTextArea() {
return textArea;
}
public String getName() {
return name;
}
}
And then whenever you get the selectedComponent from the JTextPane, make sure it's not null, cast it to FilePanel and call getTextArea() on it.
Edit 3
For example:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FooSwing extends JFrame {
private static final int PREF_W = 600;
private static final int PREF_H = 450;
private JFileChooser fileChooser = new JFileChooser();
private JTabbedPane tabbedPane = new JTabbedPane();
public FooSwing() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new AbstractAction("Open") {
@Override
public void actionPerformed(ActionEvent e) {
btnOpenActionPerformed(e);
}
}));
btnPanel.add(new JButton(new AbstractAction("Get Selected Text") {
@Override
public void actionPerformed(ActionEvent e) {
FilePanel selectedComp = (FilePanel)tabbedPane.getSelectedComponent();
if (selectedComp != null) {
String text = selectedComp.getTextArea().getText();
System.out.println(text);
} else {
System.out.println("No component selected");
}
}
}));
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
JPanel filePanel = new FilePanel(file);
tabbedPane.add(filePanel, filePanel.getName());
tabbedPane.setSelectedComponent(filePanel);
} catch (IOException ex) {
System.out.println("problem accessing file"
+ file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShow();
}
});
}
private static void createAndShow() {
FooSwing fooSwing = new FooSwing();
fooSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fooSwing.pack();
fooSwing.setLocationRelativeTo(null);
fooSwing.setVisible(true);
}
}
class FilePanel extends JPanel {
private File file;
private JTextArea textArea;
private String name;
public FilePanel(File file) throws FileNotFoundException, IOException {
this.file = file;
setLayout(new BorderLayout());
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
add(scroll, BorderLayout.CENTER);
textArea.read(new FileReader(file.getAbsolutePath()), null);
name = file.getName();
}
public File getFile() {
return file;
}
public JTextArea getTextArea() {
return textArea;
}
public String getName() {
return name;
}
}