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 8786653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:41:28+00:00 2026-06-13T21:41:28+00:00

I’m new to GUI design and I’m trying to plan this out before I

  • 0

I’m new to GUI design and I’m trying to plan this out before I go too far the wrong way, any help would be nice. I’m trying to display a JTable with rows of Employee, which itself has datatypes of String and ArrayList<Cert>. Cert contains a String.

I’d like to have the table present the data for editing, but for a few of the columns I’d like to implement a JComboBox for selection of a String from a set of valid strings, as well as color each option differently (different background colors in the JComboBox).

Also, the ArrayList<Cert> currently displays in a cell as [xxx, xxx, …] where XXX is the return from the toString() function for each item in the ArrayList. I think I’d like to display that ArrayList<Cert> as a read-only JComboBox, but I’m not as concerned with this item.

I’m questioning how many classes I need to create to make this happen. I already have a custom model for the JTable extending AbstractTableModel. Do I need to write an extension of JComboBox or do I just need to extend the appropriate renderer for a JComboBox as a cell and do the magic there, then assign that custom renderer to the cell renderer for the String cell?

Here’s what I have so far, lightly abridged:

public class EmployeeTableModel extends AbstractTableModel {
  ...
  private ArrayList<Employee> myDataObjects = new ArrayList<Employee>();
  ...
  @Override
  public Object getValueAt(int row, int column) {
      Employee emp = myDataObjects.get(row);

      switch (column) {
          case 0:
              return emp.getName();
          case 1:
              return emp.getShift();
          case 2:
              return emp.getCertifications();
          default:
              return "";
      }
   }
}

Employees:

public class Employee {
  private String name;
  private String shift;
  private ArrayList<Cert> certs;
  ...
  public String getName() {
    return name;
  }

  public String getShift() {
    return shift;
  }

  public ArrayList<Cert> getCerts() {
    return certs;
  }
  ...
}    

And the initializations:

EmployeeTableModel etm = new EmployeeTableModel();
JTable employeeTable = new JTable();
employeeTable.setModel( etm );
  • 1 1 Answer
  • 0 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-06-13T21:41:31+00:00Added an answer on June 13, 2026 at 9:41 pm

    you can to start with, simplest code as is possible, maybe depends if you want to see JComboBox as Renderer too by @aterai

    import java.awt.BorderLayout;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JList;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    public class TableRenderDemo extends JPanel {
    
        private static final long serialVersionUID = 1L;
    
        public TableRenderDemo() {
            super(new BorderLayout(5, 5));
            final JTable table = new JTable(new MyTableModel());
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            table.setFillsViewportHeight(true);
            table.setRowHeight(20);
            JScrollPane scrollPane = new JScrollPane(table);
            initColumnSizes(table);
            setUpSportColumn(table, table.getColumnModel().getColumn(2));
            add(scrollPane, BorderLayout.CENTER);
            JButton resetButton = new JButton("Reset to default");
            resetButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    for (int i = 0; i < table.getRowCount(); i++) {
                        table.getModel().setValueAt("None of the above", i, 2);
                    }
                }
            });
            add(resetButton, BorderLayout.SOUTH);
        }
    
        private void initColumnSizes(JTable table) {
            MyTableModel model = (MyTableModel) table.getModel();
            TableColumn column = null;
            Component comp = null;
            int headerWidth = 0;
            int cellWidth = 0;
            Object[] longValues = model.longValues;
            TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
            for (int i = 0; i < 5; i++) {
                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;
                column.setPreferredWidth(Math.max(headerWidth, cellWidth));
            }
        }
    
        private void setUpSportColumn(JTable table, TableColumn sportColumn) {
            ArrayList<String> listSomeString = new ArrayList<String>();
            listSomeString.add("Snowboarding");
            listSomeString.add("Rowing");
            listSomeString.add("Knitting");
            listSomeString.add("Speed reading");
            listSomeString.add("Pool");
            listSomeString.add("None of the above");
            JComboBox comboBox = new JComboBox();
            comboBox.addItem(new Item(1, "-"));
            comboBox.addItem(new Item(2, "Snowboarding"));
            comboBox.addItem(new Item(3, "Rowing"));
            comboBox.addItem(new Item(4, "Knitting"));
            comboBox.addItem(new Item(5, "Speed reading"));
            comboBox.addItem(new Item(6, "Pool"));
            comboBox.addItem(new Item(7, "None of the above"));
            comboBox.setMaximumRowCount(3);
            comboBox.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    JComboBox comboBox = (JComboBox) e.getSource();
                    Item item = (Item) comboBox.getSelectedItem();
                    System.out.println(item.getId() + " : " + item.getDescription());
                }
            });
            comboBox.setRenderer(new ItemRenderer());
            sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
            renderer.setToolTipText("Click for combo box");
            sportColumn.setCellRenderer(renderer);
        }
    
        class ItemRenderer extends BasicComboBoxRenderer {
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value != null) {
                    Item item = (Item) value;
                    setText(item.getDescription().toUpperCase());
                }
                if (index == -1) {
                    Item item = (Item) value;
                    setText("" + item.getId());
                }
                return this;
            }
        }
    
        class Item {
    
            private int id;
            private String description;
    
            public Item(int id, String description) {
                this.id = id;
                this.description = description;
            }
    
            public int getId() {
                return id;
            }
    
            public String getDescription() {
                return description;
            }
    
            @Override
            public String toString() {
                return description;
            }
        }
    
        class MyTableModel extends AbstractTableModel {
    
            private static final long serialVersionUID = 1L;
            private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
            private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), false},
                {"John", "Doe", "Rowing", new Integer(3), true}, {"Sue", "Black", "Knitting", new Integer(2), false},
                {"Jane", "White", "Speed reading", new Integer(20), true}, {"Joe", "Brown", "Pool", new Integer(10), false}};
            public final Object[] longValues = {"Jane", "Kathy", "None of the above", new Integer(20), Boolean.TRUE};
    
            @Override
            public int getColumnCount() {
                return columnNames.length;
            }
    
            @Override
            public int getRowCount() {
                return data.length;
            }
    
            @Override
            public String getColumnName(int col) {
                return columnNames[col];
            }
    
            @Override
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
    
            @Override
            public Class<?> getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
    
            @Override
            public boolean isCellEditable(int row, int col) {
                if (col < 2) {
                    return false;
                } else {
                    return true;
                }
            }
    
            @Override
            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
                System.out.println("New value of data: " + getValueAt(row, col));
            }
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("TableRenderDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            TableRenderDemo newContentPane = new TableRenderDemo();
            frame.setContentPane(newContentPane);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

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.