Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:52:05+00:00 2026-05-16T15:52:05+00:00

I want to be able to have a JPanel in a cell with a

  • 0

I want to be able to have a JPanel in a cell with a JButton that does some work when clicked.

I looked for howtos about Cell Editors, but all examples talk about replacing the cell with another component (e.g. replace an int with a JTextField etc.) My situation is a little different:

I have the following ADT

class MyClass {
  public String title;
  public String url;
  public String path;
  public int annotations;
}

I created a custom table cell model that has 1 column and the class for that column is MyClass. Then I created a cell renderer for that class that returns a JPanel as shown here:

MyClass Cell Renderer

As you can see, the JPanel contains a button. I want this button to launch a JFrame whenever clicked. Any ideas?

If you will suggest Cell Editor, please be a little more specific about how to do so. If possible, provide some pseudo-code.

Thank you.

P.S. I’m pretty sure the title of this question needs some work. 😉

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:52:05+00:00Added an answer on May 16, 2026 at 3:52 pm

    After coding.mof’s reply, I finally did what I wanted. However, I wanted a more complete answer for this question, so I will provide one myself.

    So, Cell Renderers simply draw the component and do not allow any interactions within it. While Cell Editors do.

    Initially, all cells in the JTable are components that are returned by the registered renderer. However, when a cell is selected, this component is replaced by a component that is returned by the editor. These two can actually be different components! Which I’m pretty sure you can take advantage of this and make some funky cells 😛

    Anyway, in this example, both the renderer and the editor display the same component, so we will create a component that will be used by both.

    First, we need to create a TableModel that returns our ADT:

    class MyClassTableModel extends DefaultTableModel {
      List<MyClass> data;
    
      public MyClassTableModel(List<MyClass> data) {
        this.data = data;
      }
    
      public Class<?> getColumnClass(int columnIndex) { return MyClass.class; }
      public int getColumnCount() { return 1; }
      public String getColumnName(int columnIndex) { return "MyClass"; }
      public int getRowCount() { return (data == null) ? 0 : data.size(); }
      public Object getValueAt(int rowIndex, int columnIndex) { return data.get(rowIndex); }
      public boolean isCellEditable(int rowIndex, int columnIndex) { return true; }
    }
    

    Now, we create a component that will be shared between the Renderer and the Editor:

    class MyClassCellComponent extends JPanel() {
      MyClass myClass;
    
      public MyClassCellComponent() {
        // initialize components (labels, buttons, etc.)
        // add action listeners
      }
    
      public void updateData(MyClass myClass, boolean isSelected, JTable table) {
        this.myClass = myClass;
        // update buttons, labels etc. accordingly
      }
    }
    

    The isSelected and table parameters are used to render the background of the panel and are optional. Here is how the renderer uses our component:

    class MyClassCellRenderer implements TableCellRenderer {
      MyClassCellComponent panel;
    
      public MyClassCellRenderer() {
        panel = new MyClassCellComponent();
      }
    
      public Component getTableCellRendererComponent(JTable table, Object value,        boolean isSelected, boolean hasFocus, int row, int column) {
        MyClass myClass = (MyClass)value;
        panel.updateData(myClass, isSelected, table);
        return panel;
      }
    }
    

    And here is how the editor uses it:

    class MyClassCellEditor extends AbstractCellEditor {
      MyClassCellComponent panel;
      public MyClassCellEditor() {
        panel = new MyClassCellComponent();
      }
      public Component getTableCellEditorComponent(JTable table, Object value,      boolean isSelected, int row, int column) {
        MyClass myClass = (MyClass)value;
        panel.updateData(myClass, true, table);
        return panel;
      }
      public Object getCellEditorValue() {
        return null;
      }
    }
    

    Thats all. Now we can simply create a JTable as follows:

    JTable myClassTable = new JTable(new MyClassTableModel());
    myClassTable.setDefaultRenderer(MyClass.class, new MyClassCellRenderer());
    myClassTable.setDefaultEditor(MyClass.class, new MyClassCellEditor());
    

    And we’re done!

    P.S. I’m pretty sure that we can combine the Renderer and the Editor into a single class the extends AbstractCellEditor and implements TableCellRenderer, but I’m not sure about performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a JEditorPane in a JPanel. I want to be able to
I want to be able to have a Stored Procedure that can only be
Using Jersey, I want to be able to have a GET request, that would
I want to be able to have a javascript function that hides divs for
I want to be able to have one set of build/props files that will
I want to be able to have one ruby file that can require all
I want to be able to have a Settings class that is available throughout
I want to be able to have some text clickable like in webpages in
I want to be able to have a text box on a page where
I want to be able to have default text like Enter content here... appear

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.