Я опубликовал предыдущий вопрос о проблеме с добавлением / удалением и редактированием строки JTable, содержимое которой импортировано из электронной таблицы Excel. Я не опубликовал свой код, поэтому вот он:
public class excelTojTable extends JFrame {
static JTable table;
static JScrollPane scroll;
// header is Vector contains table Column
static Vector headers = new Vector();
// Model is used to construct JTable
static DefaultTableModel model = null;
// data is Vector contains Data from Excel File
static Vector data = new Vector();
static JButton jbClick;
static JFileChooser jChooser;
static int tableWidth = 0; // set the tableWidth
static int tableHeight = 0; // set the tableHeight
private JButton btnAddParticipant;
private JButton btnEditParticipant;
private JButton btnDeleteParticipant;
public excelTojTable() {
super("Participant Details");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(255, 228, 225));
jChooser = new JFileChooser();
btnAddParticipant = new JButton("Add Participant");
btnAddParticipant.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ParticipantDetails.main(null);
DefaultTableModel model = new DefaultTableModel();
List<String> list = new ArrayList<String>();
List<Object> list1 = new ArrayList<Object>();
list.add(ParticipantDetails.ID_txtbox.getText());
list.add(ParticipantDetails.FName_txtbox.getText());
list.add(ParticipantDetails.LName_txtbox.getText());
list.add(ParticipantDetails.Designation_txtbox.getText());
list1.add(ParticipantDetails.Age_combobox.getSelectedItem());
list1.add(ParticipantDetails.Par_Type_combobox.getSelectedItem());
list.add(ParticipantDetails.HCond_txtbox.getText());
list1.add(ParticipantDetails.SingCoup_cmbobox.getSelectedItem());
model.addRow(list.toArray());
model.addRow(list1.toArray());
table.setModel(model);
}
});
buttonPanel.add(btnAddParticipant);
btnEditParticipant = new JButton("Edit Participant");
buttonPanel.add(btnEditParticipant);
btnDeleteParticipant = new JButton("Delete Participant");
buttonPanel.add(btnDeleteParticipant);
jbClick = new JButton("Import Excel File");
buttonPanel.add(jbClick, BorderLayout.CENTER);
// Show Button Click Event
jbClick.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jChooser.showOpenDialog(null);
File file = jChooser.getSelectedFile();
if (!file.getName().endsWith("xls")) {
JOptionPane.showMessageDialog(null,
"Please select only Excel file.",
"Error", JOptionPane.ERROR_MESSAGE);
} else {
fillData(file);
model = new DefaultTableModel(data,
headers);
tableWidth = model.getColumnCount()
* 150;
tableHeight = model.getRowCount()
* 25;
table.setPreferredSize(new Dimension(
tableWidth, tableHeight));
table.setModel(model);
}
}
{
}});
table = new JTable();
table.setAutoCreateRowSorter(true);
model = new DefaultTableModel(data, headers);
table.setModel(model);
table.setBackground(new Color(255, 250, 250));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setEnabled(false);
table.setRowHeight(25);
table.setRowMargin(4);
tableWidth = model.getColumnCount() * 150;
tableHeight = model.getRowCount() * 25;
table.setPreferredSize(new Dimension(
tableWidth, tableHeight));
scroll = new JScrollPane(table);
scroll.setBackground(new Color(255, 250, 250));
scroll.setPreferredSize(new Dimension(200, 200));
scroll.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(buttonPanel,
BorderLayout.NORTH);
getContentPane().add(scroll,
BorderLayout.CENTER);
setSize(1200, 600);
setVisible(true);
}
/**
* Fill JTable with Excel file data.
*
* @param file file :contains xls file to display in jTable
*/
void fillData(File file) {
Workbook workbook = null;
try {
try {
workbook = Workbook.getWorkbook(file);
} catch (IOException ex) {
Logger.getLogger(
excelTojTable.class.
getName()).log(Level.SEVERE,
null, ex);
}
Sheet sheet = workbook.getSheet(0);
headers.clear();
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cell1 = sheet.getCell(i, 0);
headers.add(cell1.getContents());
}
data.clear();
for (int j = 1; j < sheet.getRows(); j++) {
Vector d = new Vector();
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cell = sheet.getCell(i, j);
d.add(cell.getContents());
}
d.add("\n");
data.add(d);
}
} catch (BiffException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new excelTojTable();
}
}
То, что я хотел бы сделать, это иметь значения из другой панели с JTextfields, вставлять значения в новую строку, редактировать чьи-то данные, а также полностью удалять участника. Я был бы очень признателен за ответ с некоторыми отзывами и помощью о том, как решить этот вопрос!