Java JTable中的JFileChooser
我所要做的就是创建一个新组件,将其放入JTable中。它由一个JTextfield组成,单击该字段时会生成一个JFileChooser,用户可以浏览文件系统并选择所需的文件,然后用该文件的路径填充该字段。到目前为止,我已经到了可以在编辑器中填充文本字段的地步,但是当我单击它时,文件选择器不会生成。有人知道我做错了什么吗Java JTable中的JFileChooser,java,swing,jtable,jfilechooser,tablecelleditor,Java,Swing,Jtable,Jfilechooser,Tablecelleditor,我所要做的就是创建一个新组件,将其放入JTable中。它由一个JTextfield组成,单击该字段时会生成一个JFileChooser,用户可以浏览文件系统并选择所需的文件,然后用该文件的路径填充该字段。到目前为止,我已经到了可以在编辑器中填充文本字段的地步,但是当我单击它时,文件选择器不会生成。有人知道我做错了什么吗 public class FileBrowserCellEditor extends AbstractCellEditor implements TableCellEditor,
public class FileBrowserCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private static final long serialVersionUID = 1L;
private Component frame;
JButton button;
JTextField textField;
String path;
JFileChooser fc;
protected static final String EDIT = "edit";
public FileBrowserCellEditor(Component frame){
this.frame = frame;
textField = new JTextField();
textField.setActionCommand(EDIT);
textField.addActionListener(this);
fc = new JFileChooser();
}
@Override
public Object getCellEditorValue() {
return path;
}
@Override
public Component getTableCellEditorComponent(JTable arg0, Object arg1,
boolean arg2, int arg3, int arg4) {
return textField;
}
@Override
public void actionPerformed(ActionEvent e) {
//Debug
System.out.println(e.getActionCommand());
if (EDIT.equals(e.getActionCommand())) {
//The user has clicked the cell, so
//bring up the dialog.
textField.setText(path);
fc.showOpenDialog(frame);
fireEditingStopped(); //Make the renderer reappear.
} else { //User pressed dialog's "OK" button.
//currentColor = colorChooser.getColor();
File file = fc.getSelectedFile();
this.path = file.getAbsolutePath();
}
}
}
您可以尝试以下代码:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.io.*;
class TableFileChooser extends JFrame
{
private JTable table;
private JScrollPane jsPane;
private TableModel myModel;
private JPanel dialogPanel;
private JTextField tf[];
private JLabel lbl[];
public void prepareAndShowGUI()
{
setTitle("FileChooser in JTable");
myModel = new MyModel();
table = new JTable(myModel);
jsPane = new JScrollPane(table);
table.addMouseListener(new MyMouseAdapter());
getContentPane().add(jsPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prepareDialogPanel();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void prepareDialogPanel()
{
dialogPanel = new JPanel();
int col = table.getColumnCount() - 1;
dialogPanel.setLayout(new GridLayout(col,2));
tf = new JTextField[col];
lbl = new JLabel[col];
for (int i = 0; i < col; i++)
{
lbl[i] = new JLabel(table.getColumnName(i));
tf[i] = new JTextField(10);
dialogPanel.add(lbl[i]);
dialogPanel.add(tf[i]);
}
}
private void populateTextField(String[] s)
{
for (int i = 0 ; i < s.length ; i++ )
{
tf[i].setText(s[i]);
}
}
private class MyMouseAdapter extends MouseAdapter
{
@Override
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
int row = table.rowAtPoint(new Point(x,y));
int col = table.columnAtPoint(new Point(x,y));
if (col == 2)
{
String value = load();
if (value!=null && ! "null".equalsIgnoreCase(value.trim()) && ! "".equalsIgnoreCase(value.trim()))
{
myModel.setValueAt(value,row,col);
}
else
{
myModel.setValueAt("ChooseFile",row,col);
}
}
}
}
//Loads the file
private String load()
{
JFileChooser chooser = new JFileChooser(".");
chooser.setDialogTitle("Open");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
if (file!= null)
{
return file.getAbsolutePath();
}
else
{
JOptionPane.showMessageDialog(this,"Please enter a fileName","Error",JOptionPane.ERROR_MESSAGE);
return null;
}
}
return null;
}
private class MyModel extends AbstractTableModel
{
String[] columns = {
"Roll No.",
"Name",
"File Name"
};
String[][] inData = {
{"1","Anthony Hopkins","ChooseFile"},
{"2","James William","ChooseFile"},
{"3","Mc. Donald","ChooseFile"}
};
@Override
public void setValueAt(Object value, int row, int col)
{
inData[row][col] = (String)value;
fireTableCellUpdated(row,col);
}
@Override
public Object getValueAt(int row, int col)
{
return inData[row][col];
}
@Override
public int getColumnCount()
{
return columns.length;
}
@Override
public int getRowCount()
{
return inData.length;
}
@Override
public String getColumnName(int col)
{
return columns[col];
}
@Override
public boolean isCellEditable(int row ,int col)
{
return false;
}
}
public static void main(String st[])
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
TableFileChooser td = new TableFileChooser();
td.prepareAndShowGUI();
}
});
}
}
import javax.swing.*;
导入javax.swing.border.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.table.*;
导入java.io.*;
类TableFileChooser扩展了JFrame
{
专用JTable表;
私有JScrollPane jsPane;
私有表模型myModel;
私人JPanel对话小组;
私有JTextField[];
私人JLabel lbl[];
公共作废预处理显示GUI()
{
setTitle(“JTable中的文件选择器”);
myModel=新的myModel();
表=新JTable(myModel);
jsPane=新的JScrollPane(表);
table.addMouseListener(新的MyMouseAdapter());
getContentPane().add(jsPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prepareDialogPanel();
包装();
setLocationRelativeTo(空);
setVisible(真);
}
私有无效制备ALOGPANEL()
{
dialogPanel=newjpanel();
int col=table.getColumnCount()-1;
setLayout(新的GridLayout(col,2));
tf=新的JTextField[col];
lbl=新的JLabel[col];
for(int i=0;i
您可以尝试以下代码:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.io.*;
class TableFileChooser extends JFrame
{
private JTable table;
private JScrollPane jsPane;
private TableModel myModel;
private JPanel dialogPanel;
private JTextField tf[];
private JLabel lbl[];
public void prepareAndShowGUI()
{
setTitle("FileChooser in JTable");
myModel = new MyModel();
table = new JTable(myModel);
jsPane = new JScrollPane(table);
table.addMouseListener(new MyMouseAdapter());
getContentPane().add(jsPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prepareDialogPanel();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void prepareDialogPanel()
{
dialogPanel = new JPanel();
int col = table.getColumnCount() - 1;
dialogPanel.setLayout(new GridLayout(col,2));
tf = new JTextField[col];
lbl = new JLabel[col];
for (int i = 0; i < col; i++)
{
lbl[i] = new JLabel(table.getColumnName(i));
tf[i] = new JTextField(10);
dialogPanel.add(lbl[i]);
dialogPanel.add(tf[i]);
}
}
private void populateTextField(String[] s)
{
for (int i = 0 ; i < s.length ; i++ )
{
tf[i].setText(s[i]);
}
}
private class MyMouseAdapter extends MouseAdapter
{
@Override
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
int row = table.rowAtPoint(new Point(x,y));
int col = table.columnAtPoint(new Point(x,y));
if (col == 2)
{
String value = load();
if (value!=null && ! "null".equalsIgnoreCase(value.trim()) && ! "".equalsIgnoreCase(value.trim()))
{
myModel.setValueAt(value,row,col);
}
else
{
myModel.setValueAt("ChooseFile",row,col);
}
}
}
}
//Loads the file
private String load()
{
JFileChooser chooser = new JFileChooser(".");
chooser.setDialogTitle("Open");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
if (file!= null)
{
return file.getAbsolutePath();
}
else
{
JOptionPane.showMessageDialog(this,"Please enter a fileName","Error",JOptionPane.ERROR_MESSAGE);
return null;
}
}
return null;
}
private class MyModel extends AbstractTableModel
{
String[] columns = {
"Roll No.",
"Name",
"File Name"
};
String[][] inData = {
{"1","Anthony Hopkins","ChooseFile"},
{"2","James William","ChooseFile"},
{"3","Mc. Donald","ChooseFile"}
};
@Override
public void setValueAt(Object value, int row, int col)
{
inData[row][col] = (String)value;
fireTableCellUpdated(row,col);
}
@Override
public Object getValueAt(int row, int col)
{
return inData[row][col];
}
@Override
public int getColumnCount()
{
return columns.length;
}
@Override
public int getRowCount()
{
return inData.length;
}
@Override
public String getColumnName(int col)
{
return columns[col];
}
@Override
public boolean isCellEditable(int row ,int col)
{
return false;
}
}
public static void main(String st[])
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
TableFileChooser td = new TableFileChooser();
td.prepareAndShowGUI();
}
});
}
}
import javax.swing.*;
导入javax.swing.border.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.table.*;
导入java.io.*;
类TableFileChooser扩展了JFrame
{
专用JTable表;
私有JScrollPane jsPane;
私有表模型myModel;
私人JPanel对话小组;
私有JTextField[];
私人JLabel lbl[];
公共作废预处理显示GUI()
{
setTitle(“JTable中的文件选择器”);
myModel=新的myModel();
表=新JTable(myModel);
jsPane=新的JScrollPane(表);
table.addMouseListener(新的MyMouseAdapter());
getContentPane().add(jsPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prepareDialogPanel();
包装();
setLocationRelativeTo(空);
setVisible(真);
}
私有无效制备ALOGPANEL()
{
dialogPanel=新JPane
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.File;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;
public class FileChooserCellEditor extends DefaultCellEditor implements TableCellEditor {
/** Number of clicks to start editing */
private static final int CLICK_COUNT_TO_START = 2;
/** Editor component */
private JButton button;
/** File chooser */
private JFileChooser fileChooser;
/** Selected file */
private String file = "";
/**
* Constructor.
*/
public FileChooserCellEditor() {
super(new JTextField());
setClickCountToStart(CLICK_COUNT_TO_START);
// Using a JButton as the editor component
button = new JButton();
button.setBackground(Color.white);
button.setFont(button.getFont().deriveFont(Font.PLAIN));
button.setBorder(null);
// Dialog which will do the actual editing
fileChooser = new JFileChooser();
}
@Override
public Object getCellEditorValue() {
return file;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
file = value.toString();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
fileChooser.setSelectedFile(new File(file));
if (fileChooser.showOpenDialog(button) == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile().getAbsolutePath();
}
fireEditingStopped();
}
});
button.setText(file);
return button;
}
}
yourJTable.getColumnModel().getColumn(yourColumnIndex).setCellEditor(new FileChooserCellEditor());