我需要从JTable中删除行。
这是我的代码:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
public class GuiManyComponents implements ItemListener, ActionListener {
public static void main(String[] args) {
new GuiManyComponents();
}
JTextField txtField1;
JTextArea tarMultipleLines;
JButton btnDelete;
JButton btnAdd;
JButton btnEdit;
JButton ok;
JButton cancel;
JTable table = new JTable();
JTextField title;
JTextField description;
JTextField genre;
JTextField director;
JTextField rating;
ArrayList<Movie> movies = new ArrayList<Movie>();
public GuiManyComponents()
{
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(500,700);
frame.setTitle("Text area test");
final JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
frame1.setLayout(new BorderLayout());
frame1.setTitle("Text area test");
final JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
frame2.setLayout(new BorderLayout());
frame2.setTitle("Text area test");
table.getSelectedRow();
//test data
movies.add(new Movie("1ABC", "D", "1", "qq", ""));
movies.add(new Movie("2ABC", "F", "2", "", ""));
movies.add(new Movie("3ABC", "G", "3", "", ""));
updateJTable();
JPanel south = new JPanel(new GridLayout(1, 3));
south.add(btnDelete = new JButton("Delete", new ImageIcon("images\\nocheck.png")));
btnDelete.addActionListener(this);
south.add(btnEdit = new JButton("Change info", new ImageIcon("images\\paste.png")));
btnEdit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
frame.setVisible(false);
frame2.setVisible(true);
}
});
south.add(btnAdd = new JButton("Add New", new ImageIcon("images\\add.png")));
btnAdd.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
frame.setVisible(false);
frame1.setVisible(true);
}
});
JScrollPane scrollPane = new JScrollPane(table); //add scrollpane to table
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(south, BorderLayout.SOUTH);
JPanel down = new JPanel(new GridLayout(1, 2));
down.add(cancel = new JButton("Cancel", new ImageIcon("images\\nocheck.png")));
cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
frame.setVisible(true);
frame1.setVisible(false);
}
});
down.add(ok = new JButton("Save", new ImageIcon("images\\checkmark.png")));
ok.setPreferredSize(new Dimension(100, 70));
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg3)
{
String input = title.getText();
String input2 = genre.getText();
String input3 = description.getText();
String input4 = director.getText();
String input5 = rating.getText();
movies.add(new Movie(input, input2, input3, input4, input5 ));
frame.setVisible(true);
frame1.setVisible(false);
updateJTable();
}});
title = new JTextField(5);
description = new JTextField(5);
director = new JTextField(5);
rating = new JTextField(5);
genre = new JTextField(5);
title.addActionListener(this); // add actionListener
description.addActionListener(this);
rating.addActionListener(this);
genre.addActionListener(this);
director.addActionListener(this);
JPanel north = new JPanel(new GridLayout(5, 2));
north.add(new JLabel("Title: "));
north.add(title);
north.add(new JLabel("Director: "));
north.add(director);
north.add(new JLabel("Description: "));
north.add(description);
north.add(new JLabel("Genre: "));
north.add(genre);
north.add(new JLabel("Rating: "));
north.add(rating);
frame1.add(down, BorderLayout.SOUTH);
frame1.add(north, BorderLayout.NORTH);
frame1.pack();
JPanel apacia = new JPanel(new GridLayout(1, 2));
apacia.add(cancel = new JButton("Cancel", new ImageIcon("images\\nocheck.png")));
cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
frame.setVisible(true);
frame2.setVisible(false);
}
});
apacia.add(ok = new JButton("Done", new ImageIcon("images\\checkmark.png")));
JPanel virsus = new JPanel(new GridLayout(5, 2));
virsus.add(new JLabel("Title: "));
virsus.add(title);
virsus.add(new JLabel("Director: "));
virsus.add(director);
virsus.add(new JLabel("Description: "));
virsus.add(description);
virsus.add(new JLabel("Genre: "));
virsus.add(rating);
virsus.add(new JLabel("Rating: "));
virsus.add(genre);
frame2.add(virsus, BorderLayout.NORTH);
frame2.add(apacia, BorderLayout.SOUTH);
frame2.pack();
frame.setVisible(true);
frame1.setVisible(false);
frame2.setVisible(false);
}
public void updateJTable()
{
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new String[] { "title", "genre", "description", "director", "rating" });
model.setRowCount(movies.size());
int row = 0;
for (Movie movie : movies) {
model.setValueAt(movie.getTitle(), row, 0);
model.setValueAt(movie.getGenre(), row, 1);
model.setValueAt(movie.getDescription(), row, 2);
model.setValueAt(movie.getDirector(), row, 3);
model.setValueAt(movie.getRating(), row, 4);
row++;
}
table.setModel(model);
}
public void actionPerformed(ActionEvent arg0)
{
}
public void itemStateChanged( ItemEvent event )
{
}
}
我刚刚开始做GUI,这有点困难。
如果你能提供帮助,我会非常非常满意。
答案 0 :(得分:2)
JTable
通常是MVC architecture的一部分,即它有一个Model
来监听事件(添加/删除等)。您应该从Model
中删除所需的数据,并查看DefaultTableModel
答案 1 :(得分:2)
从表中删除单行很简单。您可以使用以下代码:
int row = table.getSelectedRow();
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.removeRow( row );
但是,表中的选定行可能与TableModel中的行不匹配,因为您可能正在对表进行排序或过滤。因此,您需要额外的步骤将表中的行转换为模型中的行:
int row = table.getSelectedRow();
int modelRow = table.convertRowIndexToModel( row );
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.removeRow( modelRow );
如果要一次删除多行,可能会更加混乱。当您删除一行时,TableModel中的所有数据都会移动一行,因此您的索引现在已关闭。
以下示例代码向您展示如何从JList和JTable中删除多行数据:
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
public class ItemDeletion extends JPanel
{
private JList<String> list;
private JTable table;
public ItemDeletion()
{
setLayout( new BorderLayout(5, 5) );
String[] items =
{
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"
};
// Add the list
DefaultListModel<String> listModel = new DefaultListModel<String>();
for (String item: items)
listModel.addElement( item );
list = new JList<String>( listModel );
JButton listDelete = new JButton( "Delete From List" );
listDelete.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
DefaultListModel model = (DefaultListModel)list.getModel();
int row = list.getSelectedIndex();
while (row != -1)
{
model.removeElementAt( row );
row = list.getSelectedIndex();
}
}
});
JPanel listPanel = new JPanel( new BorderLayout(5, 5) );
listPanel.add(new JScrollPane( list ), BorderLayout.CENTER);
listPanel.add(listDelete, BorderLayout.PAGE_END);
// Add the table
DefaultTableModel tableModel = new DefaultTableModel(0, 1);
List<String> tableItems = Arrays.asList( items );
Collections.shuffle( tableItems );
for (String item: tableItems)
{
System.out.println( item );
tableModel.addRow( new String[]{item} );
}
table = new JTable( tableModel );
table.setAutoCreateRowSorter(true);
((DefaultRowSorter)table.getRowSorter()).toggleSortOrder(0);
JButton tableDelete = new JButton( "Delete From Table" );
tableDelete.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
DefaultTableModel model = (DefaultTableModel)table.getModel();
int row = table.getSelectedRow();
while (row != -1)
{
int modelRow = table.convertRowIndexToModel( row );
model.removeRow( modelRow );
row = table.getSelectedRow();
}
}
});
JPanel tablePanel = new JPanel( new BorderLayout(5, 5) );
tablePanel.add(new JScrollPane( table ), BorderLayout.CENTER);
tablePanel.add(tableDelete, BorderLayout.PAGE_END);
add(listPanel, BorderLayout.LINE_START);
add(tablePanel, BorderLayout.LINE_END);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Multiple Item Deletion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ItemDeletion(), BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
答案 2 :(得分:0)
我通常使用它:
DefaultTableModel model = (DefaultTableModel) yourTable.getModel();
model.removeRow(indexOfRowToDelete);
更改模型会立即更新表格的内容。
答案 3 :(得分:-1)
jTables
的棘手问题在于它们依赖于模型(表模型有不同的类,但对于简单的事情,例如在你的情况下,我建议你阅读{{1}来自java的教程),基本上每次修改表中的数据时都必须创建一个新模型并调用DefaultTableModel
,这里是我如何做到的一个例子:
jTable.setModel(newModel)