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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:41:55+00:00 2026-06-06T16:41:55+00:00

import javax.swing.*; public class test { public static void main(String[] args) throws Exception {

  • 0
import javax.swing.*;

public class test
{   
    public static void main(String[] args) throws Exception
    {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(120,80);      
        JComboBox cb = new JComboBox();
        cb.addItem("A very long combo-box item that doesn't fit no. 1");
        cb.addItem("A very long combo-box item that doesn't fit no. 2");
        frame.add(cb);
        frame.validate();
        frame.setVisible(true);
    }
}

How can I make combo-box items to appear in the way that all their text is visible?
Now I have something like this:
enter image description here
I don’t want to change size of combo-box while collapsed.
I just want to increase width of the expanded part.

  • 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-06T16:41:57+00:00Added an answer on June 6, 2026 at 4:41 pm

    I think this may help.

    Code from link

    public class WiderDropDownCombo extends JComboBox {
    
        private String type;
        private boolean layingOut = false;
        private int widestLengh = 0;
        private boolean wide = false;
    
        public WiderDropDownCombo(Object[] objs) {
            super(objs);
        }
    
        public boolean isWide() {
            return wide;
        }
    
        // Setting the JComboBox wide
        public void setWide(boolean wide) {
            this.wide = wide;
            widestLengh = getWidestItemWidth();
    
        }
    
        public Dimension getSize() {
            Dimension dim = super.getSize();
            if (!layingOut && isWide())
                dim.width = Math.max(widestLengh, dim.width);
            return dim;
        }
    
        public int getWidestItemWidth() {
    
            int numOfItems = this.getItemCount();
            Font font = this.getFont();
            FontMetrics metrics = this.getFontMetrics(font);
            int widest = 0;
            for (int i = 0; i < numOfItems; i++) {
                Object item = this.getItemAt(i);
                int lineWidth = metrics.stringWidth(item.toString());
                widest = Math.max(widest, lineWidth);
            }
    
            return widest + 5;
        }
    
        public void doLayout() {
            try {
                layingOut = true;
                super.doLayout();
            } finally {
                layingOut = false;
            }
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String t) {
            type = t;
        }
    
        public static void main(String[] args) {
            String title = "Combo Test";
            JFrame frame = new JFrame(title);
    
            String[] items = {
                    "I need lot of width to be visible , oh am I visible now",
                    "I need lot of width to be visible , oh am I visible now" };
            WiderDropDownCombo simpleCombo = new WiderDropDownCombo(items);
            simpleCombo.setPreferredSize(new Dimension(180, 20));
            simpleCombo.setWide(true);
            JLabel label = new JLabel("Wider Drop Down Demo");
    
            frame.getContentPane().add(simpleCombo, BorderLayout.NORTH);
            frame.getContentPane().add(label, BorderLayout.SOUTH);
            int width = 200;
            int height = 150;
            frame.setSize(width, height);
            frame.setVisible(true);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Sheet extends JFrame{ private String[] line
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class test extends JFrame implements MouseListener {
I have the following: import javax.swing.JFrame; public class Directions { public Directions(){ JFrame frame
package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
import java.awt.Graphics; import javax.swing.*; public class Demo { JFrame jf; JLabel[] labels; JPanel panel;
Here is my code... import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class
import java.awt.*; import java.awt.image.ImageObserver; import javax.swing.*; public class CharacterMove { CharacterCollision CharacterCollision; public static
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {

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.