2012-01-31 72 views
0

我正在为学校制作程序。在Jtextfield中显示来自JTable的数据,带有2个JFrames

我的计划有两个Jframe的 第一的JFrame = Basisscherm 第二的JFrame = Toetsenbord

在JFrame的basisscherm我得充满了来自MySQL数据库的数据JTable中。该表显示的标签,并用这个标签是特定的文本,以便每个标签都有自己的文字,这是在同一个数据库中现在

上的JFrame toetsenbord我有一个JTextField名为:Tekst

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

+2

1)不要忘记添加[标签:家庭作业]标签作业问题。 2)你有问题吗? 3)请使用普通的Java命名法。 4)为了更快地获得更好的帮助,请发布当前代码的[SSCCE](http://sscce.org/)。 – 2012-01-31 14:01:21

+0

我还没有任何代码,我想知道如何开始使用哪种方法,我需要使用示例 – user1138629 2012-01-31 14:03:08

+0

检查Oracle教程,他们非常好学! - > http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection。如果你从不尝试自己,你将永远无法独自完成某件事......你的问题很基本,一些自我投资可以解决你的问题! – 2012-01-31 14:08:16

回答

0

看看这个。使用它可以在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 
      } 
     } 
    } 
} 
相关问题