2012-01-31 75 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 
      } 
     } 
    } 
} 
相關問題