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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:40:01+00:00 2026-06-12T14:40:01+00:00

I would like to have a ComboBox displaying some different line styles, such as

  • 0

I would like to have a ComboBox displaying some different line styles, such as solid, dotted, dashed etc.

How to create a custom render to accomplish this?

Thanks all.

  • 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-12T14:40:02+00:00Added an answer on June 12, 2026 at 2:40 pm

    The good way to do this is to use a CustomRenderer. You can either use predefined images or paint the line stroke on the fly. Here is an example of the latter option:

    import java.awt.BasicStroke;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagLayout;
    import java.awt.Stroke;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.ListCellRenderer;
    import javax.swing.SwingUtilities;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestComboBox {
    
        private static enum LineType {
    
            PLAIN {
                @Override
                public Stroke getStroke() {
                    return new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 1);
                }
            },
            DOTTED {
                @Override
                public Stroke getStroke() {
                    return new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f, new float[] { 0.1f, 5.0f }, 1);
                }
    
            },
            DASHED {
                @Override
                public Stroke getStroke() {
                    return new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f, new float[] { 3.0f, 3.0f }, 1);
                }
    
            };
            public abstract Stroke getStroke();
        }
    
        public class LineRenderer extends JPanel implements ListCellRenderer {
            private LineType value;
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                if (value instanceof LineType) {
                    setLineType((LineType) value);
                } else {
                    setLineType(null);
                }
                return this;
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                if (value != null) {
                    g2d.setStroke(value.getStroke());
                    g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
                }
    
            }
    
            private void setLineType(LineType value) {
                this.value = value;
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(50, 20);
            }
    
        }
    
        protected void initUI() {
            final JFrame frame = new JFrame(TestComboBox.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel(new GridBagLayout());
            final JComboBox comboBox = new JComboBox(LineType.values());
            comboBox.setRenderer(new LineRenderer());
            comboBox.setSelectedItem(null);
            comboBox.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            JOptionPane.showMessageDialog(comboBox, "You have selected " + comboBox.getSelectedItem());
                        }
                    });
                }
            });
            panel.add(comboBox);
            frame.add(panel);
            frame.setSize(300, 100);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestComboBox().initUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have a ComboBox and I would like to bind to two different properties,
I have the following collection which i would like to bind to a combobox:
I would like to have a web server displaying the status of 2 of
I would like to have a ComboBox control on a form which will be
I would like to create a ComboBox like you can see in the following
I have a combobox widget in python that I would like to be able
I would like to take the xaml I currently have for a ComboBox (below),
I have a dataGridView with comboBox dropdowns for each cell. I would like to
I would like to have the standard silverlight combobox behave like an html combobox.
Say i have a combobox with apples apples pears oranges oranges i would like

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.