【问题标题】:Show data from JTable in Jtextfield with 2 JFrames使用 2 个 JFrame 在 Jtextfield 中显示来自 JTable 的数据
【发布时间】:2012-01-31 13:55:03
【问题描述】:

我正在为学校制作一个程序。

我的程序有两个 JFrame 第一个 Jframe = Basisscherm 第二个Jframe = Toetsenbord

在 Jframe 基础架构上,我有一个 Jtable,其中填充了来自 MYSQL 数据库的数据。此表显示标签,并且带有此标签的是特定文本,因此每个标签都有自己的文本,这些文本位于同一数据库中

现在在 Jframe toetsenbord 上,我有一个名为:Tekst 的 Jtextfield

现在我的问题是我想通过从 jtable 中选择标签并单击确定按钮来显示 jtextfield 中的文本,但我现在不知道从哪里开始

【问题讨论】:

  • 1) 不要忘记在作业问题中添加homework 标签。 2)你有问题吗? 3) 请使用常见的 Java 命名法。 4) 为了尽快获得更好的帮助,请发布您当前代码的SSCCE
  • 我还没有任何代码,我想知道如何从它开始我需要使用哪种方法示例
  • 查看 Oracle 教程,他们非常好学! ->docs.oracle.com/javase/tutorial/uiswing/components/…。如果您从不自己尝试,您将永远无法独自做某事...您的问题很基本,一些自我投资将解决您的问题!
  • 发生了什么“我的程序有两个JFrame's..”?请注意,SO 不是代码工厂,因此如果您不提出更具体的问题并发布代码,问题可能会被关闭。

标签: java swing netbeans


【解决方案1】:

看看这个。使用它您可以在 JTable 中获取选定的文本。

JTable table = new JTable();

if (table.getColumnSelectionAllowed()
        && !table.getRowSelectionAllowed()) {
    // Column selection is enabled
    // Get the indices of the selected columns
    int[] vColIndices = table.getSelectedColumns();
} else if (!table.getColumnSelectionAllowed()
        && table.getRowSelectionAllowed()) {
    // Row selection is enabled
    // Get the indices of the selected rows
    int[] rowIndices = table.getSelectedRows();
} else if (table.getCellSelectionEnabled()) {
    // Individual cell selection is enabled

    // In SINGLE_SELECTION mode, the selected cell can be retrieved using
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    int rowIndex = table.getSelectedRow();
    int colIndex = table.getSelectedColumn();

    // In the other modes, the set of selected cells can be retrieved using
    table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    // Get the min and max ranges of selected cells
    int rowIndexStart = table.getSelectedRow();
    int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
    int colIndexStart = table.getSelectedColumn();
    int colIndexEnd = table.getColumnModel().getSelectionModel()
        .getMaxSelectionIndex();

    // Check each cell in the range
    for (int r=rowIndexStart; r<=rowIndexEnd; r++) {
        for (int c=colIndexStart; c<=colIndexEnd; c++) {
            if (table.isCellSelected(r, c)) {
                // cell is selected
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2019-10-26
    • 2015-01-16
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    相关资源
    最近更新 更多