java item listener - java

i need help regarding the save menu item i have in my menu bar. i have set the code to
private void smActionPerformed(java.awt.event.ActionEvent evt) {
}
but i am unsure what needs to go inbetween the brackets? The gui is a form where poeple fill in there medical record e.g combo box "mr/mrs" textfield "medical problems" etc and when they click file save i would like the user to get a save box (like save as box in word) and the information to be saved in a txt file.
P.S. would it be possible to have the "file type" set to save as a txt file as default.

Use JFileChooser#showSaveDialog() to ask the enduser to select a File.
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// ...
} else {
// User pressed cancel.
}

Maybe looking at this could help you. I don't fully understand what you need.
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFileChooser.html
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

Related

delete selected text from JScrollpane

I am developing a GUI in Java using the Netbeans IDE and swing forms. Here I want to delete the selected text from the Jscrollpane using a DELETE button which I have added by using an ADD button. I am able to delete the entire text but not the selected one. I have searched on Internet but could not get any help. Below is the code which I have tried. Any suggestions will be helpful for me.
private void buttondelActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
FileFilter ft = new FileNameExtensionFilter( "arxml Files", "arxml" );
chooser.setFileFilter(ft);
jScrollPane1.getContainerListeners();
chooser.setMultiSelectionEnabled(true);
JOptionPane.showConfirmDialog(frame, "You Selected : " + list.getSelectedValue());
if (evt.getSource() == buttondel )
{
File[] files = chooser.getSelectedFiles();
log.setText("");
}
Your JScrollPane must have a JTextArea where you will be putting the content. Add a listener to the JTextArea and get the selected text on an action performed. Then delete the selected text

How to get file path from JavaFX FileChooser?

I have a simple JavaFX window with a TextField for users to enter a file path and a separate browse link.
JavaFX Window
I'd like to ask how to extract the full file path of the selected file from JavaFX FileChooser (so I can set the path in the TextField)?
I understand what I'm trying to achieve can be done simply with Swing JFileChooser with something like:
JFileChooser chooser = new JFileChooser();
String someString = chooser.getSelectedFile().toString();
But since my application is in JavaFX I want it to have a consistent look and not a mix with Swing.
I've looked through the documentation, there doesn't seem to be a method for this https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html
Thanks in advance.
Here's another documentation. What you get in return from using showOpenDialog is a File object.
public File showOpenDialog(Window ownerWindow)
Shows a new file open dialog. The method doesn't return until the
displayed open dialog is dismissed. The return value specifies the
file chosen by the user or null if no selection has been made. If the
owner window for the file dialog is set, input to all windows in the
dialog's owner chain is blocked while the file dialog is being shown.
A file object has various methods like e. g. getAbsolutePath.
Use showOpenDialog or showSaveDialog (depending on whether you want to open an existing file or save a new one). Both return a File object.
In the controller class where you have the TextField you can create a method like so:
public void getTheUserFilePath() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Upload File Path");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("ALL FILES", "*.*"),
new FileChooser.ExtensionFilter("ZIP", "*.zip"),
new FileChooser.ExtensionFilter("PDF", "*.pdf"),
new FileChooser.ExtensionFilter("TEXT", "*.txt"),
new FileChooser.ExtensionFilter("IMAGE FILES", "*.jpg", "*.png", "*.gif")
);
File file = fileChooser.showOpenDialog(dialogPane.getScene().getWindow());
if (file != null) {
// pickUpPathField it's your TextField fx:id
pickUpPathField.setText(file.getPath());
} else {
System.out.println("error"); // or something else
}
}

Swing - JFileChooser open dialog does not show images for various options

I'm using JFileChooser API for opening a file. When open dialogbox appears, it does not show images for various option like, Up one lever, Create new Folder, List, Details. Some of the option is also not visible untill mouse hover. Here is the image :
My code looks something like this:
JFileChooser fileChooser = new JFileChooser();
FileFilter xlsExcelType = new FileNameExtensionFilter("Excel spreadsheet (.xls)", "xls");
FileFilter xlsxExcelType = new FileNameExtensionFilter("Excel spreadsheet (.xlsx)", "xlsx");
fileChooser.addChoosableFileFilter(xlsExcelType);
fileChooser.addChoosableFileFilter(xlsxExcelType);
fileChooser.setFileFilter(xlsxExcelType);
int returnVal = fileChooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file= fileChooser.getSelectedFile();
}
Kndly pass your idea to make those option visible with image.
Thanks
Look and see if java.awt.FileDialog has the option(s) you want. If not, you might try ideas from here or here.

JFileChooser that manually picks the file

I was just wondering. Is there any way to use JFileChooser but open the file manually? So I could put the directory somewhere in code before and then just load it?
Here is part of my code:
JFileChooser fc = new JFileChooser();
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try {
File plik = fc.getSelectedFile();
Scanner skaner = new Scanner(plik);
while (skaner.hasNext())
dialog.append(skaner.nextLine() + "\n");
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
So I want to:
Open file from directory so i won't have to choose the "something.txt".
Piece of code that let me open another file in the same JTextArea one by one, by clicking the JButton.
What I really need is a piece of code that lets me load txt file (from directory) by clicking a button few times in a row.
Is this even possible?
You can use setSelectedFile method of the JFileChooser to 'pre-select' the File, the user will only have to click OK if the file exists.
JTextArea directory=new JTextArea();
directory.setText(System.getProperty("user.home"));
directory.setEditable(true);
JFileChooser choose=new JFileChooser(directory.getText());

java - JFileChooser - open/cancel/exit button

I made a button that will create a JFileChooser so the user can open a .txt file, here's the code inside the action listener of the button:
JFileChooser fc = new JFileChooser();
//filter-show only .txt files
FileNameExtensionFilter txtfilter = new FileNameExtensionFilter("txt files (*.txt)", "txt");
//apply the filter to file chooser
fc.setFileFilter(txtfilter);
fc.setDialogTitle("Otvori txt file");
//disable the ability to show files of all extensions
fc.setAcceptAllFileFilterUsed(false);
//create file chooser via jFrame
fc.showOpenDialog(jFrame);
//get selected file
File selFile = fc.getSelectedFile();
Path path = Paths.get(selFile.toString());
asdf = selFile.toString();
//display chosen file on jLabel5
jLabel5.setText(path.getFileName().toString());
It works just fine, if you select the .txt file inside the file chooser, but it also works if you just select a file and then press cancel and exit. I assume it's because of the getSelectedFile() but I am wondering if there is a way to make sure the user selected a file and pressed open inside file chooser as a condition to get a file?
You should check whether the return value from:
fc.showOpenDialog(jFrame) == JFileChooser.APPROVE_OPTION
That return value indicates how the user exited the dialog.
See JFileChooser.showOpenDialog(Component) docs.

Categories