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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:59:37+00:00 2026-06-09T14:59:37+00:00

I found a Dual JList sample code, but I need two columns in a

  • 0

I found a Dual JList sample code, but I need two columns in a Jlist.
How to add two columns in a Jlist?

I tried to use ListCellRenderer, but I failed to add elements to the model.

Here’s the code show in webpage with image.

  • 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-09T14:59:39+00:00Added an answer on June 9, 2026 at 2:59 pm

    You mean something like this?

    Dual Wielding Tables

    I’d start by having a read through How to Use Tables

    The Main frame…

    public class DualTableFrame extends JFrame {
    
        private JTable leftTable;
        private JTable rightTable;
        private JButton addButton;
        private JButton removeButton;
    
        public DualTableFrame() {
    
            setTitle("Dual wielding tables");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            setLayout(new GridBagLayout());
    
            leftTable = new JTable(new SimpleColorTableModel());
            rightTable = new JTable(new SimpleColorTableModel());
    
            setupTable(leftTable);
            setupTable(rightTable);
    
            populate((SimpleColorTableModel) leftTable.getModel());
    
            addButton = new JButton("Add >>");
            removeButton = new JButton("<< Remove");
    
            JPanel pnlActions = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            pnlActions.add(addButton, gbc);
    
            gbc.gridy++;
            pnlActions.add(removeButton, gbc);
    
            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 0.33;
            gbc.fill = GridBagConstraints.HORIZONTAL;
    
            add(new JLabel("Available Choices"), gbc);
            gbc.gridx++;
            add(new JPanel(), gbc);
            gbc.gridx++;
            add(new JLabel("Your Choices"), gbc);
    
            gbc.gridy++;
            gbc.gridx = 0;
            gbc.weighty++;
            gbc.fill = GridBagConstraints.BOTH;
    
            add(new JScrollPane(leftTable), gbc);
            gbc.gridx++;
            add(pnlActions, gbc);
            gbc.gridx++;
            add(new JScrollPane(rightTable), gbc);
    
            setSize(400, 600);
    
            addButton.setEnabled(false);
            removeButton.setEnabled(false);
    
            leftTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
    
                    int count = leftTable.getSelectedRowCount();
                    addButton.setEnabled(count > 0);
    
                }
            });
            rightTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
    
                    int count = rightTable.getSelectedRowCount();
                    removeButton.setEnabled(count > 0);
    
                }
            });
    
            addButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
    
                    moveSelectedRow(leftTable, rightTable);
    
                }
            });
    
            removeButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
    
                    moveSelectedRow(rightTable, leftTable);
    
                }
            });
    
        }
    
        protected void moveSelectedRow(JTable from, JTable to) {
    
            SimpleColorTableModel fromModel = (SimpleColorTableModel) from.getModel();
            SimpleColorTableModel toModel = (SimpleColorTableModel) to.getModel();
    
            for (int index : from.getSelectedRows()) {
    
                Vector rowValue = (Vector) fromModel.getDataVector().get(index);
    
                toModel.addRow(rowValue);
    
            }
    
            int selectedRow = -1;
            while ((selectedRow = from.getSelectedRow()) != -1) {
    
                fromModel.removeRow(selectedRow);
    
            }
    
            from.clearSelection();
    
        }
    
        protected void populate(SimpleColorTableModel model) {
    
            model.addRow(new Object[]{"Black", Color.BLACK});
            model.addRow(new Object[]{"Blue", Color.BLUE});
            model.addRow(new Object[]{"Cyan", Color.CYAN});
            model.addRow(new Object[]{"Dark Gray", Color.DARK_GRAY});
            model.addRow(new Object[]{"Gray", Color.GRAY});
            model.addRow(new Object[]{"Green", Color.GREEN});
            model.addRow(new Object[]{"Light Gary", Color.LIGHT_GRAY});
            model.addRow(new Object[]{"Mangenta", Color.MAGENTA});
            model.addRow(new Object[]{"Orange", Color.ORANGE});
            model.addRow(new Object[]{"Pink", Color.PINK});
            model.addRow(new Object[]{"Red", Color.RED});
            model.addRow(new Object[]{"White", Color.WHITE});
            model.addRow(new Object[]{"Yellow", Color.YELLOW});
    
        }
    
        protected void setupTable(JTable table) {
    
            table.setFillsViewportHeight(true);
    
            table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
    
        }
    }
    

    The Table cell renderer

    public class ColorTableCellRenderer extends DefaultTableCellRenderer {
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            setText(null);
            if (value instanceof Color) {
    
                setOpaque(true);
                setBackground((Color)value);
    
            }
    
            return this;
    
        }
    
    }
    

    The table model

    public class SimpleColorTableModel extends DefaultTableModel {
    
        public SimpleColorTableModel() {
    
            addColumn("Name");
            addColumn("Color");
    
        }
    
        @Override
        public Class<?> getColumnClass(int columnIndex) {
    
            Class clazz = String.class;
    
            switch (columnIndex) {
    
                case 1:
                    clazz = Color.class;
                    break;
    
            }
    
            return clazz;
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely new to python but I found a package that I need
Reviewing our code I've found a curious definition in one of .idl files: [
I've found the following error in my application's error log file. ERROR [org.sample.dao.hibernate.LoginDAOImpl] org.sample.dao.hibernate.LogonDAOImpl
I've decided to add more tablet-friendly UI to my app by creating a dual-pane
I tried to do the following: SQL> select 1>2 from dual; select 1>2 from
I've read a number of SO questions on this topic, but grokking the applied
This probably isn't as complicated as it should be, but Business Objects seems to
I found a funny issue with DB2 v9.7 and the SQL LIKE operator. Check
I have a package that gets invalidated on a regular basis and found this
If I have a class, which has dual dependencies on the same type (needs

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.