摇摆 - 是否有可能建立一个JTable细胞内的“特定”文本的字体颜色? [英] Swing - Is it possible to set the font color of 'specific' text within a JTable cell?

查看:125
本文介绍了摇摆 - 是否有可能建立一个JTable细胞内的“特定”文本的字体颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,其中一列显示以下格式的值:

  423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]

我想知道是否有可能以红色显示在方括号内的值?

我一直在谷歌上搜索周围的最后几天,并已发现了几个例子,说明如何设置单元格的背景,但没有真正如何更改单元格的字体特别是没有文字的特定部分。

 公共类myTableCellRenderer
       扩展DefaultTableCellRenderer {
  公共组件getTableCellRendererComponent(JTable的表,
                                                 对象的值,
                                                 布尔isSelected,
                                                 布尔hasFocus,
                                                 INT行,
                                                 INT列){
    组件C =
      super.getTableCellRendererComponent(表,价值,
                                          isSelected,hasFocus,
                                          行列);    如果(列== 3){
       c.setForeground(Color.YELLOW);
       c.setBackground(Color.RED);
    }
    返回℃;
  }

难道真的可以改变文本的一部分,以不同的颜色(即即方括号中的文字)。

修改

我发现作为一个例子中的文本在表格单元格显示的实际文本(逗号分隔符不会再presenting列)。在单元格中显示的文本是一个逗号分隔的字符串,我列在表上显示3。

作为一个例子表可能看起来像这样

 的product_id | PRODUCT_NAME | invoice_numbers
12 |书籍| 423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]
323 |视频| 423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]
4434 |音乐| 423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]


解决方案

您必须使用单元格渲染和HTML结合起来。

下面是一个小的演示例如:

 进口java.awt.BorderLayout中;
进口java.awt.Component中;
进口java.util.Vector中;进口javax.swing.JFrame中;
进口javax.swing.JScrollPane中;
进口javax.swing.JTable中;
进口javax.swing.SwingUtilities中;
进口javax.swing.UIManager中;
进口javax.swing.UnsupportedLookAndFeelException;
进口javax.swing.table.DefaultTableCellRenderer中;
进口javax.swing.table.DefaultTableModel中;公共类TestTable2 {    类MyCellRenderer扩展DefaultTableCellRenderer {
        @覆盖
        公共组件getTableCellRendererComponent(JTable的表,对象的值,布尔isSelected,布尔hasFocus,诠释行,诠释列){
            组件tableCellRendererComponent = super.getTableCellRendererComponent(表,价值,isSelected,hasFocus,行,列);
            如果(价值的instanceof字符串){
                串串=(字符串)值;
                如果(string.indexOf('[')-1个){
                    的setText(getHTML(串));
                }
            }
            返回tableCellRendererComponent;
        }        私人字符串getHTML(字符串字符串){
            StringBuilder的SB =新的StringBuilder();
            sb.append(< HTML和GT;);
            INT索引= 0;
            而(指数< string.length减()){
                接下来INT = string.indexOf('[',索引);
                如果(下一-1个){
                    INT结束= string.indexOf(']',旁边);
                    如果(完-1个){
                        接下来++;
                        sb.append(string.substring(索引,旁边));
                        sb.append(<跨度风格= \\颜色:红色; \\>中);
                        sb.append(string.substring(下一页尾));
                        sb.append(&下; /跨度>中);
                        指数=结束;
                    }其他{
                        打破;
                    }
                }其他{
                    打破;
                }
            }
            sb.append(string.substring(指数,string.length减()));
            sb.append(< / HTML>中);
            返回sb.toString();
        }
    }    保护无效initUI(){
        的DefaultTableModel模型=新的DefaultTableModel();
        对(INT I = 0; I&2;我++){
            model.addColumn(同事+第(i + 1));
        }
        的for(int i = 0; I< 200;我++){
            矢量<对象>行=新的矢量<对象>();
            为(中间体J = 0; J&小于5; J ++){
                row.add(423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]);
            }
            model.addRow(行);
        }
        JTable的表=新的JTable(模型);
        table.setDefaultRenderer(Object.class,新MyCellRenderer());
        JFrame的帧=新的JFrame(TestTable2.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane的滚动窗格=新JScrollPane的(表);
        frame.add(滚动窗格,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(真);
    }    公共静态无效的主要(字串[] args)抛出了ClassNotFoundException,InstantiationException,IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(Runnable的新(){            @覆盖
            公共无效的run(){
                新TestTable2()initUI()。
            }
        });
    }}

和结果:

I have a JTable where one column displays values in the following format:

423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]

I am wondering if it is possible to display the values within square brackets in RED?

I have been googling around for the last few days and have found several examples showing how to set the 'background' of a cell but not really how to change the font of a cell especially not a specific part of the text.

public class myTableCellRenderer
       extends DefaultTableCellRenderer {
  public Component getTableCellRendererComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 boolean hasFocus,
                                                 int row,
                                                 int column) {
    Component c = 
      super.getTableCellRendererComponent(table, value,
                                          isSelected, hasFocus,
                                          row, column);

    if (column == 3) {
       c.setForeground(Color.YELLOW);
       c.setBackground(Color.RED);
    }
    return c;
  }

Is it really possible to change part of the text to be a different color (i.e. the text that is within the square brackets).

Edit

The text i showed as an example is the actual text shown in the table cell (the comma separators are not representing columns). The text displayed in the cell is a comma separated string that i display on the table in column 3.

As an example the table could look like this

product_id |product_name| invoice_numbers
12         |    Books   | 423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]
323        |    Videos  | 423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]
4434       |    Music   | 423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]

解决方案

You must use a Cell renderer combined with HTML.

Here is a small demo example:

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class TestTable2 {

    class MyCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component tableCellRendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (value instanceof String) {
                String string = (String) value;
                if (string.indexOf('[') > -1) {
                    setText(getHTML(string));
                }
            }
            return tableCellRendererComponent;
        }

        private String getHTML(String string) {
            StringBuilder sb = new StringBuilder();
            sb.append("<html>");
            int index = 0;
            while (index < string.length()) {
                int next = string.indexOf('[', index);
                if (next > -1) {
                    int end = string.indexOf(']', next);
                    if (end > -1) {
                        next++;
                        sb.append(string.substring(index, next));
                        sb.append("<span style=\"color: red;\">");
                        sb.append(string.substring(next, end));
                        sb.append("</span>");
                        index = end;
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
            sb.append(string.substring(index, string.length()));
            sb.append("</html>");
            return sb.toString();
        }
    }

    protected void initUI() {
        DefaultTableModel model = new DefaultTableModel();
        for (int i = 0; i < 2; i++) {
            model.addColumn("Col-" + (i + 1));
        }
        for (int i = 0; i < 200; i++) {
            Vector<Object> row = new Vector<Object>();
            for (int j = 0; j < 5; j++) {
                row.add("423545(50),[7568787(50)],53654656,2021947(50),[021947],2021947(50),[8021947(50)]");
            }
            model.addRow(row);
        }
        JTable table = new JTable(model);
        table.setDefaultRenderer(Object.class, new MyCellRenderer());
        JFrame frame = new JFrame(TestTable2.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollpane = new JScrollPane(table);
        frame.add(scrollpane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable2().initUI();
            }
        });
    }

}

And the result:

这篇关于摇摆 - 是否有可能建立一个JTable细胞内的“特定”文本的字体颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆