自定义JFileChooser.如何将JComboBox添加到JFileChooser [英] Custom JFileChooser. How to add JComboBox into the JFileChooser

查看:105
本文介绍了自定义JFileChooser.如何将JComboBox添加到JFileChooser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JComboBox添加到JFileChooser中.

I want to add JComboBox into the JFileChooser.

我尝试扩展JFileChooser并手动添加组合框.我实际上设法做到了,但这从JFileChooser对话框中删除了文件导航面板.代码:

I tried to extend JFileChooser and to add the combo box manually. I actually managed to do it but that removed the file navigation panel from the JFileChooser dialog. Code:

 public class CustomDefinitionJFileChooser extends JFileChooser{
       public CustomDefinitionJFileChooser() {
            JComboBox comboBox = new JComboBox();
            comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
            super.add(comboBox);
       }
 }

预期结果:

实际结果:

推荐答案

首先尝试获取要添加combobox的位置的Container. 然后只需将组合框添加到该容器中即可.

First try to get the Container of the location you want to add the combobox. Then simply add the combobox to that Container.

例如,在这里首先我找到了SaveCancel按钮的Container面板.

For example here first I have found the Container panel of the Save and Cancel button.

    JPanel panel1 = (JPanel)this.getComponent(3);
    JPanel panel2 = (JPanel) panel1.getComponent(3);

然后我在面板2中添加了组合框

Then I have added the combobox in the panel2

panel2.add(comboBox);

它看起来像:

完整代码:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class TestFilechooser extends JFrame {

    private JPanel contentPane;
    MyFileChooser jc;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFilechooser frame = new TestFilechooser();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestFilechooser() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        jc = new MyFileChooser();
        JButton btnOpen = new JButton("open");
        contentPane.add(btnOpen, BorderLayout.NORTH);

        btnOpen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                int returnVal = jc.showOpenDialog(TestFilechooser.this);

            }
        });
        pack();
    }

}
class MyFileChooser extends JFileChooser{
    public MyFileChooser() {
        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));

        JPanel panel1 = (JPanel)this.getComponent(3);
        JPanel panel2 = (JPanel) panel1.getComponent(3);

        Component c1=panel2.getComponent(0);//optional used to add the buttons after combobox
        Component c2=panel2.getComponent(1);//optional used to add the buttons after combobox
        panel2.removeAll();

        panel2.add(comboBox);
        panel2.add(c1);//optional used to add the buttons after combobox
        panel2.add(c2);//optional used to add the buttons after combobox

   }
}

简单的过程:

此解决方案不会在底部添加组合框,但我认为它可以为您提供帮助.

This solution will not add the combobox at the bottom but I think it can help you.

像这样更改班级:

class MyFileChooser extends JFileChooser{
    public MyFileChooser() {
        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
        JPanel panel = new JPanel();
        panel.add(comboBox);
        setAccessory(panel);
        //add(comboBox, BorderLayout.SOUTH);
   }
}

结果:

示例的完整工作代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class TestFilechooser extends JFrame {

    private JPanel contentPane;
    MyFileChooser jc;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFilechooser frame = new TestFilechooser();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestFilechooser() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        jc = new MyFileChooser();
        JButton btnOpen = new JButton("open");
        contentPane.add(btnOpen, BorderLayout.NORTH);

        btnOpen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                int returnVal = jc.showOpenDialog(TestFilechooser.this);

            }
        });
        pack();
    }

}
class MyFileChooser extends JFileChooser{
    public MyFileChooser() {
        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
        JPanel panel = new JPanel();
        panel.add(comboBox);
        setAccessory(panel);
        //add(comboBox, BorderLayout.SOUTH);
   }
}

这篇关于自定义JFileChooser.如何将JComboBox添加到JFileChooser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆