0

Even though there are many examples on how to put a JCombobox instead of a textfield on SO (by switching between two editors), what I want is a little bit more complicated.

I use the following method to populate my table:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col )
{

    if (((JobCustomWorkUnit)param.getModel()).isEditbale(row))
    {
        if (col == 1)
            varLabel.setEnabled(true);
    }
    else
    {
        if (col == 1)
            varLabel.setEnabled(false);
    }

    switch( col )
    {
        case 0  :
            renderJTable.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);

            if (isSelected)
            {
                renderJTable.setBackground(table.getSelectionBackground());
            }
            else
            {
                if (((JobCustomWorkUnit)param.getModel()).isMandatory(row))
                    renderJTable.setBackground(LIGHT_YELLOW);
                else
                    renderJTable.setBackground(UIManager.getColor("Table.background"));
            }
            return renderJTable;

        case 1  :
            varLabel.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

            if (isSelected)
            {
                renderJTable.setBackground(table.getSelectionBackground());
            }
            else
            {
                if (((JobCustomWorkUnit)param.getModel()).isMandatory(row))
                    varLabel.setBackground(LIGHT_YELLOW);
                else
                    varLabel.setBackground(UIManager.getColor("Table.background"));
            }

            String valueStr = value.toString();
            String defaultStr =((JobCustomWorkUnit)param.getModel()).getDefault(row);
            String type = ((JobCustomWorkUnit)param.getModel()).getType(row);
            if (valueStr == null || valueStr.length() == 0 ) {
                varLabel.setVText(defaultStr);
            }
            if (((JobCustomWorkUnit)param.getModel()).isValueCrypt(row) || type.equalsIgnoreCase("password"))
            {
                varLabel.setVText("********");
            }

            if (type.equalsIgnoreCase("user")){
            }
            return varLabel;
    }
    return null;
}

The varLabel variable here is an instance of a custom class where the setVText() method is the following:

public void setVText( String s )
{
    int             pos;
    String          ss;
    VariableView    vv;

    if( s.equals(vtext) ) return;
    vtext = s;
    tokens.clear();
    try
    {
        pos = 0;

        ArrayList<MatchResult> matches = getAllMatches(s, REGEXP_EXP);

        for( MatchResult m : matches )
        {
            // R�cup�rer la cha�ne qui n'est pas une variable
            ss = s.substring( pos, m.start() );
            tokens.add( ss );

            // R�cuperer xxxx qui correspond � une variable dans <$xxxxx>
            ss = m.group(2);
            //tab[i].toString(1);
            vv = createVariableView( ss );
            if( vv != null ) tokens.add( vv );
            pos = m.end(1);
        }

        // Inserer la fin
        ss = s.substring( pos );
        tokens.add( ss );
    }
    catch( Exception ex )
    {
        LogFactory.createLog().error( "Erreur la chaine avec Variables semble �tre mal format�", ex );
        tokens.clear();
    }
}

When the type equals a string I use the above method to put the text in the cell.

And as you can see, I'm using two different getTableCellRendererComponent() here:

When the column equals 0, I use the default getTableCellRendererComponent from DefaultTableCellRenderer.

When the column equals 1, I use the one I have in class VarLabel, which is the following:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    RENDER.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
    setFont( RENDER.getFont() );
    setBorder( RENDER.getBorder() );
    setForeground( RENDER.getForeground() );
    setBackground( RENDER.getBackground() );
    setVText( value.toString() );
    setOpaque( true );

    return this;
}

Here's the tricky part. What I would like to do now is to draw a JComboBox instead of a textfield when type is equal to "user".

        if (type.equalsIgnoreCase("user")){
        //so instead of doing varLabel.setVText(value) here and end up having a textfield, I would like to put a JComboBox instead.
        }

Can this be done?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jesse James
  • 1,203
  • 5
  • 22
  • 39
  • Thanks for the tip. I edited my question and removed the code of `VarLabel` class. – Jesse James Mar 26 '20 at 15:29
  • Not clear what the problem is. Since getTableCellRendererComponent() knows the value to render, it can inspect it and return the suitable renderer. – Olivier Mar 26 '20 at 15:36
  • `getTableCellRendererComponent()` from the `VarLabel` class always return an editable textfield with the parameter`value` as its value. What I would like to do is to make it return a JComboBox when the variable `type` is equal to "user". – Jesse James Mar 26 '20 at 15:42

0 Answers0