JTable in JComboBox

Discussion in 'Java' started by lizard, Jan 27, 2006.

  1. lizard

    lizard Guest

    I am attempting to build a combobox that uses a table
    instead of a list. The user should be able to select
    a row in the table. As the mouse moves over the table,
    the row should be highlighted.

    I am looking at the source code for the combobox and
    it is overwhelming. Does someone know if something
    like the above has been implemented? If not, any suggest
    as how I go about implement such combobox?

    Thanks.
     
    lizard, Jan 27, 2006
    #1
    1. Advertising

  2. lizard

    Bart Cremers Guest

    You should not look at re-implementing JComboBox but simply implement a
    custom ListCellRenderer. Instead of the default JLabel used by the
    DefaultListCellRenderer create on that extends JTable and work on that
    one.

    Bart
     
    Bart Cremers, Jan 27, 2006
    #2
    1. Advertising

  3. lizard

    zero Guest

    lizard <> wrote in
    news:p:

    >
    > I am attempting to build a combobox that uses a table
    > instead of a list. The user should be able to select
    > a row in the table. As the mouse moves over the table,
    > the row should be highlighted.
    >
    > I am looking at the source code for the combobox and
    > it is overwhelming. Does someone know if something
    > like the above has been implemented? If not, any suggest
    > as how I go about implement such combobox?
    >
    > Thanks.
    >
    >


    Bart's suggestion (a ListCellRenderer that extends JTable) is indeed the
    way to go. I just wanted to ask, when you get it to work, would you mind
    posting the code here or on a website somewhere? Sounds like something
    that could potentially be useful to others as well.
     
    zero, Jan 27, 2006
    #3
  4. lizard

    Bart Cremers Guest

    Here's a working example, not perfect, but could be used as starters.

    import java.awt.Component;
    import java.awt.FlowLayout;
    import javax.swing.*;
    import javax.swing.table.AbstractTableModel;

    /**
    * @author Bart Cremers
    */
    public class TableCombo extends JFrame {

    protected void frameInit() {
    super.frameInit();

    setLayout(new FlowLayout());

    JComboBox box = new JComboBox(new Row[] {
    new Row("Row 1", "Row 1 value", "Row 1 Extra"),
    new Row("Row 2", "Row 2 value", "Row 2 Extra"),
    new Row("Row 3", "Row 3 value", "Row 3 Extra"),
    new Row("Row 4", "Row 4 value", "Row 4 Extra"),
    new Row("Row 5", "Row 5 value", "Row 5 Extra"),
    new Row("Row 6", "Row 6 value", "Row 6 Extra"),
    new Row("Row 7", "Row 7 value", "Row 7 Extra"),
    new Row("Row 8", "Row 8 value", "Row 8 Extra"),
    new Row("Row 9", "Row 9 value", "Row 9 Extra"),
    });

    box.setRenderer(new RowCellRenderer());

    add(box);
    }

    public static void main(String[] args) {
    JFrame f = new TableCombo();
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setVisible(true);
    }

    private static class Row {

    private String id, val, extra;

    public Row(String id, String val, String extra) {
    this.id = id;
    this.val = val;
    this.extra = extra;
    }

    public String getId() {
    return id;
    }

    public String getVal() {
    return val;
    }

    public String getExtra() {
    return extra;
    }
    }

    private static class RowCellRenderer extends JTable implements
    ListCellRenderer {

    public RowCellRenderer() {

    setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    }

    public Component getListCellRendererComponent(JList list,
    Object value, int index, boolean isSelected,
    boolean
    cellHasFocus) {
    setModel(new RowTableModel((Row) value));
    if (isSelected) {
    getSelectionModel().setSelectionInterval(0, 0);
    }
    return this;
    }
    }

    private static class RowTableModel extends AbstractTableModel {

    private Row row;

    public RowTableModel(Row row) {
    this.row = row;
    }

    public int getRowCount() {
    return 1;
    }

    public int getColumnCount() {
    return 3;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
    switch(columnIndex) {
    case 0:
    return row.getId();
    case 1:
    return row.getVal();
    case 2:
    return row.getExtra();
    }
    return null;
    }
    }
    }

    Bart
     
    Bart Cremers, Jan 30, 2006
    #4
  5. lizard

    zero Guest

    "Bart Cremers" <> wrote in news:1138606078.948149.194740
    @g14g2000cwa.googlegroups.com:

    > Here's a working example, not perfect, but could be used as starters.
    >


    <code snipped>

    >
    > Bart
    >
    >


    Looks nice, thanks :)
     
    zero, Jan 30, 2006
    #5
  6. lizard

    lizard Guest

    On Sun, 29 Jan 2006 23:27:58 -0800, Bart Cremers wrote:

    > Here's a working example, not perfect, but could be used as starters.



    Thanks. :)

    Assume that I have a large number of columns (20+) and that the
    first column contains the ID of each rows. Can I display the
    ID in the field above the dropped down table rather than the
    entire row?

    And what if I rather than constructs a table for each list item,
    can I simply use a single table instead of a list of tables? This
    is because it would be nice to have the headers.
     
    lizard, Jan 30, 2006
    #6
  7. lizard

    Bart Cremers Guest

    Displaying only the ID in the field is just a matter of working on the
    renderers.

    The reason I use one table per row is that the cellrenderers in a List
    require one single renderer per row. Implementing something like you
    said is best achieved through your own component I think.

    [textfield][button]
    [window with JTable ]

    Is not that hard to implement.

    Regards,

    Bart
     
    Bart Cremers, Feb 1, 2006
    #7
  8. lizard

    valprivate

    Joined:
    Jul 27, 2012
    Messages:
    1
    Hi, guys! It's really good working example but I can't implement only one column dislpaying in comboBox. :trytofly: Need help.
     
    valprivate, Jul 27, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tivo Escobar
    Replies:
    1
    Views:
    7,350
    manusa
    Apr 12, 2007
  2. Chris
    Replies:
    0
    Views:
    8,257
    Chris
    Jun 14, 2005
  3. Yulia
    Replies:
    1
    Views:
    681
    Andrew Thompson
    Jul 4, 2005
  4. Replies:
    1
    Views:
    732
    Thomas Fritsch
    Aug 9, 2006
  5. sonaljain

    JComboBox not visible in JTable

    sonaljain, Mar 18, 2011, in forum: Java
    Replies:
    0
    Views:
    424
    sonaljain
    Mar 18, 2011
Loading...

Share This Page