javax.swing.table.TableCellRenderer类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(17.9k)|赞(0)|评价(0)|浏览(143)

本文整理了Java中javax.swing.table.TableCellRenderer类的一些代码示例,展示了TableCellRenderer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TableCellRenderer类的具体详情如下:
包路径:javax.swing.table.TableCellRenderer
类名称:TableCellRenderer

TableCellRenderer介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

};
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(newContentPane);
    TableCellRenderer cellRenderer = jTable.getCellRenderer(row, column);
    Object valueAt = jTable.getValueAt(row, column);
    Component tableCellRendererComponent = cellRenderer.getTableCellRendererComponent(jTable, valueAt, false, false, row, column);
    int heightPreferable = tableCellRendererComponent.getPreferredSize().height;
    maxHeight = Math.max(heightPreferable, maxHeight);
int width;
Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
width = comp.getPreferredSize().width;
for (int r = 0; r < table.getRowCount(); r++) {
  renderer = table.getCellRenderer(r, column);
  comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, column), false, false, r, column);
  int currentWidth = comp.getPreferredSize().width;
  width = Math.max(width, currentWidth);
col.setPreferredWidth(width);
col.setWidth(width);

代码示例来源:origin: stackoverflow.com

private JFrame frame = new JFrame("JTable RowHeader");
private JScrollPane scrollPane;
private JTable table;
  table = new JTable(4, 4);
  for (int i = 0; i < table.getRowCount(); i++) {
    table.setValueAt(i, i, 0);
  sorter = new TableRowSorter<TableModel>(table.getModel());
  headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  headerTable.setPreferredScrollableViewportSize(new Dimension(50, 0));
  headerTable.getColumnModel().getColumn(0).setPreferredWidth(50);
  headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
      boolean selected = table.getSelectionModel().isSelectedIndex(row);
      Component component = table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, false, false, -1, -2);
      ((JLabel) component).setHorizontalAlignment(SwingConstants.CENTER);
      if (selected) {
        component.setFont(component.getFont().deriveFont(Font.BOLD));
        component.setForeground(Color.red);
      } else {
        component.setFont(component.getFont().deriveFont(Font.PLAIN));

代码示例来源:origin: stackoverflow.com

JFrame f = new JFrame();
f.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
initTable();
calendar.setLayout(new GridBagLayout());
calendar.setBorder(BorderFactory.createTitledBorder("Calendar"));
gbc.fill = GridBagConstraints.NONE;
setTable(new JTable());
getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getTable().setAutoscrolls(false);
getTable().getTableHeader().setResizingAllowed(false);
    Component comp = rend.getTableCellRendererComponent(table, value, false, false, row, col);
    maxWidth = Math.max(comp.getPreferredSize().width, maxWidth);
  TableColumn column = columnModel.getColumn(col);
  TableCellRenderer headerRenderer = column.getHeaderRenderer();
  Object headerValue = column.getHeaderValue();
  Component headerComp = headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0, col);
  maxWidth = Math.max(maxWidth, headerComp.getPreferredSize().width);
  column.setPreferredWidth(maxWidth + 6);//IntercellSpacing * 2 + 2 * 2 pixel instead of taking this value from Borders

代码示例来源:origin: stackoverflow.com

private JTable table = new JTable(model);
  table.setRowSorter(new TableRowSorter<TableModel>(model) {
  header = table.getTableHeader();
  header.getActionMap().put("toggleSortOrder2", new AbstractAction() {
      table.getActionMap().get("focusHeader").actionPerformed(e);
      if (selectedColumn != null) {
        int i = header.getColumnModel().getColumnIndex(selectedColumn);
      header.repaint();
  header.addMouseListener(new MouseAdapter() {
    public Component getTableCellRendererComponent(JTable t, Object v,
        boolean isS, boolean hasF, int row, int col) {
      return (v == selectedColumn) ? hr.getTableCellRendererComponent(
          t, v, true, true, row, col) : hr.getTableCellRendererComponent(
          t, v, false, false, row, col);
  header.setToolTipText("Sorting by F8");
  JPanel p = new JPanel(new BorderLayout());
  p.add(new JScrollPane(table));
  return p;

代码示例来源:origin: stackoverflow.com

@Override
public void run() {
 JTable table = new JTable();
 table.getColumnModel().addColumnModelListener( new WrapColListener( table ) );
 table.setDefaultRenderer( Object.class, new JTPRenderer() );
 table.getTableHeader().setFont(table.getTableHeader().getFont().deriveFont(20f).deriveFont(Font.BOLD));
 ((DefaultTableModel) table.getModel()).setDataVector(dataVector, columnIdentifiers);
 JFrame frame = new JFrame("MultiWrapColTable");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 JScrollPane jsp = new JScrollPane(table);
 frame.getContentPane().add(jsp);
 frame.pack();
 frame.setBounds(50, 50, 800, 500);
   TableCellRenderer renderer = m_table.getCellRenderer(row, col);
   if( renderer == null ) return;
   Component comp = renderer.getTableCellRendererComponent( m_table, value, false, false,
     row, col);
   if( comp == null ) return;
   int col_width = col_model.getColumn(col).getWidth();
   comp.setBounds(new Rectangle(0, 0, col_width - intercell_width, Integer.MAX_VALUE ));
   int pref_cell_height = comp.getPreferredSize().height  + intercell_height;
   if (pref_cell_height > pref_row_height) {
    pref_row_height = pref_cell_height;

代码示例来源:origin: stackoverflow.com

mainTable.setGridColor(Color.black);
mainTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
  @Override
  public Component getTableCellRendererComponent(JTable jTable, Object o, boolean b, boolean b1, int row, int column) {
    JLabel parent = (JLabel) defaultRenderer.getTableCellRendererComponent(jTable, o, b, b1, row, column);
    if (column == 0) {
      parent.setText("name");
    TableCellRenderer cellRenderer = jTable.getCellRenderer(row, column);
    Object valueAt = jTable.getValueAt(row, column);
    Component tableCellRendererComponent = cellRenderer.getTableCellRendererComponent(jTable, valueAt, false, false, row, column);
    int heightPreferable = tableCellRendererComponent.getPreferredSize().height;
    maxHeight = Math.max(heightPreferable, maxHeight);
int width;
TableCellRenderer renderer = col.getHeaderRenderer();
Component comp = renderer.getTableCellRendererComponent(
    table, col.getHeaderValue(), false, false, 0, 0);
width = comp.getPreferredSize().width;
  comp = renderer.getTableCellRendererComponent(
      table, table.getValueAt(r, column), false, false, r, column);
  int currentWidth = comp.getPreferredSize().width;
  width = Math.max(width, currentWidth);

代码示例来源:origin: stackoverflow.com

final JTable table = new JTable(new MyTableModel());
table.setRowHeight(18);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
JScrollPane scrollPane = new JScrollPane(table);
initColumnSizes(table);
setUpSportColumn(table, table.getColumnModel().getColumn(2));
add(scrollPane);
JButton resetButton = new JButton("Reset to default");
resetButton.addActionListener(new ActionListener() {
add(resetButton, BorderLayout.SOUTH);
  column = table.getColumnModel().getColumn(i);
  comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
  headerWidth = comp.getPreferredSize().width;
  comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i);
  cellWidth = comp.getPreferredSize().width;
  if (DEBUG) {
    System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth);
  column.setPreferredWidth(Math.max(headerWidth, cellWidth));
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");

代码示例来源:origin: stackoverflow.com

table1.getColumnModel().getColumn(0).setPreferredWidth(120);
     Container gp = port.getParent();
     if (gp instanceof JScrollPane) {
   super(table.getColumnModel());
   this.table = table;
table.getColumnModel().addColumnModelListener(new TableColumnModelListener() {
       repaint();
       repaint();
     if (model != null) {
       for (int index = 0; index < model.getColumnCount(); index++) {
         Component comp = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, index);
         size.width = Math.max(comp.getPreferredSize().width, size.width);
         size.height += table.getRowHeight(index);
         boolean selected = table.getSelectedRow() == index;
         Component comp = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), selected, false, 0, index);
   removeAll();

代码示例来源:origin: stackoverflow.com

JTable table = new JTable(model);
model.addTableModelListener(new HeaderCheckBoxHandler(table));
TableCellRenderer r = new HeaderRenderer(table.getTableHeader(), 0);
table.getColumnModel().getColumn(0).setHeaderRenderer(r);
table.getColumnModel().getColumn(1).setHeaderRenderer(leftAlign);
table.getColumnModel().getColumn(2).setHeaderRenderer(leftAlign);
table.getTableHeader().setReorderingAllowed(false);
 e.printStackTrace();
TableCellRenderer r = tbl.getTableHeader().getDefaultRenderer();
JLabel l=(JLabel)r.getTableCellRendererComponent(tbl, null, isS, hasF, row, col);
JTable t, Object v, boolean isS, boolean hasF, int row, int col) {
TableCellRenderer r = t.getTableHeader().getDefaultRenderer();
JLabel l=(JLabel)r.getTableCellRendererComponent(t, v, isS, hasF, row, col);
l.setHorizontalAlignment(SwingConstants.LEFT);
return l;
 table.getTableHeader().repaint();

代码示例来源:origin: stackoverflow.com

if (getParent() != null) {
      int width = 0;
      for (int col = 0; col < getColumnCount(); col++) {
        int colWidth = 0;
        for (int row = 0; row < getRowCount(); row++) {
          int prefWidth = getCellRenderer(row, col).
              getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col).
              getPreferredSize().width;
          colWidth = Math.max(colWidth, prefWidth + getIntercellSpacing().width);
        TableColumn tc = getColumnModel().getColumn(convertColumnIndexToModel(col));
        tc.setPreferredWidth(colWidth);
        width += colWidth;
  protected TableColumnModel createDefaultColumnModel() {
    TableColumnModel model = super.createDefaultColumnModel();
    model.addColumnModelListener(new TableColumnModelListener() {
      @Override
      public void columnAdded(TableColumnModelEvent e) {
table.getColumn("key").setPreferredWidth(60);
getContentPane().add(scrollPane);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(200, 200);

代码示例来源:origin: stackoverflow.com

JTable table = new JTable(mod);
JScrollPane pane =
JFrame frame = new JFrame("JTable Column Widths");
frame.getContentPane().add(pane);
frame.pack();
frame.setVisible(true);
TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < table.getColumnCount(); col++) {
    TableCellRenderer rend = table.getCellRenderer(row, col);
    Object value = table.getValueAt(row, col);
    Component comp = rend.getTableCellRendererComponent(table,
        value, false, false, row, col);
    maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
  TableColumn column = columnModel.getColumn(col);
  column.setPreferredWidth(maxwidth);

代码示例来源:origin: stackoverflow.com

JTable table = new JTable(new DefaultTableModel(columnNames, 3));
TableColumnModel m = table.getColumnModel();
for (int i = 0; i < m.getColumnCount(); i++) {
 TableColumn column = m.getColumn(i);
 column.setHeaderRenderer(new FilterIconHeaderRenderer4());
table.setAutoCreateRowSorter(true);
p.add(new JScrollPane(table));
return p;
 e.printStackTrace();
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TableHeaderIconTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
String str = Objects.toString(value, "");
String html = String.format("<html><table><td><img src='%s'/><td>%s", url, str);
return r.getTableCellRendererComponent(table, html, isSelected, hasFocus, row, column);

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-visualweb-webui-designtime

private void adjustColumnWidth(JTable table, int columnIndex) {
  TableColumnModel columnModel = table.getColumnModel();
  TableModel tableModel = table.getModel();
  TableColumn column = columnModel.getColumn(columnIndex);
  // Calculate width of header
  TableCellRenderer headerRenderer = column.getHeaderRenderer();
  if (headerRenderer == null)
    headerRenderer = table.getTableHeader().getDefaultRenderer();
  Component headerComponent = headerRenderer.getTableCellRendererComponent(
      table, column.getHeaderValue(), false, false, 0, columnIndex);
  int maxWidth = headerComponent.getPreferredSize().width;
  column.setPreferredWidth(maxWidth);
}

代码示例来源:origin: stackoverflow.com

{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
final TableCellRenderer renderer = table.getDefaultRenderer(Object.class);
   Object value, boolean isSelected, boolean hasFocus, int row,
   int column) {
  Component component = renderer.getTableCellRendererComponent(table,
    value, isSelected, hasFocus, row, column);
  String columnHeader = table.getColumnModel().getColumn(column)
    .getHeaderValue().toString();
  ((JComponent) component).setToolTipText(columnHeader);
  return component;
add(scrollPane);
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(newContentPane);

代码示例来源:origin: stackoverflow.com

private JFrame frame = new JFrame("Table Demo");
private JTableHeader header;
private Object selectedColumn = null;
private JTable table = new JTable(model);
  header = table.getTableHeader();
  header.addMouseListener(new MouseAdapter() {
      header.getColumnModel().getColumn(i).setHeaderValue("Clicked");
      h.requestFocusInWindow();
  final TableCellRenderer hr = table.getTableHeader().getDefaultRenderer();
  header.setDefaultRenderer(new TableCellRenderer() {
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      if (selectedColumn == value) {
        lbl = (JLabel) hr.getTableCellRendererComponent(table, value, true, true, row, column);
        lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createLineBorder(Color.red, 1)));
        lbl.setHorizontalAlignment(SwingConstants.LEFT);
      } else {
        lbl = (JLabel) hr.getTableCellRendererComponent(table, value, false, false, row, column);
        lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));
        lbl.setHorizontalAlignment(SwingConstants.CENTER);
        lbl.setForeground(Color.red);
      } else {
        lbl.setForeground(header.getForeground());

代码示例来源:origin: stackoverflow.com

JFrame jFrame = new JFrame();
JTable jTable = new JTable();
jTable.setModel(new DefaultTableModel(new String[][] { new String[] {"col1", "col2"}, new String[] {"col1", "col2"}, new String[] {"col1", "col2"}}, new String[] {"colHeader1", "colHeader2"}));
final TableCellRenderer defaultRenderer = jTable.getDefaultRenderer(Object.class);
jTable.setDefaultRenderer(Object.class, new TableCellRenderer() {
    Component result = defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        result.setBackground(Color.GRAY);
      } else {
        result.setBackground(Color.DARK_GRAY);
jFrame.setLayout(new BorderLayout());
jFrame.add(jTable.getTableHeader(), BorderLayout.NORTH);
jFrame.add(jTable, BorderLayout.CENTER);

代码示例来源:origin: stackoverflow.com

private JFrame frame = new JFrame("Table Demo");
private JTableHeader header;
private Object selectedColumn = null;
        ((JComponent) c).setBackground(Color.YELLOW);
      } else {
        ((JComponent) c).setBackground(table.getBackground());
  final TableCellRenderer hr = table.getTableHeader().getDefaultRenderer();
  header.setDefaultRenderer(new TableCellRenderer() {
    private JLabel lbl;
        int row, int column) {
      if (selectedColumn == value) {
        lbl = (JLabel) hr.getTableCellRendererComponent(table, value, 
            true, true, row, column);
        lbl.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.red, 1)));
        lbl.setHorizontalAlignment(SwingConstants.LEFT);
        lbl.setBackground(lbl.getBackground());
      } else {
        lbl = (JLabel) hr.getTableCellRendererComponent(table, value, 
            false, false, row, column);
        lbl.setBorder(BorderFactory.createCompoundBorder(

代码示例来源:origin: ron190/jsql-injection

/**
 *  Calculated the width based on the column name
 */
private int getColumnHeaderWidth(int column) {
  if (! this.isColumnHeaderIncluded) {
    return 0;
  }
  TableColumn tableColumn = this.tableAdjust.getColumnModel().getColumn(column);
  Object value = tableColumn.getHeaderValue();
  TableCellRenderer renderer = tableColumn.getHeaderRenderer();
  if (renderer == null) {
    renderer = this.tableAdjust.getTableHeader().getDefaultRenderer();
  }
  Component c = renderer.getTableCellRendererComponent(this.tableAdjust, value, false, false, -1, column);
  return c.getPreferredSize().width;
}

代码示例来源:origin: stackoverflow.com

this.add(pane);
public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  Component c = 
      orig.getTableCellRendererComponent(
        table, value, isSelected, hasFocus, row, column);
  if(true){
    int width  = table.getColumnModel().getColumn(column).getWidth();
    int height = table.getTableHeader().getSize().height;
    System.out.println("height"+height);
    jp.removeAll();                //clean the JPanel
      l.setPreferredSize(new Dimension(width, height));
      FontMetrics fontMetrics = l.getFontMetrics(l.getFont());
      int sizeOfSpace = fontMetrics.charWidth(' ');
      int numSpaces = (int)Math.round(icon.getIconWidth() / (double)sizeOfSpace);
    jp.add(c, StackLayout.BOTTOM);  //will contain modifications for spacing.
    jp.add(pic, StackLayout.TOP);
    return jp;

代码示例来源:origin: stackoverflow.com

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
Object[][] data = {
  {"Kathy", "Smith", "Snowboarding", new Integer(5), false}};
myTable = new TableBackroundPaint(data, columnNames);
myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
myTable.setGridColor(Color.gray);
final TableCellRenderer tcrOs = myTable.getTableHeader().getDefaultRenderer();
myTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
  public Component getTableCellRendererComponent(JTable table,
      Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    JLabel lbl = (JLabel) tcrOs.getTableCellRendererComponent(
        table, value, isSelected, hasFocus, row, column);
    lbl.setForeground(Color.black);
JTable table = ((JTableHeader) evt.getSource()).getTable();
TableColumnModel colModel = table.getColumnModel();
osPanelImport.columnIndex = colModel.getColumnIndexAtX(evt.getX());
table.changeSelection(0, 0, false, false);
return;

相关文章