创建可以被另一个调用的 JTable 渲染方法 class

Create JTable rendering method which can be called by another class

我有一个 JFrame,它有一个 JTable,它可以表示来自数据库的数据。一切都很好,但我想在删除操作后从另一个 class 加载或刷新它。

我已经完成了删除操作,但无法从另一个 class 加载 JTable。我的代码如下:

scrollPane = new JScrollPane();
add(scrollPane, BorderLayout.CENTER);

DefaultTableModel model=null;
try {
    model = makeTableModel();
} catch (SQLException | IOException e) {
    e.printStackTrace();
}
table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        int row = table.getSelectedRow();
        String getvalue = (table.getModel().getValueAt(row, 4).toString());
        PopulatePhotographerClass pp=new PopulatePhotographerClass(getvalue);
    }
});
table.setRowHeight(200);

scrollPane.setViewportView(table);

这是我的 makeTableModel 方法:

public static DefaultTableModel makeTableModel() throws SQLException, IOException {
    DefaultTableModel model = new DefaultTableModel(new String[]{"Image", "Name","Address","mobile-Number","NID"}, 0) {
         @Override
         public Class<?> getColumnClass(int columnIndex) {
             return columnIndex == 0 ? Icon.class : super.getColumnClass(columnIndex);
         }

     };
     String cmd = "select * from photographer_lookup";
     try (Connection con =database.DbConnect.getconnection()) {
     try (PreparedStatement stmt = con.prepareStatement(cmd)) {
     try (ResultSet rs = stmt.executeQuery()) {
         while (rs.next()) {
             String name = rs.getString(3);
             Blob blob = rs.getBlob(1);
             String address=rs.getString("address");
             String mobile=rs.getString("mobile_number");
             String nid=rs.getString("Nid");
             ImageIcon icon = null;
             try (InputStream is = blob.getBinaryStream()) {
                 BufferedImage img = ImageIO.read(is);
                 icon = new ImageIcon(img);
             }
             model.addRow(new Object[]{icon,name,address,mobile,nid});
         }
     }}}
     return model;
 }

这是我写的代码。现在我想定义一个方法来完成上面提到的所有工作,它也会被另一个 class.

调用

首先,您在使用视图索引为模型编制索引时犯了一个错误。 JTable 中 return 行或列索引的侦听器和所有方法将报告 view 索引(convertXXXIndexToModel 方法除外)。

随着 table 的排序或列的移动,视图索引 不同于模型索引。 JTable 不会对模型进行排序或重新排列模型中的列,而是会更改其到模型的映射。

如果您拥有的是视图索引并且您想要查找单元格值,

  1. 使用 JTable.getValueAt,它采用视图索引
  2. 在模型中建立索引之前,首先使用 JTable.convertRowIndexToModelJTable.convertColumnIndexToModel 将视图索引转换为模型索引 (JTable.getModel())。

您的鼠标侦听器应显示为:

table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        int row = table.getSelectedRow();
        if( row < 0 ) return; // check if a row is selected first!
        String getvalue = table.getValueAt(row, table.convertColumnIndexToView( 4 ) ).toString(); // use table.getValueAt, this getter takes view indices! Use convertColumnIndexToView to get a view index from a model index!
        PopulatePhotographerClass pp=new PopulatePhotographerClass(getvalue);
    }
});

更好的办法是实现 ListSelectionListener 来监听选择事件,而不是使用 MouseListener 来对选择事件采取行动(感谢 @mKorbel 指出这一点)。这样您将在列表选择更改时直接收到通知。


如果您希望从另一个 class 完成操作,请在您的 class 中编写一个 public 方法来扩展执行此工作的 JFrame。如果你在另一个 class 中有这个 class 的实例,只需调用这个新创建的 public 方法。

假设您的 JFrame class 称为 MyFrameWithJTable

public class MyFrameWithJTable extends JFrame {
    public void doSomeWork( /*parameters required in the operation*/ ) {
        // Does the work you want to call from another class
        // Eg the updates you want done in the JTable's model
    }
}

然后在另一个 class 中,如果您有 MyFrameWithJTable 的实例 class 您可以执行

public class AnotherClass {
    private MyFrameWithJTable instance;
    public AnotherClass( MyFrameWithJTable instance ) {
        this.instance = instance;
    }
    public void someMethod( ) {
        instance.doSomeWork( /*supply parameters*/ );
    }
}