Example usage for javax.swing JTable getSelectedColumn

List of usage examples for javax.swing JTable getSelectedColumn

Introduction

In this page you can find the example usage for javax.swing JTable getSelectedColumn.

Prototype

@BeanProperty(bound = false)
public int getSelectedColumn() 

Source Link

Document

Returns the index of the first selected column, -1 if no column is selected.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();

    if (table.getCellSelectionEnabled()) {
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        int rowIndex = table.getSelectedRow();
        int colIndex = table.getSelectedColumn();

    }/*  w  w w  . j  a v  a 2 s. c o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();

    if (table.getCellSelectionEnabled()) {
        table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        int rowIndexStart = table.getSelectedRow();
        int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
        int colIndexStart = table.getSelectedColumn();
        int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();

    }//from w ww .j ava 2  s.  com
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();

    if (table.getCellSelectionEnabled()) {
        // 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)) {
                    System.out.println("cell is selected");
                }/*from  w  w  w.j av a  2s .c  o m*/
            }
        }
    }
}

From source file:Main.java

public Main() {
    DefaultTableModel model = new DefaultTableModel(0, 5) {
        @Override/*from  w w  w . j a v a  2  s  .  c  o m*/
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    JTable table = new JTable(model);
    for (int i = 0; i < 20; i++) {
        model.addRow(new String[] { i + ".1", i + ".2", i + ".3", i + ".4", i + ".5", });
    }
    add(table, BorderLayout.CENTER);
    table.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            System.out.println("pressed");
            char key = e.getKeyChar();
            int selectedColumn = table.getSelectedColumn();
            for (int i = 0; i < model.getRowCount(); i++) {
                String value = (String) model.getValueAt(i, selectedColumn);
                model.setValueAt(value + key, i, selectedColumn);
            }
        }
    });
}

From source file:fitmon.WorkoutLog.java

private void removeItem(java.awt.event.MouseEvent evt) {

    if (evt.getClickCount() == 1) {
        JTable target = (JTable) evt.getSource();
        int row = target.getSelectedRow();
        int column = target.getSelectedColumn();
        String selectedwkt = (String) target.getValueAt(row, 0);
        //int index = selectedwkt.indexOf("--");
        // String foodName = selectedFood.substring(0, index);
        //int calorie = selectedFood.substring(index+2);
        if (target.getColumnName(target.getSelectedColumn()).equals("Edit")) {
            DefaultTableModel model = (DefaultTableModel) tab.getModel();
            model.removeRow(row);// w  ww .ja v a2  s  .  c  o  m
            for (int i = 0; i < wkt.size(); i++) {
                if (wkt.get(i).getWorkout().equals(selectedwkt)) //&& wkt.get(i).getCal().equals(calorie))
                {
                    wkt.remove(i);
                    break;
                }
            }
        }
        // do some action if appropriate column
    }

    // TODO add your handling code here:
}

From source file:fitmon.WorkoutLog.java

private void removeItem(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_removeItem

    if (evt.getClickCount() == 1) {
        JTable target = (JTable) evt.getSource();
        int row = target.getSelectedRow();
        int column = target.getSelectedColumn();
        String selectedwkt = (String) target.getValueAt(row, 0);
        //int index = selectedwkt.indexOf("--");
        // String foodName = selectedFood.substring(0, index);
        //int calorie = selectedFood.substring(index+2);
        if (target.getColumnName(target.getSelectedColumn()).equals("Edit")) {
            DefaultTableModel model = (DefaultTableModel) tab.getModel();
            model.removeRow(row);// w  w w  .j  a  v a 2s .co  m
            for (int i = 0; i < wkt.size(); i++) {
                if (wkt.get(i).getWorkout().equals(selectedwkt)) //&& wkt.get(i).getCal().equals(calorie))
                {
                    wkt.remove(i);
                    break;
                }
            }
        }
        // do some action if appropriate column
    }

    // TODO add your handling code here:
}

From source file:SetValueAtToSetValue.java

public SetValueAtToSetValue() {
    final AbstractTableModel model = new MyModel();
    final JTable table = new JTable(model);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    model.setValueAt(new Integer(1), 0, 0);

    JButton button = new JButton("Increment selected cell");
    getContentPane().add(button, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = table.getSelectedRow();
            int column = table.convertColumnIndexToModel(table.getSelectedColumn());
            int currentValue = ((Integer) model.getValueAt(row, column)).intValue();
            model.setValueAt(new Integer(currentValue + 1), row, column);
        }//  w ww  . j av a 2s.  c  o  m
    });

    pack();
}

From source file:Main.java

public Main() {
    final AbstractTableModel model = new MyModel();
    final JTable table = new JTable(model);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    model.setValueAt(new Integer(1), 0, 0);

    JButton button = new JButton("Increment selected cell");
    getContentPane().add(button, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = table.getSelectedRow();
            int column = table.convertColumnIndexToModel(table.getSelectedColumn());
            int currentValue = ((Integer) model.getValueAt(row, column)).intValue();
            model.setValueAt(new Integer(currentValue + 1), row, column);
        }/*from  w ww .  j a  v  a 2  s. c o  m*/
    });

    pack();
}

From source file:canreg.client.gui.analysis.FrequenciesByYearInternalFrame.java

private void columnTableMousePressed(java.awt.event.MouseEvent evt) {
    if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
        JTable target = (JTable) evt.getSource();
        int columnNumber = target.getSelectedColumn();

        JPopupMenu jpm = new JPopupMenu("" + columnNumber);
        jpm.add(java.util.ResourceBundle
                .getBundle("canreg/client/gui/analysis/resources/FrequenciesByYearInternalFrame")
                .getString("COLUMN ")
                + tableColumnModel.getColumn(tableColumnModel.getColumnIndexAtX(evt.getX())).getHeaderValue());
        jpm.show(target, evt.getX(), evt.getY());
    }// w w w.ja  v a  2  s .c  o m
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ??????//from ww w .  j a  v  a2 s.  c  o  m
 * ?????????????
 * 
 * @return ???
 * @see JTable#getValueAt(int, int)
 * @see ItemSelectable#getSelectedObjects()
 */
public Object[] selectedObjects() {
    for (Component child : list) {
        if (child instanceof JTable) {
            JTable table = ((JTable) child);
            int[] rows = table.getSelectedRows();
            int col = table.getSelectedColumn();
            ArrayList<Object> sublist = new ArrayList<Object>();
            for (int index : rows) {
                sublist.add(table.getValueAt(index, col));
            }
            return sublist.toArray();
        } else if (child instanceof ItemSelectable) {
            ItemSelectable selector = (ItemSelectable) child;
            return selector.getSelectedObjects();
        } else {
            // ignore
        }
    }

    return new Object[0];
}