Results 1 to 3 of 3
  1. #1
    javabud is offline Member
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    2
    Rep Power
    0

    Default Creating multiline item in JComboBox

    I am trying to create multiline items in JComboBox. The combo box items should be able to accommodate text of different lengths and displayed in different height and lines.

    I have attempted to add JTextArea using setCellRenderer for JComboBox, but the text entered is not wrapped around.

    I have attempted setLineWrap and the setWordWrap methods in JComboBox but do not work.

    Anyone with similar experience or idea to work around this problem. Thank you in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class MultiLine {
        private JPanel getContent() {
            String[] lines = {
                "<html>A day in<br>the life",
                "<html>hello<br>world",
                "<html>sinew strength<br>is very strong"
            };
            JComboBox combo = new JComboBox(lines);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(combo, new GridBagConstraints());
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new MultiLine().getContent());
            f.setSize(300,150);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    javabud is offline Member
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    2
    Rep Power
    0

    Default Multiline resolved plus a few more new head-cracking requirments

    Thanks Hardwired.
    Your posting may be helpful later on as I also plan to provide RTF text, i.e. underline, bold, or italicize.

    My software requirement is to have an editable JTable cell, which gives user option to pick from the dropdown items in JComboBox if available, otherwise to enter new text.

    After tweaking and some luck, I manage to get the wrapping working. In the cell renderer, I set the setEditable(boolean) argument to false, as below:

    private class ComboBoxListCellRenderer extends JTextArea implements ListCellRenderer, TableCellRenderer, java.io.Serializable {
    public ComboBoxListCellRenderer(){
    setPreferredSize(new Dimension(10,20));
    setToolTipText("Select drop down or enter new text.");
    setFont(new Font(Font.SERIF,Font.PLAIN,10));
    setLineWrap(true);
    setWrapStyleWord(true);
    setEditable(false);
    setBorder(null);
    validate();
    }

    Initially, the argument had been set to true; and that seemingly disabled setLineWrap settings. No idea why the argument is such a big deal.

    Anyway, right now, I am working on the JTable cell height to automatically accomodate different heights of the cell in each row.
    www_chka_de/swing/table/cell-sizes_html
    Will keep the result of the work posted later.

    Also, I would like to check about doing the following in the JTable cell:
    1. Press Ctrl + Enter to add newline
    2. Press Ctrl + B to bold the highlighted text in the cell.
    3. Press Ctrl + U to underline text in the cell.

    Any one please?

Similar Threads

  1. To get multiline tooltip
    By diva_garg in forum New To Java
    Replies: 8
    Last Post: 03-25-2011, 04:16 PM
  2. How to write multiline String in a JLabel
    By JavaBean in forum AWT / Swing
    Replies: 4
    Last Post: 12-14-2009, 05:09 AM
  3. Multiline Regex
    By zriggle in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 04:49 AM
  4. [SOLVED] Item too big for JComboBox
    By nwboy74 in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 08:21 PM
  5. grabbing an item from an arraylist.
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •