Reputation: 460
i am making a text editor, and this is the basic version of my code. I used UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
to make the whole thing look like the platform I am using, but the JFileChooser save
is always the java look and feel. Can anybody help? I might be putting it in the wrong spot, but I don't know where
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
@SuppressWarnings("serial")
public class TextEditor extends JPanel {
static Container pane;
static Container paneText;
static BasicFrame frame;
static JTextArea textArea;
static JScrollPane areaScrollPane;
static FileFilter txtFile;
static JFileChooser save = new FileChooser(System.getProperty("user.home//documents"));
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException, IOException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
frame = BasicFrame.getInstance();
pane = frame.getContentPane();
paneText = new JPanel();
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
int hGap = 10;
int vGap = 20;
pane.setLayout(new FlowLayout(FlowLayout.LEFT, hGap, vGap));
Action SaveAs = new SaveAs("Save File", "Writes the text file");
JButton one = new JButton(SaveAs);
one.addActionListener(null);
txtFile = new FileNameExtensionFilter("Text File (.txt)", "txt");
save.addChoosableFileFilter(txtFile);
save.setFileFilter(txtFile);
save.setAcceptAllFileFilterUsed(true);
pane.add(areaScrollPane);
pane.add(one);
pane.add(paneText);
paneText.setLayout(new BoxLayout(paneText, BoxLayout.Y_AXIS));
frame.setSize(450, 320);
frame.setVisible(true);
}
static class SaveAs extends AbstractAction {
public SaveAs(String text, String desc) {
super(text);
putValue(SHORT_DESCRIPTION, desc);
}
public void actionPerformed(ActionEvent e) {
save.setFileHidingEnabled(false);
save.setApproveButtonText("Save");
save.setSelectedFile(new File("new 1"));
int actionDialog = save.showSaveDialog(null);
if (actionDialog != JFileChooser.APPROVE_OPTION) {
return;
} else {
log("Done!", true);
}
String name = save.getSelectedFile().getAbsolutePath();
if (!name.endsWith(".txt") && save.getFileFilter() == txtFile) {
name += ".txt";
}
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(name));
textArea.write(outFile);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (outFile != null) {
try {
outFile.close();
} catch (IOException ioee) {
}
}
}
}
private void log(String msg, boolean remove) {
JLabel label1;
label1 = new JLabel(msg);
if (remove) {
paneText.removeAll();
}
paneText.add(label1);
paneText.validate();
pane.validate();
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
paneText.removeAll();
paneText.validate();
pane.validate();
}
}.start();
}
}
}
Upvotes: 1
Views: 1535
Reputation: 591
The LookAndFeel declaration must be made before the initialization of the Object, otherwise the object won't take on those LookAndFeel properties.
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
THEN
save = new FileChooser(System.getProperty("user.home//documents"));
Upvotes: 0
Reputation: 18334
The JFileChooser
in your code is static
and therefore is instantiated before the Look and Feel is set in main
.
Set the Look and Feel before instantiation. So, both in a static
block since your JFileChooser
is static
.
...
static FileFilter txtFile;
static JFileChooser save;
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException |
InstantiationException |
IllegalAccessException |
UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
save = new JFileChooser(System.getProperty("user.home//documents"));
}
public static void main(String[] args) {
frame = new JFrame();
pane = frame.getContentPane();
...
...
Upvotes: 4
Reputation: 150
Instead of System.getProperty("user.home//documents")
, type (System.getProperty("user.home") + "/documents")
(Assuming your documents folder is called "documents"). The former would return null
, setting the JFileChooser
's location to its default -- the user's home directory.
This is because "user.home//documents"
is not a defined key for System.getProperty()
, while "user.home"
is.
You should also set your JFileChooser
to a new JFileChooser(<path>)
rather than a new FileChooser(<path>)
Upvotes: 0