How to make a JComboBox table editor have the design of an ordinary JComboBox?
Asked Answered
F

2

2

I have a JComboBox used as an editor in a JTable. In the picture you can see them in the column labeled Produs. I would like to use the design of the stand-alone JComboBox in the grid cells, particularly the right part of the combo box where the triangle is missing from the grid cells, so a user will know that the grid cells are combo boxes without having to click on one of them. How can I apply the design of the JComboBox (IsBackFlush) to the JComboBoxes in the grid?

Essentially, how can I design comboBox2 based on the design of comboBox1? Thanks.

cropped image

Feria answered 27/6, 2013 at 12:16 Comment(22)
1) please to rewrite this question, 2) for better help sooner post an SSCCE, 3) short runnable, compilable just about a.m. issue, 4) don't understand whats is desing, AFAIK JComboBox hasn't some desingArmindaarming
not sure if you really mean contains JComboBox instances but if so: it's wrong to have components in a Table/Model, instead have data which is rendered/edited by a renderer/editor that provides suitable componentWrite
mKorbel, I would like to make my combo boxes in the grid look like the combo box outside the grid. Essentially we have two jComboBox instances and I would like to make the second look like the first. Kleopatra, the JComboBox instances are cell editors, they are not part of the model. I have shared the fact that the JComboBox instances to design to look like the other are inside a JTable, just for reference. Essentially the question is how to make a combo box to look like another combo box. If we have a solution to that I believe there will be no problem applying the solution to my comboboxes.Feria
Tip: Add @Armindaarming (or whoever) to notify them of a new comment. Only one person can be notified per comment.Regretful
@mKorbel, do you know how can I make a JTable cell to look like a JComboBox?Feria
@Lajos Arpad read oracle tutorial and rest is in commnent by (@kleopatra), btw there are a few attempts, some of them are very goodArmindaarming
@Andrew Thompson,@somebody,@somebody,@somebody I think that is possible multiple recipient in form, without spaces :-)Armindaarming
@mKorbel,@Write Huh.. I'll have to try it and see if I can make it work. Were you informed of this comment kleopatra?Regretful
still don't understand: your editor doesn't look like the stand-alone combo? That's unusual, sounds like something wrong with your setup. Or do you want the cell look like a comboBox even if not editing? If so, you need a renderer which vends a comboBox as rendering component. Anyway, your best bet now is to show a SSCCE of what you tried to explain exactly how it doesn't fulfill your requirementsWrite
@AndrewThompson,@Armindaarming no, wasn't - got notified by mkorbel's comment at lajos, guessing that's because the other at is the OPWrite
@camickr, I do not believe it is constructive to question the abilities of other fellow programmers. If the answer to a question is complicated for one, it might be simple for the other. So, your habit of telling others to step back and this or that is complicated for them is un-scientific, subjective and arrogant. Maybe you could look into yourself instead of questioning the abilities of other people. If you do not want to answer a question, you do not have to, but please, do not add poison to an otherwise constructive conversation by posting uncivilized comments. Thanks.Feria
Exactly, you questioned my ability to answer a question, even though I responded to your comments and concerns 4 times. Not a good feeling is it? As a volunteer, I made every effort to help you through the process of asking a question and getting an answer. You never explained what you didn't understand. I kept trying to reword the comments. Eventually you did understand and gave me no credit. Yes, stepping back is a valid. Once again you have not posted a SSCCE. You want people to help but are unwilling to make an effort to give us the information we need to help you. You get what you give.Equine
@camickr, I believe I gave you credit by upvoting your answer, even though your answer was not helpful for me, as I didn't understand it, because you did not explain well enough what you suggest and how should that be realized. So, I have upvoted your answer, because it was valid, but will not accept it in its current form, because it is not helpful, in the future other people will not understand it. I am not questioning your ability to answer, I just share the FACT that your answer was not helpful. It is solely your problem that you are angry that your un-helpful answer was not accepted.Feria
"I do not believe it is constructive to question the abilities of other fellow programmers" I believe you're wrong. If someone is better suited to working in the fast food industry, better to have them find out sooner, rather than later. Pander to them now and you'll first see them as the co-worker who is always pestering you with questions to which they should already know the answers, then the clueless manager who makes the decisions.Regretful
@AndrewThompson, there are questions and there are answers. There is no point to get personal. I have answered many questions and was never offending the person who asked a question just because he or she did not understand my answer, I tried to explain better instead. And I believe here we should ask questions and answer them, there is no point to act like a troll and tell people we do not answer a question because we think he or she will not understand. As you can see, our troll was successful, as we are not talking about the issue; we are talking about what's good and what's wrong.Feria
@Lajos Arpad you are part of best of posters on this site, Renderer is illusion, and you probably want to paitining JComboBox in the JTable Cells, renderer insn't editable, and editor is invoked on mouse or keyboard event, here could be used JComboBox as editor, Oracle tutorial very good to describe this area, be sure that 99pct of post about TableCellEditor and TableCellRenderer is based on this tutorial, rest of is hereArmindaarming
@AndrewThompson notification for two or more users doesn't works, only my idea, my big pardonArmindaarming
@Lajos Arpad i can't believe that this question is only once time downvoted and without any close votes, start with code example from tutorial, copy - paste this code and to ask how to use JComboBox as TableCellRenderer too, thats it, dot :-)Armindaarming
@mKorbel, I do not understand why you are sarcastic, I believe my question is valid.Feria
I'm don't joking, nor sarcastic, you didn't bothering with basic stuff described in tutorial, my sorry if you are wrongArmindaarming
@LajosArpad I have edited the question to reflect my understanding of the problem; please edit to correct any misunderstandings.Grata
FYI, you may want to check out this question (https://mcmap.net/q/1781759/-jcombobox-shared-between-multiple-table-cells-automatically-selecting-currently-selected-item/131872), which is a duplicate of your previous question. Hopefully I did a better job answering the question :) But, also note the person did a better job of asking the question by posting a simplified SSCCE. This is what I am talking about when I say step back and start with a simple example.Equine
G
5

Working from this complete example as a common frame of reference, note how the appearance of unselected cells in the ITEM_COL column is due to the default renderer. The arrow button typical of a stand-alone JComboBox only appears when the cell's editor is evoked, as by clicking on the cell or pressing Space when the cell is selected. You can add a triangle in a custom renderer:

final JComboBox combo = new JComboBox(items);
TableColumn col = table.getColumnModel().getColumn(ITEM_COL);
col.setCellRenderer(new DefaultTableCellRenderer(){

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel label = (JLabel) super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);
        label.setIcon(UIManager.getIcon("Table.descendingSortIcon"));
        return label;
    }
});

Addendum: A more complete example due to @aterai is seen here.

Grata answered 27/6, 2013 at 16:19 Comment(2)
With some small changes on this code I have finally solved the problem. I accept your solution (I already upvoted it). The additional calls were: label.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);Feria
+1, a label with an arrow renders better than an actual combo box.Equine
E
3

In the spirit of moving forward I was going to post the solution that I was talking about earlier. This solution actually uses comboBox as a renderer. So it will look the same as a real comboBox in any LAF. The problem is that the text is squished, so I like trashgod's implementation better.

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public TableComboBoxByRow()
    {
        // Create the editors to be used for each row

        String[] items1 = { "Red", "Blue", "Green" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

        String[] items2 = { "Circle", "Square", "Triangle" };
        JComboBox comboBox2 = new JComboBox( items2 );
        DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
        editors.add( dce2 );

        String[] items3 = { "Apple", "Orange", "Banana" };
        JComboBox comboBox3 = new JComboBox( items3 );
        DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
        editors.add( dce3 );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                    return editors.get(row);
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
        table.getColumnModel().getColumn(1).setCellRenderer(new ComboBoxRenderer() );
    }

    class ComboBoxRenderer extends JComboBox implements TableCellRenderer
    {

        public ComboBoxRenderer()
        {
            setBorder(new EmptyBorder(0, 0, 0, 0));
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column)
        {
//          setFocusable(false);
            removeAllItems();
            addItem( value );
            return this;
        }
    }

    public static void main(String[] args)
    {
        TableComboBoxByRow frame = new TableComboBoxByRow();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}
Equine answered 27/6, 2013 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.