问题
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);
}
}
}
回答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.
回答2:
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:
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.
Since
filechooser
is opened withsetMultiSelectionEnabled
astrue
, you need to get selected files asFile[]
in place of getting a single file.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 useArrays.asList()
, get the list and add all the the files at once.
Hope this helps.
来源:https://stackoverflow.com/questions/13425475/panel-losing-color