在第一个Jframe中,我有一个从数据库填充的JTable,我需要将所选jTable的数据传递到另一个框架。
所以我需要从另一个JInternalFrame中知道在第一个Jframe中选择了哪一行
public void showTableData() {
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, userName, password);
String sql = "SELECT t.name, t.exam, l.coursename\n"
+ "FROM exam AS t\n"
+ "INNER JOIN Course as l ON (t.LendaID=l.LendaID)";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
int i = 0;
Jtable1.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
这是我在第一个Jframe中使用的表
答案 0 :(得分:0)
不管您的组件有多少,作为一种简单的解决方案,您都可以创建一个CourseEventDispatcher
类作为在整个应用程序中分配课程事件的中心点。
public class CourseEventDispatcher {
private List<CourseEventSubscriber> subscribers;
// ...
public void dispatchEvent(CourseEvent event) {
for(CourseEventSubscriber: subscribers) {
if( event.getSource() != subscriber ) {
subscriber.onCourseEvent(event);
}
}
}
}
对于每个相关视图,都有一个控制器,它是CourseEventSubscriber:
public class SomeFrameController implements CourseEventSubscriber {
private CourseEventDispatcher courseEventDispatcher;
public SomeFrameController(CourseEventDispatcher courseEventDispather) {
this.courseEventDispatcher = courseEventDispatcher;
}
public void addSelectionListener() {
// ...
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
doYourOwnStuff();
// then dispatch the event
courseEventDispatcher.dispatch(new CourseEvent(this, event));
}
}
);
}
// from some other view
public void onCourseEvent(CourseEvent event) {
// process the event
// e.g. event.getEvent()
}
}
CourseEvent是一个简单的类
public class CourseEvent {
private CourseEventSubscriber source;
private EventObject event;
public CourseEvent(CourseEventSubscriber source, EventObject event) {
this.source = source;
this.event = event;
}
// getters
}
创建调度程序后,可以添加(注册)控制器。
希望这会给您另一个视角。