Home > front end >  Customize jFileChooser vertical scrollbar
Customize jFileChooser vertical scrollbar

Time:10-15

I'm using a jFileChooser and I'm trying to achieve the following :

https://i.stack.imgur.com/O6MNj.png

I'm trying to force the the LIST view to have a VERTICAL scroll bar or my second option is to disable the size and modified columns from the details view.

EDIT:

Is there any way that I can insert a JScrollBar inside the jFileChooser?

CodePudding user response:

You can access the JList and change the orientation of the list as follows:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.List;

class FileChooserList
{
    private static void createAndShowUI()
    {
        JFileChooser  fileChooser = new JFileChooser(".");

        //  Change list orientation

        JList list = SwingUtils.getDescendantsOfType(JList.class, fileChooser).get(0);
        list.setLayoutOrientation( JList.VERTICAL );

        fileChooser.showOpenDialog(null);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }

}

The above code requires the Swing Utils class.

disable the size and modified columns from the details view

Depends on what you mean by "disable".

You can remove those columns from the view of the table:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.List;

class FileChooserDetails
{
    private static void createAndShowUI()
    {
        JFileChooser  fileChooser = new JFileChooser(".");

        //  Show the Details view of the file chooser

        Action details = fileChooser.getActionMap().get("viewTypeDetails");
        details.actionPerformed(null);

        //  Remove columns from view

        JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
        TableColumnModel tcm = table.getColumnModel();
        tcm.removeColumn( tcm.getColumn(3) );
        tcm.removeColumn( tcm.getColumn(1) );

        fileChooser.showOpenDialog(null);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }

}
  • Related