第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嵌入 JFileChooser

嵌入 JFileChooser

忽然笑 2023-06-08 14:38:34
我正在嘗試添加一個JFileChooser選擇父目錄并允許用戶輸入文件名的文件。我知道showSaveDialog和showOpenDialog方法,但是我不想創(chuàng)建一個新窗口。這是我到目前為止所擁有的:public class BrowserTest extends JFrame {    private JPanel contentPane;    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    BrowserTest frame = new BrowserTest();                    frame.setVisible(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    public BrowserTest() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 450, 300);        contentPane = new JPanel();        contentPane.setLayout(new BorderLayout(0, 0));        setContentPane(contentPane);        JFileChooser browser = new JFileChooser();        browser.setFileFilter(new FileFilter() {            @Override            public String getDescription() {                return "A .extension file";            }            @Override            public boolean accept(File f) {                return f.isDirectory();            }        });        browser.setDialogType(JFileChooser.SAVE_DIALOG);        browser.setSelectedFile(new File("*.extension"));        browser.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                final String action = e.getActionCommand();                if(action.equals(JFileChooser.APPROVE_SELECTION)) {                    System.out.println("trigger");                    File f = browser.getCurrentDirectory();                    System.out.println(f);System.out.println("trigger");出于某種原因,只有在我選擇文件時按下保存按鈕才會執(zhí)行。有沒有辦法直接監(jiān)聽保存按鈕的按下?
查看完整描述

3 回答

?
紫衣仙女

TA貢獻(xiàn)1839條經(jīng)驗 獲得超15個贊

您可以使用以下代碼訪問文件選擇器的默認(rèn)按鈕,然后將您自己的偵聽器添加到該按鈕:

JButton defaultButton = browser.getUI().getDefaultButton(browser);


查看完整回答
反對 回復(fù) 2023-06-08
?
30秒到達(dá)戰(zhàn)場

TA貢獻(xiàn)1828條經(jīng)驗 獲得超6個贊

如前所述,一種解決方案是遞歸地遍歷 JFileBrowser 的組件,直到找到正確的組件,這里是一個帶有操作命令字符串“Open”的 JButton。


例如,此方法可能有效:


public static void recursiveComponentSearch(Component c, String actionCommand,

        ActionListener listener) {

    if (c instanceof JButton) {

        JButton button = (JButton) c;


        // TODO: delete the line below

        System.out.printf("Text: \"%s\";  action command: \"%s\"%n", button.getText(),

                button.getActionCommand());


        if (button.getActionCommand().equalsIgnoreCase(actionCommand)) {

            button.addActionListener(listener);

        }

    }


    // recursive search here

    if (c instanceof Container) {

        Container container = (Container) c;

        Component[] components = container.getComponents();

        for (Component component : components) {

            recursiveComponentSearch(component, actionCommand, listener);

        }

    }

}

像這樣使用:


ActionListener listener = evt -> System.out.println("Save button here");

String actionCommand = "Open";

recursiveComponentSearch(browser, actionCommand, listener);


查看完整回答
反對 回復(fù) 2023-06-08
?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗 獲得超13個贊

我才發(fā)現(xiàn)問題出在線路上browser.setSelectedFile(new File("*.extension"));。顯然,這種方法不喜歡星號,將其替換為其他任何東西都可以解決問題。例如:browser.setSelectedFile(new File("new.extension"));將按預(yù)期工作。



查看完整回答
反對 回復(fù) 2023-06-08
  • 3 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號