2013-11-18 4 views
0

Я хочу настроить окно FileChooser. Здесь я хочу вставить одну метку и текстовое поле, также я встал, как получить вставленное значение из недавно добавленного текстового поля. Вот что я сделал.Настроить окно FileChooser - java

Мой CustomFileChooser:

enter image description here

и мне нужно как:

enter image description here

Я просто расширяет JFileChooser и используя нижеуказанным код для добавления компонентов:

JPanel panel = new JPanel(); 
     JLabel lable = new JLabel("Document Name: "); 
     lable.setForeground(Color.RED); 
     docText = new JTextField(); 
     docText.setName("documentNameText"); 
     docText.setHorizontalAlignment(SwingConstants.LEFT); 
     GroupLayout gl_panel = new GroupLayout(panel); 
     gl_panel.setHorizontalGroup(gl_panel 
       .createParallelGroup(Alignment.LEADING) 
       .addGroup(
         gl_panel.createSequentialGroup() 
           .addContainerGap() 
           .addGroup(
             gl_panel.createParallelGroup(
               Alignment.LEADING) 
               .addComponent(
                 docText, 
                 GroupLayout.PREFERRED_SIZE, 
                 150, 
                 GroupLayout.PREFERRED_SIZE) 
               .addComponent(lable)) 
           .addContainerGap(0, Short.MAX_VALUE))); 
     gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
       Alignment.TRAILING).addGroup(
       gl_panel.createSequentialGroup() 
         .addContainerGap(172, Short.MAX_VALUE) 
         .addComponent(lable) 
         .addPreferredGap(ComponentPlacement.RELATED) 
         .addComponent(docText, GroupLayout.PREFERRED_SIZE, 
           GroupLayout.DEFAULT_SIZE, 
           GroupLayout.PREFERRED_SIZE).addContainerGap())); 
     gl_panel.setAutoCreateGaps(true); 
     gl_panel.setAutoCreateContainerGaps(true); 
     panel.setLayout(gl_panel); 
     setAccessory(panel); 
+0

Опубликовать свой код ... –

+0

@Sandhu Я обновил свой вопрос с помощью своего кода – Balasaheb

ответ

0

Я не смог получить результат с вашим GroupLayout.

Запустите и проверьте следующую программу, которую я создал. Я дам вам пользовательский JFileChooser, как вам нужно

public class CustomJFileChooser { 
    public static void main(String[] args) { 
     final JFileChooser chooser = new JFileChooser(); 
     JComponent panel = new JPanel((LayoutManager) new FlowLayout(
       FlowLayout.LEFT)); 
     JTextField docText = new JTextField(20); 
     docText.setName("documentNameText"); 
     docText.setHorizontalAlignment(SwingConstants.LEFT); 
     panel.add(new JLabel("Document Name: ")); 
     panel.add(docText); 
     chooser.setAccessory(panel); 

     // This part is important 
     JComponent center = null; 
     BorderLayout layout = (BorderLayout) chooser.getLayout(); 
     for (Component child : chooser.getComponents()) { 
      if (BorderLayout.CENTER == layout.getConstraints(child)) { 
       center = (JComponent) child; 
      } 
     } 
     if (center != null) 
      center.add(panel, BorderLayout.SOUTH); 

     // -------- 

     final JFrame frame = new JFrame(); 
     JButton button = new JButton("Open File Chooser"); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       chooser.showOpenDialog(frame); 
      } 
     }); 
     frame.getContentPane().add(button); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

} 
0

Я только что проверил другой ответ, и я не мог заставить его работать. Вот как я это сделал:

JFileChooser fc = new JFileChooser();  

    //get the center component 
    BorderLayout layout = (BorderLayout) fc.getLayout(); 
    JComponent comp = (JComponent) layout.getLayoutComponent(BorderLayout.CENTER); 
    layout = (BorderLayout) comp.getLayout(); 
    comp = (JComponent) layout.getLayoutComponent(BorderLayout.CENTER); 

    //add my panel at the bottom 
    comp.add(panel, BorderLayout.SOUTH); 
Смежные вопросы