验证并突出显示 JTable 单元格

发布于 2025-01-06 10:41:10 字数 2104 浏览 1 评论 0原文

我根据验证突出显示 JTable 单元格。在某些情况下,我必须采用其他列的值。例如,如果 column2 包含 USA,则 column3 应该只是数字。另一个例子,如果 col2 是“USA”并且 col4 是数字,那么 col5 应该只有三个字符。有人可以建议如何做到这一点吗?

在下面的片段中,col3 包含国家/地区名称; col4col5 依赖于 col3。当我处于 case 3case 4 中时,我无法检查 case 2 的值。例如,我想要 if (col3.value == "USA")

    [code]
    tcol = editorTable.getColumnModel().getColumn(0);
    tcol.setCellRenderer(new CustomTableCellRenderer());

    tcol = editorTable.getColumnModel().getColumn(1);
    tcol.setCellRenderer(new CustomTableCellRenderer());

    tcol = editorTable.getColumnModel().getColumn(2);
    tcol.setCellRenderer(new CustomTableCellRenderer());

    public class CustomTableCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent (JTable table, Object
        value,boolean isSelected, boolean hasFocus, int row, int col){

        Component cell = super.getTableCellRendererComponent(table, value,
        isSelected,hasFocus, row, col);

       if (value instanceof String) {
           String str = (String) value;

           switch (col) {
                case 0:
                    col1(str, cell);
                    break;
                case 1:
                    col2(str, cell);
                    break;
                case 2:
                    col3(str, cell);
                    break; 
           }
        }
        return cell;
     }

      private void col1(String str, Component cell) {       
            if(!str.matches("[0-9a-zA-z]")){
                cell.setBackground(Color.RED);
            } else {
                cell.setBackground(Color.GREEN); 
           }
     }

      private void col2(String str, Component cell) {       
         if(!str.matches("[A-Z]{3}")){
             cell.setBackground(Color.RED);
         } else {
              cell.setBackground(Color.GREEN); 
         }
     }
    [/code]

I am highlighting JTable cells based on validation. In some conditions, I have to take the value of other columns. For example, if column2 has USA then column3 should be only numeric. As another example, if col2 is "USA" and col4 is numeric, then col5 should be only three chars. Can someone suggest how this can be done?

In the fragment below, col3 contains country names; col4 and col5 depend on col3. When I am in case 3 and in case 4, I cannot check the value of case 2. For example, I want like, if (col3.value == "USA").

    [code]
    tcol = editorTable.getColumnModel().getColumn(0);
    tcol.setCellRenderer(new CustomTableCellRenderer());

    tcol = editorTable.getColumnModel().getColumn(1);
    tcol.setCellRenderer(new CustomTableCellRenderer());

    tcol = editorTable.getColumnModel().getColumn(2);
    tcol.setCellRenderer(new CustomTableCellRenderer());

    public class CustomTableCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent (JTable table, Object
        value,boolean isSelected, boolean hasFocus, int row, int col){

        Component cell = super.getTableCellRendererComponent(table, value,
        isSelected,hasFocus, row, col);

       if (value instanceof String) {
           String str = (String) value;

           switch (col) {
                case 0:
                    col1(str, cell);
                    break;
                case 1:
                    col2(str, cell);
                    break;
                case 2:
                    col3(str, cell);
                    break; 
           }
        }
        return cell;
     }

      private void col1(String str, Component cell) {       
            if(!str.matches("[0-9a-zA-z]")){
                cell.setBackground(Color.RED);
            } else {
                cell.setBackground(Color.GREEN); 
           }
     }

      private void col2(String str, Component cell) {       
         if(!str.matches("[A-Z]{3}")){
             cell.setBackground(Color.RED);
         } else {
              cell.setBackground(Color.GREEN); 
         }
     }
    [/code]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2025-01-13 10:41:10

@kleopatra 和@mKorbel 是正确的。您的片段不完整,但看起来好像您正在尝试解决渲染器中的编辑器和模型问题。

您可以验证自定义 TableCellEditor 中输入的值,如本示例所示。您可以处理 TableModel 中的依赖列,如本示例。

在评论中,您说:“如果我没记错的话,prepareRenderer() 需要循环所有行, 正确的?”

不,JTable“内部实现总是使用此方法来准备渲染器,以便子类可以安全地覆盖此默认行为。”当必须有选择地将更改应用于所有渲染器时,覆盖prepareRenderer()非常有用。

请参阅概念:编辑器和渲染器< /a> 了解更多详细信息。

@kleopatra and @mKorbel are correct. Your fragment is incomplete, but it appears as if you are trying to solve editor and model problems in the renderer.

You can validate entered values in a custom TableCellEditor, as shown in this example. You can handle dependent columns in the TableModel, as shown in this example.

In a comment you say, "If I am not wrong, prepareRenderer() needs looping of all the rows, right?"

No, JTable "internal implementations always use this method to prepare renderers so that this default behavior can be safely overridden by a subclass." Overriding prepareRenderer() is most useful when changes must be be selectively applied to all renderers.

See Concepts: Editors and Renderers for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文