简体   繁体   中英

Show data from JTable in Jtextfield with 2 JFrames

I am making a program for school.

My program has two JFrame's The first Jframe = Basisscherm The second Jframe = Toetsenbord

On the Jframe basisscherm i've got a Jtable filled with data from MYSQL Database. This Table showing labels and with this labels are specific text so each label has his own text this is in the same data base

Now on the Jframe toetsenbord i've got a Jtextfield with the name: Tekst

Now my problem is i want to show the text in the jtextfield by selecting the label from the jtable and clicking on a ok button but i don't now where to start

Have a look at this. using which you can get the selected text in 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
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM