Java Code Examples for javax.swing.JTable#getColumnModel()

The following examples show how to use javax.swing.JTable#getColumnModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableSorter.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    MouseAdapter listMouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                System.out.println("Sorting ...");
                int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK;
                boolean ascending = (shiftPressed == 0);
                sorter.sortByColumn(column, ascending);
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
 
Example 2
Source File: CPSTable.java    From cropplanning with GNU General Public License v3.0 6 votes vote down vote up
public void mouseMoved(MouseEvent evt) {
    TableColumn col = null;
    JTableHeader header = (JTableHeader)evt.getSource();
    JTable table = header.getTable();
    TableColumnModel colModel = table.getColumnModel();
    int vColIndex = colModel.getColumnIndexAtX(evt.getX());
    
    // Return if not clicked on any column header
    if (vColIndex >= 0) {
        col = colModel.getColumn(vColIndex);
    }
    
    if (col != curCol) {
        header.setToolTipText((String)tips.get(col));
        curCol = col;
    }
}
 
Example 3
Source File: TableSorter.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    MouseAdapter listMouseListener = new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                if (lastSortedColumn == column) ascending = !ascending;
                sorter.sortByColumn(column, ascending);
                lastSortedColumn = column;
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
 
Example 4
Source File: TableSorter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    MouseAdapter listMouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                System.out.println("Sorting ...");
                int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK;
                boolean ascending = (shiftPressed == 0);
                sorter.sortByColumn(column, ascending);
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
 
Example 5
Source File: TableSorter.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    MouseAdapter listMouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                System.out.println("Sorting ...");
                int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK;
                boolean ascending = (shiftPressed == 0);
                sorter.sortByColumn(column, ascending);
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
 
Example 6
Source File: TableSorter.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    MouseAdapter listMouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                System.out.println("Sorting ...");
                int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK;
                boolean ascending = (shiftPressed == 0);
                sorter.sortByColumn(column, ascending);
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
 
Example 7
Source File: TableSorter.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    final MouseAdapter listMouseListener = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            final TableColumnModel columnModel = tableView.getColumnModel();
            final int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            final int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                //System.out.println("Sorting ...");
                final int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK;
                final boolean ascending = (shiftPressed == 0);
                sorter.sortByColumn(column, ascending);
            }
        }
    };
    final JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
 
Example 8
Source File: ObjectivePanel.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(
    final JTable table,
    final Object obj,
    final boolean isSelected,
    final boolean hasFocus,
    final int row,
    final int column) {
  // set the colors, etc. using the standard for that platform
  adaptee.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
  setForeground(adaptee.getForeground());
  setBackground(adaptee.getBackground());
  setBorder(adaptee.getBorder());
  setFont(adaptee.getFont());
  setText(adaptee.getText());
  // This line was very important to get it working with JDK1.4
  final TableColumnModel columnModel = table.getColumnModel();
  setSize(columnModel.getColumn(column).getWidth(), 100000);
  int heightWanted = (int) getPreferredSize().getHeight();
  addSize(table, row, column, heightWanted);
  heightWanted = findTotalMaximumRowSize(table, row);
  if (heightWanted != table.getRowHeight(row)) {
    table.setRowHeight(row, heightWanted);
  }
  return this;
}
 
Example 9
Source File: PopulationWidget.java    From opt4j with MIT License 5 votes vote down vote up
protected final JTable getTable() {
	Model model = new Model();
	JTable table = new Table(model);
	table.addMouseListener(new TableMouseListener());

	final TableColumnModel columnModel = table.getColumnModel();
	columnModel.getColumn(0).setPreferredWidth(25);
	columnModel.getColumn(1).setPreferredWidth(140);
	return table;
}
 
Example 10
Source File: DateTimeBrowser.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
void setViewColumnsWidth(JTable jt) {
    /*
     * Resize column 0, 1
     */
    TableColumnModel colmodel = jt.getColumnModel();
    TableColumn col0 = colmodel.getColumn(0);
    col0.setPreferredWidth(175);
    TableColumn col1 = colmodel.getColumn(1);
    col1.setPreferredWidth(175);
    return;
}
 
Example 11
Source File: TableTab.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
public void adjustColumnPreferredWidths(JTable table) {
	// Gets max width for cells in column as the preferred width
	TableColumnModel columnModel = table.getColumnModel();
	for (int col = 0; col < table.getColumnCount(); col++) {
		int width = 45;
		for (int row = 0; row < table.getRowCount(); row++) {
			if (tableCellRenderer == null)
				tableCellRenderer = table.getCellRenderer(row, col);
			Component comp = table.prepareRenderer(tableCellRenderer, row, col);
			width = Math.max(comp.getPreferredSize().width, width);
		}
		columnModel.getColumn(col).setPreferredWidth(width);
	}
}
 
Example 12
Source File: CoverageReportTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void resizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        int width = 50; // Min width
        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width, width);
        }
        columnModel.getColumn(column).setPreferredWidth(width);
        columnModel.getColumn(column).setMinWidth(minColumnWidths[column]);
    }
}
 
Example 13
Source File: TopTablePanel.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
public TopTablePanel(final ElistTablePanel panel, JTable table) {
    super(table.getColumnModel());
    this.panel = panel;
    this.table = table;
    this.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseMoved(MouseEvent e) {
            setToolTipText(panel.getToolTipText(getHeight() - e.getY(), e
                    .getX()));
        }
    });
    setResizingAllowed(false);
    panel.setSize(getSize().height, getSize().width);
}
 
Example 14
Source File: FrameBox.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
public static void fitTableToLastColumn(JTable tab) {
	TableColumnModel model = tab.getColumnModel();
	TableColumn lastCol = model.getColumn(model.getColumnCount() - 1);

	int delta = tab.getSize().width;
	for(int i = 0; i < model.getColumnCount(); i++) {
		delta -= model.getColumn(i).getWidth();
	}
	int newWidth = lastCol.getWidth() + delta;
	lastCol.setWidth(newWidth);
}
 
Example 15
Source File: TableUtils.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private static int getColumnModelIndex(JTable table, int columnIndex) {
	TableColumnModel columnModel = table.getColumnModel();
	return columnModel.getColumn(columnIndex).getModelIndex();
}
 
Example 16
Source File: MosaicExpressionsPanel.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
private JScrollPane createConditionsTable(final String labelName) {
    conditionsTable = new JTable() {
        private static final long serialVersionUID = 1L;

        @Override
        public Class getColumnClass(int column) {
            if (column == 2) {
                return Boolean.class;
            } else {
                return super.getColumnClass(column);
            }
        }
    };
    conditionsTable.setName(labelName);
    conditionsTable.setRowSelectionAllowed(true);
    bindingCtx.bind("conditions", new ConditionsTableAdapter(conditionsTable));
    bindingCtx.bindEnabledState("conditions", false, "updateMode", true);
    conditionsTable.addMouseListener(createExpressionEditorMouseListener(conditionsTable, true));

    final JTableHeader tableHeader = conditionsTable.getTableHeader();
    tableHeader.setName(labelName);
    tableHeader.setReorderingAllowed(false);
    tableHeader.setResizingAllowed(true);

    final TableColumnModel columnModel = conditionsTable.getColumnModel();
    columnModel.setColumnSelectionAllowed(false);

    final TableColumn nameColumn = columnModel.getColumn(0);
    nameColumn.setPreferredWidth(100);
    nameColumn.setCellRenderer(new TCR());

    final TableColumn expressionColumn = columnModel.getColumn(1);
    expressionColumn.setPreferredWidth(360);
    expressionColumn.setCellRenderer(new TCR());
    final ExprEditor cellEditor = new ExprEditor(true);
    expressionColumn.setCellEditor(cellEditor);
    bindingCtx.addPropertyChangeListener("updateMode", new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            final boolean enabled = Boolean.FALSE.equals(evt.getNewValue());
            cellEditor.button.setEnabled(enabled);
        }
    });


    final TableColumn outputColumn = columnModel.getColumn(2);
    outputColumn.setPreferredWidth(40);

    final JScrollPane pane = new JScrollPane(conditionsTable);
    pane.setName(labelName);
    pane.setPreferredSize(new Dimension(PREFERRED_TABLE_WIDTH, 80));

    return pane;
}
 
Example 17
Source File: PlacemarkManagerTopComponent.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
public void initUI() {
    setLayout(new BorderLayout());
    placemarkTable = new JTable(placemarkTableModel);
    placemarkTable.setRowSorter(new TableRowSorter<>(placemarkTableModel));
    placemarkTable.setName("placemarkTable");
    placemarkTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    placemarkTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    placemarkTable.setRowSelectionAllowed(true);
    // IMPORTANT: We set ReorderingAllowed=false, because we export the
    // table model AS IS to a flat text file.
    placemarkTable.getTableHeader().setReorderingAllowed(false);

    ToolTipSetter toolTipSetter = new ToolTipSetter();
    placemarkTable.addMouseMotionListener(toolTipSetter);
    placemarkTable.addMouseListener(toolTipSetter);
    placemarkTable.addMouseListener(new PopupListener());
    placemarkTable.getSelectionModel().addListSelectionListener(new PlacemarkTableSelectionHandler());
    updateTableModel();

    final TableColumnModel columnModel = placemarkTable.getColumnModel();
    columnModel.addColumnModelListener(new ColumnModelListener());

    JScrollPane tableScrollPane = new JScrollPane(placemarkTable);
    JPanel mainPane = new JPanel(new BorderLayout(4, 4));
    mainPane.add(tableScrollPane, BorderLayout.CENTER);

    buttonPane = new PlacemarkManagerButtons(this);

    JPanel content = new JPanel(new BorderLayout(4, 4));
    content.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    content.add(BorderLayout.CENTER, mainPane);
    content.add(BorderLayout.EAST, buttonPane);
    Component southExtension = getSouthExtension();
    if (southExtension != null) {
        content.add(BorderLayout.SOUTH, southExtension);
    }
    content.setPreferredSize(new Dimension(420, 200));

    setCurrentView(snapApp.getSelectedProductSceneView());
    setProduct(snapApp.getSelectedProduct(VIEW));
    snapApp.getSelectionSupport(ProductSceneView.class).addHandler(new ProductSceneViewSelectionChangeHandler());
    snapApp.getProductManager().addListener(new ProductRemovedListener());
    updateUIState();
    add(content, BorderLayout.CENTER);
}
 
Example 18
Source File: ResultWindow.java    From mzmine2 with GNU General Public License v2.0 4 votes vote down vote up
public ResultWindow(String title, PeakListRow peakListRow, double searchedMass, int charge,
    Task searchTask) {

  super(title);

  this.title = title;
  this.peakListRow = peakListRow;
  this.searchTask = searchTask;

  setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  setBackground(Color.white);

  JPanel pnlLabelsAndList = new JPanel(new BorderLayout());
  pnlLabelsAndList.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

  pnlLabelsAndList.add(new JLabel("List of possible formulas"), BorderLayout.NORTH);

  resultsTableModel = new ResultTableModel(searchedMass);
  resultsTable = new JTable();
  // int rowHeight = resultsTable.getRowHeight();
  resultsTable.setRowHeight(22);
  resultsTable.setModel(resultsTableModel);
  resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  resultsTable.getTableHeader().setReorderingAllowed(false);

  resultsTableSorter = new TableRowSorter<ResultTableModel>(resultsTableModel);

  // set descending order by isotope score
  resultsTableSorter.toggleSortOrder(3);
  resultsTableSorter.toggleSortOrder(3);

  resultsTable.setRowSorter(resultsTableSorter);

  TableColumnModel columnModel = resultsTable.getColumnModel();
  DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
  renderer.setHorizontalAlignment(SwingConstants.LEFT);
  resultsTable.setDefaultRenderer(Double.class, renderer);
  columnModel.getColumn(4).setCellRenderer(new PercentageCellRenderer(1));
  columnModel.getColumn(5).setCellRenderer(new PercentageCellRenderer(1));

  JScrollPane listScroller = new JScrollPane(resultsTable);
  listScroller.setPreferredSize(new Dimension(350, 100));
  listScroller.setAlignmentX(LEFT_ALIGNMENT);
  JPanel listPanel = new JPanel();
  listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.PAGE_AXIS));
  listPanel.add(listScroller);
  listPanel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
  pnlLabelsAndList.add(listPanel, BorderLayout.CENTER);

  JPanel pnlButtons = new JPanel();
  pnlButtons.setLayout(new BoxLayout(pnlButtons, BoxLayout.X_AXIS));
  pnlButtons.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

  GUIUtils.addButton(pnlButtons, "Add identity", null, this, "ADD");
  GUIUtils.addButton(pnlButtons, "Copy to clipboard", null, this, "COPY");
  GUIUtils.addButton(pnlButtons, "Export all", null, this, "EXPORT");
  GUIUtils.addButton(pnlButtons, "View isotope pattern", null, this, "SHOW_ISOTOPES");
  GUIUtils.addButton(pnlButtons, "Show MS/MS", null, this, "SHOW_MSMS");

  setLayout(new BorderLayout());
  setSize(500, 200);
  add(pnlLabelsAndList, BorderLayout.CENTER);
  add(pnlButtons, BorderLayout.SOUTH);
  pack();

}
 
Example 19
Source File: Options.java    From ramus with GNU General Public License v3.0 4 votes vote down vote up
public static void getJTableOptions(final String name, final JTable table,
                                    final Properties properties) {
    final Integer colCount = getObjectInteger(name + "_col_count",
            properties);
    if (colCount == null || colCount.intValue() != table.getColumnCount())
        return;
    final Object cols[] = new Object[table.getColumnCount()];

    for (int i = 0; i < colCount.intValue(); i++) {
        cols[i] = table.getColumnModel().getColumn(
                table.convertColumnIndexToView(i));
    }

    for (int i = 0; i < colCount.intValue(); i++) {
        try {
            int index = table.convertColumnIndexToView(i);
            final int width = getInteger(name + "_col_" + i + "_width",
                    table.getColumnModel().getColumn(index).getWidth(),
                    properties);
            table.getColumnModel().getColumn(index)
                    .setPreferredWidth(width);
        } catch (Exception e) {

        }
    }

    final TableColumnModel cm = table.getColumnModel();
    final int tci[] = new int[colCount.intValue()];
    for (int i = 0; i < colCount.intValue(); i++)
        cm.removeColumn((TableColumn) cols[i]);

    for (int i = 0; i < colCount.intValue(); i++) {
        tci[i] = getInteger(name + "_col_" + i + "_index", i, properties);
    }

    for (int i = 0; i < colCount.intValue(); i++)
        for (int j = 0; j < colCount.intValue(); j++)
            if (tci[j] == i)
                cm.addColumn((TableColumn) cols[j]);

}
 
Example 20
Source File: HeaderPanel.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
private void initComponents() {
    JTable impl = new JTable(new DefaultTableModel(new Object[] { "" }, 0)); // NOI18N
    TableColumnModel colMod = impl.getColumnModel();
    final TableColumn col = colMod.getColumn(0);
    impl.setFocusable(false);
    header = new Header(colMod);
    impl.setTableHeader(header);
    header.setResizingAllowed(false);
    header.setReorderingAllowed(false);

    final TableCellRenderer renderer = header.getDefaultRenderer();
    header.setDefaultRenderer(new TableCellRenderer() {
        public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {

            Component component = renderer.getTableCellRendererComponent(
                    table, getRendererValue(), isSelected(),
                    isSelected(), row, processMouseEvents() ? 0 : 1);

            setupRenderer(component);

            col.setWidth(header.getWidth());
            return component;
        }
    });

    JScrollPane scroll = new JScrollPane(impl, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
                                               JScrollPane.HORIZONTAL_SCROLLBAR_NEVER) {
        public Dimension getPreferredSize() { return header.getPreferredSize(); }
        public void reshape(int x, int y, int width, int height) {
            header.setPreferredSize(new Dimension(width, height));
            super.reshape(x, y, width, height);
        }
    };
    scroll.setBorder(BorderFactory.createEmptyBorder());
    scroll.setViewportBorder(BorderFactory.createEmptyBorder());

    setLayout(new OverlayLayout(this));
    add(scroll);
}