我有一个 JFrame,它有一个 JTable,它可以表示数据库中的数据。一切都很好,但我想在删除操作后从另一个类加载或刷新它。
我已经完成了删除操作,但无法从另一个类加载 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;
}
这是我写的代码。现在我想定义一个方法来完成上面提到的所有工作,并且它将由另一个类调用。
最佳答案
首先,您在使用 View 索引来索引模型时犯了一个错误。 JTable 中返回行或列索引的监听器和所有方法都将报告 view 索引(convertXXXIndexToModel
方法除外)。
当表排序或列移动时, View 索引将与模型索引不同。 JTable 不会对模型进行排序或重新排列模型中的列,而是会更改其到模型的映射。
如果您拥有的是 View 索引并且想要查找单元格值,则可以
- 使用
JTable.getValueAt
,它获取 View 索引 - 首先使用
JTable.convertRowIndexToModel
和JTable.convertColumnIndexToModel
将 View 索引转换为模型索引,然后再在模型中建立索引 (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
code> 指出这一点)。这样您就可以直接收到有关列表选择更改的通知。
如果您希望从另一个类完成操作,请在您的类中编写一个扩展 JFrame 的公共(public)方法来完成这项工作。如果您在另一个类中有该类的实例,只需调用这个新创建的公共(public)方法即可。
假设您的 JFrame 类名为 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
}
}
然后在另一个类中,如果您有 MyFrameWithJTable 类的实例,您可以执行以下操作
public class AnotherClass {
private MyFrameWithJTable instance;
public AnotherClass( MyFrameWithJTable instance ) {
this.instance = instance;
}
public void someMethod( ) {
instance.doSomeWork( /*supply parameters*/ );
}
}
关于java - 创建可由另一个类调用的 JTable 渲染方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194015/