I have used the JFileChooser option but by default, it shows save and cancel button. I want to put an OK button instead of save button. How do I do that?
Maybe this one help:
JFileChooser j = new JFileChooser();
j.showDialog(this, "ok");
When using one of the showDialog()-variants you can use
showDialog(Component parent, String approveButtonText)
with an approveButtonText of your choice.
If you instanciate your own JFileChooser use
setApproveButtonText(String approveButtonText)
to change the button's text.
Related
I'm making a simple Download manager in java.
When running the code in mac it works fine, but i went to test it in windows 7, and i got this problem:
If I click in save, it will leave the path to C:\Users\Gui
If I click to cancel, it will change it to C:\Users\Gui\Documents
Code that i'm using:
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f.showSaveDialog(null);
tfPath.setText(f.getCurrentDirectory().toString());
Anyone know a solution to this problem?
default directory of JFileChooser is user/documents for example
for me its
C:\Users\Adeel Ahmad\Documents
for you it will be your C:\Users\your_login_account_name\Documents
use setcurrent directory to set the default directory when jfilechooser is opened
f.setCurrentDirectory("C:\somwhere");
Your code calls f.getCurrentDirectory() , which will give you the parent directory of whichever directory you're in when selecting a directory in JFileChooser.DIRECTORIES_ONLY mode.
you might wanna use f.getSelectedFile() this is the reason you are getting C:\Users\Gui\Documents because when you save it,it gets its parent directory
and you can also know user selected save or cancel, JFileChooser.showSaveDialog returns an int which you can compare with constants of JFileChooser
you can take any action on the base of if user selected save or cancel by using
JFileChooser.CANCEL_OPTION or JFileChooser.APPROVE_OPTION(meaning save option)
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f.setCurrentDirectory(new File("D:\\"));
int result=f.showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION)
tfPath.setText(f.getCurrentDirectory().toString());
I am creating a window with two image buttons (using TriplePlay in my playN game).
Now I need dynamic text on these buttons. But when I add buttons with images (setIcon), I am not able to add Text on it same time. Please check the following code block I use now.
Interface iface = new Interface(null);
pointer().setListener(iface.plistener);
Styles buttonStyles = Styles.none().add(Style.BACKGROUND.is(new NullBackground())).
addSelected(Style.BACKGROUND.is(Background.solid(0xFFCCCCCC)));
Stylesheet rootSheet = Stylesheet.builder().add(Button.class, buttonStyles).create();
Root buttonroot = iface.createRoot(AxisLayout.horizontal().gap(150), rootSheet);
buttonroot.setSize(width_needed, height_needed);
buttonroot.addStyles(Styles.make(Style.BACKGROUND.is(new NullBackground())));
graphics().rootLayer().add(buttonroot.layer);
Button you = new Button().setIcon(buttonImage);
Button friend = new Button().setIcon(buttonImage);
buttonroot.add(you).add(friend);
buttonroot.layer.setTranslation(x_needed, y_needed);
Root nameroot = iface.createRoot(AxisLayout.horizontal().gap(300), rootSheet);
nameroot.setSize(width_needed, height_needed);
nameroot.addStyles(Styles.make(Style.BACKGROUND.is(new NullBackground())));
graphics().rootLayer().add(nameroot.layer);
name = new Label("YOU");// we need the dynamic string variable instead
friendName = new Label("FRIEND"); // we need the dynamic string variable instead
nameroot.add(name).add(friendName);
nameroot.layer.setTranslation(x_needed, y_needed);
here I have tried making a root then add button with images to it then making another root and add labels on it so that it will be show like text on the image buttons. But I know this is a bad way of doing it, and the alignment will not be according to what needed as it a dynamic text. Is there anyway to add a button, with image and a label on it?
Thanks in anticipation
Creating a button with text and an icon is trivial:
Button button = new Button("Text").setIcon(iconImage);
You can then change the text on the button any time you like, like so:
button.text.update("New Text");
If you want a button with a background image with text rendered over the background, then do the following:
Button button = new Button("Text").
addStyles(Background.is(Background.image(bgImage)));
Note that you will need the latest TriplePlay code (from Github) to use the ImageBackground. The latest code also supports Flash-style "scale9" backgrounds:
Button button = new Button("Text").
addStyles(Background.is(Background.scale9(bgImage)));
First, the code:
tab_textArea_file.addTab(docLabel, null, scrollPane_textArea, null);
So the situation is that I have a list of files the user can select from. When a user clicks on a file, the contents of the file is read and loaded into a textArea. "docLabel" (which is in the code above) is the string that's suppose to change to the name of the file selected, but it doesn't. Is it possible for to change the name within docLabel from the code above? I've tested it with a JOptionPane (works), but it's not working within a tab.
Have you tried something like
int index = tab_textArea_file.getSelectedIndex();
tab_textArea_file.setTitleAt(index, "New Title");
from java doc i see
setTitleAt(int index, String title)
you could take index of tab clicked and change is name
You can know the selected tab index by calling
int selectedIndex = tabbedPane.getSelectedIndex();
and then after your file is selected call
tabbedPane.setTitleAt(selectedIndex, "New Name");
I've had issue with the setTitleAt(int index, String title) : If the title doesn't appears, try to replace:
tab_textArea_file.setTitleAt(index, docLabel);
with:
tab_textArea_file.setTitleAt(index, new String(docLabel));
I am using JFileChooser as part of an export feature. I would like for the user to be able to either select a file from JFileChooser's file viewer or enter the name of a file in the filename text box. From what I've read it's possible to get that value using the getSelectedFile() method, so I have some listeners that call getSelectedFile() and attempt to do some checks before executing the export.
The problem I'm encountering is that the getSelectedFile() method is returning null when I enter the name into the filename text box manually. To add more confusion, the getSelectedFile() method does work in three different situations:
I populate it via setSelectedFile() (a user has clicked a value from a table and I use setSelectedFile())
I click an existing file in the file viewer
I hit ENTER after populating the filename text box
I have three file filters but have had the same behavior regardless of if they are enabled or not.
Listeners that call getSelectedFile() are as follows:
Event Listener for keyReleased
Event Listener for mousePressed.
PropertyChangeEvent listener on my jFileChooser
Action Listener on my jFileChooser
Is there a better way to listen to my jFileChooser to get the user input? I feel like I'm missing something very obvious ... any help is appreciated!
edit
A little more info ...
I have a JFileChooser component in a JSplitPane, which is in a JFrame. I'm not calling showOpenDialog to get input from the user. The component is accessible as part of the form.
What I would like to do is listen to the user input as he/she types. I have a 'start export' button that I would like to leave disabled until the user has entered a valid filename in the filename textbox in the JFileChooser component. To accomplish this I have a KeyEvent listener that I would like to use to get the filename as the user types it in.
further edit
Here is the action listener code:
jFileChooserExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChooserExportActionPerformed(evt);
}
});
I also have a property change listener here:
jFileChooserExport.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
jFileChooserExportPropertyChange(evt);
}
});
Inside of both jFileChooserExportPropertyChange and jFileChooserExportActionPerformed I am trying to get the file the user has selected by invoking getSelectedFile(). In both cases, however, it remains null until the user does one of the three methods described above
Read the section from the Swing tutorial on How to Use File Choosers. The demo code there works fine for me.
Since none of below seems to work, you might want to try to add a PropertyChangeListener to your JFileChooser, listening for the SELECTED_FILE_CHANGED_PROPERTY
What might be possibly happening is that your file chooser may have multi selection enabled, in which case getSelectedFile will return null, but getSelectedFiles will return an array containing the selected file(s). You may either want to disable multi selection, or use the array (If you want the user to only select one file, set multiSelectionEnabled to false).
Another possibility, though, is if you try to get the selected file but fileChooser.showOpenDialog or fileChooser.showSaveDialog were neither called yet or did not return JFileChooser.APPROVE_OPTION
Also, I believe JFileChooser is case-sensitive, so if the file name is "Foo.bar" and you enter "FoO.bar", it will think you want something else.
After selecting file using browse button into textfield and selecting checkbox that checkbox radio buttons and run should be enabled.How can i do this in swing using java code?
Lets start with selecting a file in Swing:
JFileChooser chooser = new JFileChooser();
int choice = chooser.showSaveDialog(MainFrame.this);
if (choice != JFileChooser.APPROVE_OPTION)
return;
File selectedFile = chooser.getSelectedFile();
That block of code will open a File dialog and wait for a user to select a file. Then the selected file will be stored into selectedFile.
You need to better clarify what you are asking for if you need more help.
JFileChooser, suggested by Justin Nelson, has an addActionListener() method. By implementing the ActionListener interface, your actionPerformed() method will be called to signal various user actions. You can enable buttons and set fields there. The tutorial How to Use File Choosers shows several examples.