CellRenderer - Making text bold
Asked Answered
C

1

5

How to make the text in JTable bold, I tried the following, It does work for colours but for making the text bold i'm going wrong somewhere

    class ColourRender extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable tblData,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        Component cellComponent = super.getTableCellRendererComponent(
                tblData, value, isSelected, hasFocus, row, column);

        if (tblData.getValueAt(row, 3).equals("M")) {
            cellComponent.setForeground(forMen);

        } else {
            cellComponent.setForeground(forWomen);

        }
        return cellComponent;
    }

    class boldRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable tblData,
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            Component cellComponent = super.getTableCellRendererComponent(
                    tblData, value, isSelected, hasFocus, row, column);

            if (tblData.getValueAt(row, 1).equals(bib)) {
                cellComponent.setFont(cellComponent.getFont().deriveFont(
                        Font.BOLD));
            } else {
                cellComponent.setFont(cellComponent.getFont().deriveFont(
                        Font.PLAIN));
            }

            return cellComponent;
        }
    }
}

and this in the class that with the table

tblData.getColumn("Bib no").setCellRenderer(new CustomRenderer());
tblData.getColumn("M/F").setCellRenderer(new CustomRenderer());
Colon answered 11/3, 2014 at 12:1 Comment(0)
O
8

You create a bold font but do nothing with it: You never call setFont(...) anywhere.

i.e.,

if (tblData.getValueAt(row, 1).equals(bib)) {
   // you must call setFont(...) for a font to change
   cellComponent.setFont(cellComponent.getFont().deriveFont(Font.BOLD));
} else {  
   // don't forget to set the font back if condition not true
   cellComponent.setFont(cellComponent.getFont().deriveFont(Font.PLAIN));
}

// changing foreground color:
if (tblData.getValueAt(row, 3).equals("M")) {
   cellComponent.setForeGround(MEN_COLOR); // a constant
} else {  
   cellComponent.setForeGround(myTable.getForeground());
}

For colors, set the foreground color to null in your else block to have your colors change back to default.


Edit
For example:

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class CustomRendererExample extends JPanel {
   public static final String[] COLUMNS = { "M/F", "Fubar" };
   public static final String[] FUBARS = { "Fubar", "Snafu" };
   public static final String[] M_F = { "M", "F" };
   protected static final Color M_COLOR = Color.red;
   private DefaultTableModel dm = new DefaultTableModel(COLUMNS, 5);
   private JTable table = new JTable(dm);

   public CustomRendererExample() {
      add(new JScrollPane(table));
      DefaultCellEditor mfEditor = new DefaultCellEditor(new JComboBox<>(M_F));
      table.getColumn(COLUMNS[0]).setCellEditor(mfEditor);
      table.getColumn(COLUMNS[0]).setCellRenderer(
            new DefaultTableCellRenderer() {
               @Override
               public Component getTableCellRendererComponent(JTable table,
                     Object value, boolean isSelected, boolean hasFocus,
                     int row, int column) {
                  Component superRenderer = super.getTableCellRendererComponent(table, 
                        value, isSelected, hasFocus, row, column);

                  if (value != null && value.equals(M_F[0])) {
                     superRenderer.setForeground(M_COLOR);
                  } else {
                     superRenderer.setForeground(null);
                  }

                  return superRenderer;
               }
            });

      DefaultCellEditor fubarEditor = new DefaultCellEditor(new JComboBox<>(
            FUBARS));
      table.getColumn(COLUMNS[1]).setCellEditor(fubarEditor);
      table.getColumn(COLUMNS[1]).setCellRenderer(
            new DefaultTableCellRenderer() {
               @Override
               public Component getTableCellRendererComponent(JTable table,
                     Object value, boolean isSelected, boolean hasFocus,
                     int row, int column) {
                  Component superRenderer = super.getTableCellRendererComponent(table, 
                        value, isSelected, hasFocus, row, column);

                  if (value != null && value.equals(FUBARS[0])) {
                     superRenderer.setFont(superRenderer.getFont().deriveFont(Font.BOLD));
                  } else {
                     superRenderer.setFont(superRenderer.getFont().deriveFont(Font.PLAIN));
                  }

                  return superRenderer;
               }
            });

   }

   private static void createAndShowGui() {
      CustomRendererExample mainPanel = new CustomRendererExample();

      JFrame frame = new JFrame("Custom Renderer Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Oology answered 11/3, 2014 at 12:2 Comment(8)
thanks, how would i have to change it to have the colour change as well? because now its bold and coloured? tblData.getColumn("M/F").setCellRenderer(new CustomRenderer()); if (tblData.getValueAt(row, 3).equals("M")) { cellComponent.setForeground(forMen); }Colon
@user3405919: we can't read code in comments. Edit your question with the new code added to the bottom if you still have a question. Also, you should be able how to set the foreground color here, no? You set the color to null in the else block.Oology
i just tried what you gave, but maybe im doing something wrong but the table still goes with both changes, so changes the colour and makes it bold, edited the codeColon
@Colon cellComponent.setForeground(null); should be cellComponent.setForeground(myJTable.getForeground()), test for value isn't proper too, and etc.. , see this oneBarbarity
@user3405919: You should use separate cell renderers for each separate column.Oology
okay, but then i get "No enclosing instance of type Window3 is accessible. Must qualify the allocation with an enclosing instance of type Window3 (e.g. x.new A() where x is an instance of Window3)." as errorColon
@user3405919: if you're still stuck, time to create and post a minimal example program.Oology
@user3405919: for example, my minimal example program has been created and posted above.Oology

© 2022 - 2024 — McMap. All rights reserved.