| |

VerySource

 Forgot password?
 Register
Search
View: 775|Reply: 4

How to set the cellrender of a cell in JTable

[Copy link]

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-10-15 14:00:01
| Show all posts |Read mode
Hello everyone, it seems that JTable can only set one column of cellrender
I want to set the cellrender of the specified cell instead of the entire column. What should I do?
Reply

Use magic Report

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-10-15 14:15:01
| Show all posts
Is it possible to use prepareRenderer?
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-10-15 14:30:01
| Show all posts
jtable.setCellSelectionEnable(true);
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-10-15 15:00:01
| Show all posts
Haha, I used to be similar to you, but I set up the editor and found a lot of information. The following is how I set the CellEditor of the specified cell, I hope it will be helpful to you.
EachRowEditor.java

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

public class EachRowEditor implements TableCellEditor {
  protected Hashtable editors; //Hashtable
  protected TableCellEditor editor, defaultEditor; //Editor
  JTable table; //This variable does not seem to be used

  public EachRowEditor(JTable table) {
    this.table = table;
    editors = new Hashtable();
    defaultEditor = new DefaultCellEditor(new JTextField()); //Use JTextField to construct the editor
  }

  public void setEditorAt(int row, TableCellEditor editor) {//Key
    editors.put(new Integer(row),editor); //Specify the editor to join the hash table
  }

  public Component getTableCellEditorComponent(JTable table,
      Object value, boolean isSelected, int row, int column) {

    return editor.getTableCellEditorComponent(table,
             value, isSelected, row, column);
  }

  public Object getCellEditorValue() {
    return editor.getCellEditorValue();
  }
  public boolean stopCellEditing() {
    return editor.stopCellEditing();
  }
  public void cancelCellEditing() {
    editor.cancelCellEditing();
  }
  public boolean isCellEditable(EventObject anEvent) {
    selectEditor((MouseEvent)anEvent);
    return editor.isCellEditable(anEvent);
  }
  public void addCellEditorListener(CellEditorListener l) {
    editor.addCellEditorListener(l);
  }
  public void removeCellEditorListener(CellEditorListener l) {
    editor.removeCellEditorListener(l);
  }
  public boolean shouldSelectCell(EventObject anEvent) {
    selectEditor((MouseEvent)anEvent);
    return editor.shouldSelectCell(anEvent);
  }

  protected void selectEditor(MouseEvent e) {//I don’t understand this
    int row;
    if (e == null) {
      row = table.getSelectionModel().getAnchorSelectionIndex();
    } else {
      row = table.rowAtPoint(e.getPoint());
    }
    editor = (TableCellEditor)editors.get(new Integer(row));
    if (editor == null) {
      editor = defaultEditor;
    }
  }
}
Set up with the following methods:
EachRowEditor rowEditor = new EachRowEditor(JTable); //The parameter of JTable seems useless, null should work too
rowEditor.setEditorAt(row number, new DefaultCellEditor(jComboBox1)); //Use jComboBox1 to construct the editor and specify the row
    jTable1.getColumnModel().getColumn(column number).setCellEditor(rowEditor); //Set the editor
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-10-15 15:15:01
| Show all posts
The "row number" and "column number" above are int type, and the two ints specify the cell you want
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list