2014-06-19 76 views
0

我想將JComboBox添加到JFileChooser中。自定義JFileChooser。如何將JComboBox添加到JFileChooser中

我試圖擴展JFileChooser並手動添加組合框。我實際上設法做到了,但是從JFileChooser對話框中刪除了文件導航面板。代碼:

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

預期結果:

enter image description here

實際結果:

enter image description here

+0

不在你的構造函數中調用'super();'可能沒有幫助。 –

+2

我會避免試圖這樣做,除非你真的知道你在做什麼。我只會使用更多API特定的東西,比如['JFileChooser.setAccessory'](http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#accessory)。你可能得不到你想要的確切外觀,但至少你不會破壞任何東西。這裏還有一個[示例](http://stackoverflow.com/a/24291619/2587435) –

+0

*「我想將JComboBox添加到JFileChooser中。」*爲什麼?這個組合是什麼?爲標準文件選擇器沒有的最終用戶做什麼? –

回答

2

第一次嘗試得到你想要添加的combobox位置的Container。 然後只需將該組合框添加到該Container。

例如,在這裏首先我找到SaveCancel按鈕的Container面板。

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

然後我在加入是Panel2組合框

panel2.add(comboBox); 

它看起來像:

enter image description here

全碼:

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 

    } 
} 

簡單的過程:

此解決方案不會在底部添加組合框,但我認爲它可以幫助你。

改變你的類是這樣的:

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); 
    } 
} 

結果:

enter image description here

全部工作守則的例子:

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); 
    } 
} 
0
private void addEncodingComboBox(JFileChooser f,JComboBox<String> encodingComboBox, JLabel encodingLabel) { 
    Component comp =f.getComponent(2); 
    JPanel fPanel=(JPanel) comp; 
    JPanel na=(JPanel)fPanel.getComponent(2); 
    JPanel fields=(JPanel)na.getComponent(2); 
    fields.add(Box.createRigidArea(new Dimension(1,8))); 
    fields.add(encodingComboBox); 
    JPanel labels=(JPanel)na.getComponent(0); 
    labels.add(Box.createRigidArea(new Dimension(1,12))); 
    labels.add(encodingLabel); 
} 

領域CONTA在組合框中,標籤包含字段中組件的標籤。 此代碼僅在UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());被稱爲 結果:http://kepfeltoltes.hu/140810/103167300asd_www.kepfeltoltes.hu_.png