Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:53:45+00:00 2026-06-01T11:53:45+00:00

My intention is to embed the JFileChooser in an other component, for instance, one

  • 0

My intention is to embed the JFileChooser in an other component, for instance, one can choose a file and click the “add” button, so that the file gets added to the JList(during run-time).I have created an example GUI in this form:

GUI

I am unable to create a link between the JFileChooser and JList. Can anybody help?

You can also see what I have tried:

        public Converter() {
    setForeground(Color.BLACK);
    getContentPane().setLayout(null);

    textField = new JTextField();
    textField.setBounds(20, 12, 714, 20);
    getContentPane().add(textField);
    textField.setColumns(10);

    final JScrollPane scrollPane = new JScrollPane();
    setTitle("ABC"); 
    scrollPane.setBounds(0, 470, 766, -438);
    getContentPane().add(scrollPane);

    list = new JList();
    list.setBackground(Color.LIGHT_GRAY);
    list.setForeground(Color.GRAY);

    vector = new Vector<File>();
    field = new JTextField();

    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setBounds(10, 43, 485, 463);
    getContentPane().add(fileChooser);


    list = new JList(vector);
    list.setBackground(Color.LIGHT_GRAY);

    JButton btnNewButton = new JButton("ADD");
    btnNewButton.setBounds(505, 106, 89, 23);
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
              add();
        }

        private void add() {
            // TODO Auto-generated method stub

              {
                  for (File file : fileChooser.getSelectedFiles()) {
                        field.setText(file.getAbsolutePath());
                        vector.add(file);
                        System.out.println("Added..!!");
                }
                //list.updateUI();
              }
            }
    });
    getContentPane().add(btnNewButton);


    JButton btnNewButton_1 = new JButton("REMOVE");
    btnNewButton_1.setBounds(505, 190, 89, 23);
    btnNewButton_1.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
               remove();
               }

        private void remove() {
            if(list.getSelectedIndices().length > 0) {
                  int[] selectedIndices = list.getSelectedIndices();
                  for (int i = selectedIndices.length-1; i >=0; i--) {
                        vector.removeElementAt(i);
                        System.out.println("Removed..!!");
                  } 
                   }
                    list.updateUI();

        }   
        });
    getContentPane().add(btnNewButton_1);

    JButton btnNewButton_2 = new JButton("DECODE");
    btnNewButton_2.setBounds(505, 278, 89, 23);

    getContentPane().add(btnNewButton_2);

    JList list_1 = new JList();
    list_1.setForeground(Color.BLACK);
    list_1.setBackground(Color.LIGHT_GRAY);
    list_1.setBounds(604, 109, 162, 328);
    getContentPane().add(list_1);

    final JFrame Jframe = new JFrame();
    Jframe.setFont(new Font("Arial", Font.BOLD, 14));
    Jframe.setForeground(Color.WHITE);
    Jframe.setTitle("Additional Loader Information");
    Jframe.getContentPane().setLayout(null);

}       

static class PreviewPane extends JPanel implements PropertyChangeListener {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JLabel label;
    private int maxImgWidth;
    public PreviewPane() {
        setLayout(new BorderLayout(5,5));
        setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        add(new JLabel("Preview:"), BorderLayout.NORTH);
        label = new JLabel();
        label.setBackground(Color.WHITE);
        label.setOpaque(true);
        label.setPreferredSize(new Dimension(200, 200));
        maxImgWidth = 195;
        label.setBorder(BorderFactory.createEtchedBorder());
        add(label, BorderLayout.CENTER);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        Icon icon = null;
        if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt
                .getPropertyName())) {
            File newFile = (File) evt.getNewValue();
            if(newFile != null) {
                String path = newFile.getAbsolutePath();
                if(path.endsWith(".gif") || path.endsWith(".jpg")                                                             
                             || path.endsWith(".png") || path.endsWith(".bmp")) {
                    try {
                        BufferedImage img = 
                                             ImageIO.read(newFile);
                        float width = img.getWidth();
                        float height = img.getHeight();
                        float scale = height / width;
                        width = maxImgWidth;
                        height = (width * scale); 
                                           // height should be scaled from new width                            

                    }
                    catch(IOException e) {
                        // couldn't read image.
                    }
                }
            }

            label.setIcon(icon);
            this.repaint();

        }
                   }

                 }

                 public static void main(String args[]) {        
                    // Create an instance of the test application         
                        Converter frame = new Converter();        
                         frame.pack();     
                           frame.setVisible(true);     
                      }
                 }

It would be really helpful if somebody could help me out with this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:53:47+00:00Added an answer on June 1, 2026 at 11:53 am

    The problem you are running into is that the ListModel your JList is creating for you does not support changes. You need to create a DefaultListModel and use that instead of your Vector.

    Ex:

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final DefaultListModel<String> model = new DefaultListModel<>();
                JList<String> list = new JList<>(model);
    
                JButton add = new JButton("Click Me!");
                add.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        model.addElement(model.getSize() + "");
                    }
                });
    
                JPanel p = new JPanel();
                p.add(new JScrollPane(list));
                p.add(add);
    
                JFrame frame = new JFrame("Example");
                frame.setContentPane(p);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Intention is to take a current line (String that contains commas), replace white space
The intention of following (simplified) code fragment is to return one random row .
My intention is to make an application that will track the movement of my
My intention was to use pyGTK's main loop to create a function that blocks
My intention is to write a graphical query language where users can drag and
My intention is to read every directory and file rooted at a directory given
My intention is to output log of recursive wget in one line, 'status bar'-like.
Goal : My intention to design a utility that could access any numeric values(
my intention is to hide a div element as soon as the hide button
My intention is to make a Broadcast receiver that performs actions when receiving a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.