Results 1 to 9 of 9
Thread: [JFileChooser] File name to list
- 07-26-2010, 09:54 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
[JFileChooser] File name to list
Hi,
I don't know if it's possible but I'ld like to set last opened files in a ComboBox instead of the JTextField for the File name.
So my problem is to find out how to set the Combobox at the place of the file name TextField.
Like in notepad.
(Saving and getting last used files is not a problem)
I searched around the web but did not found a solution.Last edited by Che999po; 07-26-2010 at 10:01 AM.
- 07-26-2010, 10:11 AM #2
That degree of customization would require you to create your own UI delegate for the JFileChooser, which in turn would bind you to one LaF.
A simpler approach might be to provide the combo as an accessory component.
How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
db
- 07-27-2010, 09:47 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Thanks for the informations.
Setting an accessory 'ld not make what i wanna do.
But creating my own UI should be ok. Because I'm making my own FileChooser wich includes JFileChooser and is used in different classes.
How can I make my own UI for JFileChooser?
- 07-29-2010, 04:07 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Me again...
I did not succeed to modifie the UI.
Can someone help me with some examples?
I think this could interest a lot of people.
Thanks
- 07-30-2010, 08:44 AM #5
Hardly likely, or it would have been submitted as a RFE.
Customizing a UI delegate for a complex component is not a trivial task. If you want this kind of functionality, reconsider using an accessory component, possibly a JList, which may provide a better user experience than a JComboBox.
db
- 07-30-2010, 10:02 AM #6
Proof of concept:
dbJava Code:import java.awt.BorderLayout; import java.awt.event.*; import java.io.File; import javax.swing.*; import javax.swing.event.*; public class RecentFileChooser { private JLabel label = new JLabel(); public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new RecentFileChooser().makeUI(); } }); } public void makeUI() { final DefaultListModel model = new DefaultListModel(); final JList list = new JList(model); list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setColumnHeaderView(new JLabel("Recent Files")); final JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setMultiSelectionEnabled(false); fileChooser.setAccessory(scrollPane); list.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { fileChooser.setSelectedFile((File) list.getSelectedValue()); } } }); list.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { File file = (File) list.getSelectedValue(); fileChooser.setSelectedFile(file); fileChooser.approveSelection(); label.setText(file.toString()); } } }); JButton button = new JButton("Open file chooser"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int result = fileChooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); if (!model.contains(file)) { model.addElement(file); list.ensureIndexIsVisible(model.indexOf(file)); list.getSelectionModel().clearSelection(); label.setText(file.toString()); } } } }); JFrame frame = new JFrame(); frame.add(new JLabel("Last selected file: "), BorderLayout.WEST); frame.add(label, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 500, 80); frame.setVisible(true); } }Last edited by DarrylBurke; 07-30-2010 at 11:22 AM. Reason: polished the code a bit and added a column header
- 07-30-2010, 12:33 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Thanks a lot for your help. :)
Sure that is also a nice way to do it.
I'm still using an accessory for a preview of the selected file so instead of making another one (in a tab) my idea was to do like well known apps.
Maybe it will be possible in Java 7 :D
I'll do it in an other way. Thanks.
- 07-30-2010, 12:58 PM #8
- 07-30-2010, 10:09 PM #9
Another approach could be to add a JFileChooser, probably with setControlButtonsAreShown(false). to a JPanel along with whatever else you need.
I mention this because it's often missed that JFileChooser (also JColorChooser and JOptionPane) are JComponents, not top level windows. Their static show... methods wrap the component in a dialog for display.
db
Similar Threads
-
[SOLVED] jfilechooser for saving a file....?
By prabhurangan in forum AWT / SwingReplies: 9Last Post: 03-19-2012, 05:20 PM -
Saving to file from JFileChooser SaveDialog
By kieran82 in forum AWT / SwingReplies: 2Last Post: 03-16-2010, 03:09 AM -
how to run file fron JFileChooser ? ( need small help)
By doha786 in forum New To JavaReplies: 3Last Post: 01-29-2010, 05:14 PM -
how to use JFileChooser to select file -> string
By gezzel in forum AWT / SwingReplies: 15Last Post: 10-23-2008, 05:34 AM -
how to use JFileChooser to select file -> string
By gezzel in forum New To JavaReplies: 9Last Post: 09-18-2008, 09:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks