-1

When I click on the button that activates the file chooser, and add the resulting file the panel color disappears. Does anyone know why this is happening?

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.JFileChooser;
import javax.swing.plaf.FileChooserUI;

@SuppressWarnings("serial")
public class pan extends JPanel implements DropTargetListener {

    private DefaultListModel listModel = new DefaultListModel();
    private JButton addbutton;
    private JButton removebutton;
    private JButton selectbutton;
    private JButton lockbutton;
    private JButton unlockbutton;

    /**
     * Create the panel.
     */
    public pan() {
        setLayout(null);
        addbutton = new JButton("New button");
        addbutton.setBounds(10, 10, 90, 100);
        addbutton.addActionListener(new Action());
        add(addbutton);

        removebutton = new JButton("New button");
        removebutton.setBounds(110, 10, 90, 100);
        add(removebutton);

        selectbutton = new JButton("New button");
        selectbutton.setBounds(210, 10, 90, 100);
        add(selectbutton);

        lockbutton = new JButton("New button");
        lockbutton.setBounds(310, 10, 90, 100);
        add(lockbutton);

        unlockbutton = new JButton("New button");
        unlockbutton.setBounds(410, 10, 90, 100);
        add(unlockbutton);

        JLabel headerLabel = new JLabel("New label");
        headerLabel.setBorder(new BevelBorder(BevelBorder.RAISED,
            Color.LIGHT_GRAY, Color.GRAY, null, null));
        headerLabel.setUI(new ModifLabelUI());
        headerLabel.setBounds(10, 120, 635, 30);
        add(headerLabel);   
    }


    class Action implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==addbutton){

                JFileChooser filechooser=new JFileChooser();
                filechooser.setMultiSelectionEnabled(true);             
                filechooser.updateUI();
                filechooser.showOpenDialog(new pan());
                File files=filechooser.getSelectedFile();
                listModel.addElement(files);
        }       
    }
}
madth3
  • 7,275
  • 12
  • 50
  • 74
Yaser Har
  • 149
  • 1
  • 2
  • 11
  • I'd suggest making your question title more descriptive of what the problem is. This will likely give it some more attention. – John Nov 16 '12 at 22:43
  • 2
    1. In your call to showOpenDialog, use `pan.this` instead of `new pan()`, it just does not make sense otherwise. 2. Use Java naming conventions (classes start with an upper-case letter) 3. Use appropriate LayoutManager's and nested layout, instead of this absolute layout. 4. Post an [SSCCE](http://sscce.org) illustrating your problem and detail what is not working. From the code you have posted it is impossible to tell. Also, what is that `listModel` for, I don't see any `JList`? – Guillaume Polet Nov 16 '12 at 23:50
  • 1
    I tried your example and apart from wanting to scream over the null layout, had no issues – MadProgrammer Nov 17 '12 at 00:54
  • Please try to avoid to use names of classes available in the JDK as your own class names. Seeing a `new Action()` being set on `JButton` makes me scratch my head as I remember it being an interface, until I saw you created such a class yourself – Robin Nov 17 '12 at 11:27

2 Answers2

1

This is not an appropriate use of updateUI(), which "Resets the UI property to a value from the current look and feel." It's not clear why you invoke the method, but it may cause the color change you observe. Start by deleting the line. Failing that, please edit your question to include an sscce that exhibits the problem you describe.

Also consider using a non-null layout manager.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Change last few lines of actionPerformed method as below:

     int returnVal = filechooser.showOpenDialog(new pan());
     if(returnVal == JFileChooser.APPROVE_OPTION) {
         //since its multiselection enabled, 
         //get the selected files not selected file
         File[] files=filechooser.getSelectedFiles();
         if(files != null){
           for(File file: files){
                listModel.addElement(file);
           }
         }
     }

EDIT: Please make sure the proper exception handling for expected exceptions such as HeadlessException is done appropriately.

EXPLANATION:

  1. When browse panel is open, user may cancel the operation. In that case you shouldn't be reading the files as they were not selected. This is why, need to add a check on user selection, i.e. files were selected or not.

  2. Since filechooser is opened with setMultiSelectionEnabled as true, you need to get selected files as File[] in place of getting a single file.

  3. Since you are getting multiple files, you need to add each file in the listModel. One way is to iterate the File array and add one file at a time. Other option could be to use Arrays.asList(), get the list and add all the the files at once.

Hope this helps.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Not I don't think you answer is incorrect, but maybe you'd like to explain why this would work ;) – MadProgrammer Nov 17 '12 at 00:17
  • @MadProgrammer: Added the explanation. – Yogendra Singh Nov 17 '12 at 00:47
  • Add any exception thrown in the EDT can have adverse effects on the paint process ;) – MadProgrammer Nov 17 '12 at 00:49
  • Not your down-voter, but I'm guessing it's the spurious call to `updateUI()`, as suggested [here](http://stackoverflow.com/a/13432778/230513). I think @MadProgrammer is suggesting that you edit your answer to mention the possibility of a swallowed exception on the EDT, discussed [here](http://stackoverflow.com/a/6686168/230513). – trashgod Nov 17 '12 at 17:21
  • @trashgod: I know, few people are very helpful. MadProgrammer is one of them. I added a note in the answer. – Yogendra Singh Nov 17 '12 at 17:43