cellenderer-使文本加粗

djmepvbi  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(305)

如何使文本在jtable粗体,我尝试了以下,它确实工作的颜色,但使文本粗体我走错了地方

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;
        }
    }
}

这是和table一起上的课

tblData.getColumn("Bib no").setCellRenderer(new CustomRenderer());
tblData.getColumn("M/F").setCellRenderer(new CustomRenderer());
sg24os4d

sg24os4d1#

你创建了一个粗体字体,但什么也不做:你从不调用它 setFont(...) 任何地方。
即。,

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());
}

对于颜色,在else块中将前景颜色设置为null,以使颜色更改回默认值。
编辑
例如:

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();
         }
      });
   }
}

相关问题