【问题标题】:Changing the value of a textfied in JFrame class1, JFrame class2,... by selecting an item from jtable in Jframe class3通过从 Jframe 类 3 中的 jtable 中选择一个项目来更改 JFrame 类 1、JFrame 类 2 中的 jtextfield 的值
【发布时间】:2015-01-16 20:19:57
【问题描述】:

我有 14 个 JFrame 类和一个 JFrame 类,其中一个 JTable 用作日历。当用户从 JTable 日历中选择日期时,我希望更改 14 个 JFrame 类中的任何一个中的文本字段或任何其他组件的日期。现在,帧一次出现一个,并且 jtable 从 14 个 JFrame 类中的任何一个上的 JButton 实例化。

我的难题是如何识别已实例化包含JTableJFrameJFrame 类,以便可以更改其文本字段值。

实例化包含表格的 JFrame 的代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFrame frmMain = new Calender();
    frmMain.setVisible(true);
    frmMain.setSize(367, 342);
}

从 14 个JFrame 类之一更改文本字段值的代码:

@Override
public void mouseClicked(MouseEvent me) {
    if (me.getClickCount()==2){
        int selected1 =    jTable1.getSelectionModel().getLeadSelectionIndex();
        int selected2 =jTable1.getColumnModel().getSelectionModel().getLeadSelectionIndex();

        Object c = jTable1.getModel().getValueAt(selected1, selected2);
        SowInformation.jTextField4.setText(c.toString());
        this.setVisible(false);
    }
}

其中SowInformation 是 14 个JFrame 类之一。

【问题讨论】:

  • “SowInformation 是 14 个 JFrame 类之一” 这 13 个太多了。见The Use of Multiple JFrames, Good/Bad Practice?
  • 感谢 Andrew,使用多个 JFrames 是一个很好的做法,但我的应用程序正处于最后阶段,这将改变整个架构。我的问题有什么解决办法吗??
  • “我的问题有什么解决方案吗??” 修复架构。试图让多个框架工作就像试图用腻子修补架构中的裂缝。

标签: java swing jtable jtextfield


【解决方案1】:

正如@AndrewThompson 明智地提到的那样,the use of multiple JFrame 是一种不好的做法。在这种情况下,(非)模态对话框将是更好的选择。

现在让我们假设您选择非模态对话框,那么您仍然遇到同样的问题。有几种方法可以解决它,但基本上您需要保留对打开日历框架的框架/对话框的引用。更好的是,您可以使用接口来定义合同来更新文本字段,从而将所需行为与实际实现隔离开来。例如:

public interface IUpdateText {

    public void updateText(String text);

}

现在您的日历框架需要保留对IUpdateText 兼容对象的引用:

class Calender extends JFrame {
    ...
    private IUpdateText updateText;
    ...
    public void setIUpdateText(IUpdateText ut) {
        this.updateText = ut;
    }
    ...
}

然后在您的鼠标监听器实现中调用updateText() 方法:

@Override
public void mouseClicked(MouseEvent me) {
    if (me.getClickCount()==2){
        int selectedRow = jTable1.getSelectedRow();
        int selectedColumn = jTable1.getSelectedColumn();
        if (selectedRow > -1 && selectedColumn > -1) {
            Object value = jTable1.getValueAt(selectedRow, selectedColumn); // ask the value to the view, not the model. Otherwise you need to convert both row and column indexes
            this.updateText.updateText(value.toString());
            this.setVisible(false); // maybe this.dispose() instead ?
        }
    }
}

当然,可以打开此日历框架的所有其他框架/对话框都必须实现IUpdateText 接口。比如SowInformation框架:

class SowInformation extends JFrame implements IUpdateText {
    ...
    private JTextField jTextField4;
    ...
    @Override
    public void updateText(String text) {
        this.jTextfield4.setText(text);
    }
    ...
}

最后在实例化时将IUpdateText 兼容对象设置为日历框架:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Calender frmMain = new Calender();
    calender.setIUpdateText(this);  // assuming 'this' is an IUpdateText interface compliant
    frmMain.pack();
    frmMain.setVisible(true);
}

其他cmets

从架构的角度来看,这个问题可能会通过 MVC 模式得到更好的解决,使用一个控制器作为两个框架/对话框的中介。 SO中有多个问题about MVC pattern and Swing,但我真的很喜欢@MadProgrammer(真正的Swing大师)在this answer中显示的示例/解释。

【讨论】:

  • 我也想过,但我认为 OP 想听鼠标双击来触发动作。但是,您让我意识到实现有几个问题(使用 view 索引向 model 询问数据就是其中之一)。我现在正在编辑。谢谢! @mKorbel
【解决方案2】:

您可以做的另一件事是使用静态变量并在实例化 JFrame 之前设置它的值,根据它的值您可以决定哪个类实例化了 JFrame。

【讨论】:

  • @Dic19。非常感谢你的聪明。您的解决方案完美运行。并为大家提供相关的解决方案。
  • @AugustineBazirake 为什么不接受我的回答。谢谢!
  • 我没有拒绝你的回答,只是因为我不明白如何实现它。虽然我已经阅读 java 一段时间了,但是我正在做我的第一个项目,所以我可能不容易将不那么直接的答案翻译成代码。但是非常感谢,您可以用代码详细说明。
  • @Googo 目前尚不清楚哪种方法可以帮助您解决问题,但无论如何您应该将有帮助的答案标记为已接受,以指导未来的访问者。
  • @dic 19,对不起,你知道我是这个平台的新手,但我非常感谢我收到的支持。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
相关资源
最近更新 更多