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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:28:38+00:00 2026-05-27T11:28:38+00:00

I want to add some JSeparators to a JList. I am using a DefaultListModel,

  • 0

I want to add some JSeparators to a JList. I am using a DefaultListModel, and when I try:

((DefaultListModel)myListModel).addElement(new JSeparator());

I’m getting this written into the JList instead of the separator when I execute:

javax.swing.JSeparator[,0,0,0×0,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@962d513,flags=0,maximumSize=,minimumSize=,preferredSize=,orientation=HORIZONTAL]

  • 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-05-27T11:28:38+00:00Added an answer on May 27, 2026 at 11:28 am

    little bit complicated in the renderer but possible, since example for JComboBox but with ListCellRenderer

    enter image description here

    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class BlockComboBoxExample extends JFrame {
    
        private static final long serialVersionUID = 1L;
        final String SEPARATOR = "SEPARATOR";
    
        public BlockComboBoxExample() {
            super("Block ComboBox Example");
            String[][] str = {{"A", "B", "C"}, {"1", "2", "3"}, {"abc", "def", "ghi"}};
            JComboBox combo = new JComboBox(makeVectorData(str));
            combo.setRenderer(new ComboBoxRenderer());
            combo.addActionListener(new BlockComboListener(combo));
            combo.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXX");
            setLayout(new FlowLayout());
            add(combo);
            pack();
            setVisible(true);
        }
    
        private Vector<String> makeVectorData(String[][] str) {
            boolean needSeparator = false;
            Vector<String> data = new Vector<String>();
            for (int i = 0; i < str.length; i++) {
                if (needSeparator) {
                    data.addElement(SEPARATOR);
                }
                for (int j = 0; j < str[i].length; j++) {
                    data.addElement(str[i][j]);
                    needSeparator = true;
                }
            }
            return data;
        }
    
        public static void main(String args[]) {
            try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    //UIManager.getLookAndFeelDefaults().put("Panel.background", Color.white);
                    //UIManager.getLookAndFeelDefaults().put("Button.contentMargins", new InsetsUIResource(0,0,0,0));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
            BlockComboBoxExample frame = new BlockComboBoxExample();
            frame.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    
        private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    
            private static final long serialVersionUID = 1L;
            private JSeparator separator;
    
            public ComboBoxRenderer() {
                setOpaque(true);
                setBorder(new EmptyBorder(1, 1, 1, 1));
                separator = new JSeparator(JSeparator.HORIZONTAL);
            }
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                String str = (value == null) ? "" : value.toString();
                if (SEPARATOR.equals(str)) {
                    return separator;
                }
                if (isSelected) {
                    setBackground(list.getSelectionBackground());
                    setForeground(list.getSelectionForeground());
                } else {
                    setBackground(list.getBackground());
                    setForeground(list.getForeground());
                }
                setFont(list.getFont());
                setText(str);
                return this;
            }
        }
    
        private class BlockComboListener implements ActionListener {
    
            private JComboBox combo;
            private Object currentItem;
    
            BlockComboListener(JComboBox combo) {
                this.combo = combo;
                combo.setSelectedIndex(0);
                currentItem = combo.getSelectedItem();
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                String tempItem = (String) combo.getSelectedItem();
                if (SEPARATOR.equals(tempItem)) {
                    combo.setSelectedItem(currentItem);
                } else {
                    currentItem = tempItem;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add some style to head tag in html page using javascript.
I want to add some elements to a TextBox by using a Template with
I want to add some custom fields to add new user in Wordpress .
I want to add some functionality to every module/plugin written such as: Author, Company,
I want to add some text such as Manula, to the image using Stenography
I want to add some new functions to the core string helper, which is
I want to add some text to a screen. Since this text changes color
I want to add some rows to a database using Linq to SQL, but
I want to add some feature to this project and I need smartgwt for
I want to add some divs using jQuery in a main div called activity-div

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.