Results 1 to 9 of 9
  1. #1
    Che999po is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Question [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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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

  3. #3
    Che999po is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    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?

  4. #4
    Che999po is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Unhappy

    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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by Che999po View Post
    I think this could interest a lot of people.
    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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Proof of concept:
    Java 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);
      }
    }
    db
    Last edited by DarrylBurke; 07-30-2010 at 11:22 AM. Reason: polished the code a bit and added a column header

  7. #7
    Che999po is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Smile

    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by Che999po View Post
    I'm still using an accessory for a preview of the selected file so instead of making another one (in a tab)
    You could have mentioned that earlier.

    No need for a tab, you can use a JComboBox in a NORTH (or SOUTH), and your present preview accessory in CENTER in a JPanel with a BorderLayout which is set as the accessory for the file chooser.

    db

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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

  1. [SOLVED] jfilechooser for saving a file....?
    By prabhurangan in forum AWT / Swing
    Replies: 10
    Last Post: 10-06-2013, 10:17 PM
  2. Saving to file from JFileChooser SaveDialog
    By kieran82 in forum AWT / Swing
    Replies: 2
    Last Post: 03-16-2010, 03:09 AM
  3. Replies: 3
    Last Post: 01-29-2010, 05:14 PM
  4. how to use JFileChooser to select file -> string
    By gezzel in forum AWT / Swing
    Replies: 15
    Last Post: 10-23-2008, 05:34 AM
  5. how to use JFileChooser to select file -> string
    By gezzel in forum New To Java
    Replies: 9
    Last Post: 09-18-2008, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •