如何渲染复选框?

发布于 2024-11-02 14:13:18 字数 2770 浏览 2 评论 0原文

我在 jTable 中使用 Glazed 列表,其中有一列是布尔值。 默认情况下,jTable 呈现一个复选框,其中列类型为 Boolean.Class。 使用 Glazed 列表我无法获得所需的复选框。我渲染了扩展 DefaultTableCellRenderer 的复选框,但我对此不满意,因为这些复选框不可“单击”。

在 GL 常见问题解答中 http://www.glazedlists.com/documentation/faq 我发现:

*问:如何指定使用复选框呈现布尔表格单元格? 由于某种原因,Glazed Lists 没有 getColumnClass() 方法。

A:如果需要指定列类,则需要实现AdvancedTableFormat接口,而不是常规的TableFormat类。 如果您使用的是 GlazedLists.tableFormat() 工厂方法,则必须指定基本对象的类才能正确实现 AdvancedTableFormat.getColumnClass() 方法。*

你们中的一些人是否有这方面的经验?我没有找到任何与例子。常见问题解答中的重要链接已损坏。

我尝试过这个:

 public class CheckBoxTableModel implements  AdvancedTableFormat {

     public Class getColumnClass(int column) {
         if(column==4)
        return Boolean.class;
         else
             return Object.class;
    }
}

请帮忙!

编辑: 我尝试过这个,带有 jtable 的表单没有显示

   private class TicketTableFormat implements AdvancedTableFormat<Ticket>  {

        private final String[] cols = new String[]{"Id", "From", "Subject", "Date", "Incomplete"};

        public int getColumnCount() {
            return cols.length;
        }

        public String getColumnName(int colId) {
            return cols[colId];
        }

        public Class getColumnClass(int col) {
            if (col == 4) {
                return Boolean.class;
            } else {
                return Object.class;
            }
        }


        public Object getColumnValue(Ticket ticket, int colId) {

            switch (colId) {
                case 0:
                    return ticket.getId();
                case 1:
                    return ticket.getFrom();
                case 2:
                    return ticket.getSubject();
                case 3:
                    return ticket.getDate();
                case 4:
                    return ticket.getIncomplete();
            }
            return null;
        }

        public boolean isEditable(Ticket e, int col) {
            if (col < 4) {
                return false;
            } else {
                return true;
            }
        }

        public Ticket setColumnValue(Ticket e, Object o, int i) {
            e.setB((Boolean) editedValue);
            return e;           
           }

        public Comparator getColumnComparator(int i) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }

我用这个来称呼它:

 TicketTableFormat tbFormat = new TicketTableFormat();
 TicketsModel = new EventTableModel(textFilteredTickets, tbFormat);
 ticketTable.setModel(TicketsModel);

I'm using Glazed lists, in a jTable where I have a column which is boolean.
By default the jTable renders a checkbox where the column is type Boolean.Class.
Using Glazed lists i cannot get the checkbox which is needed. I rendered checkboxs extending the DefaultTableCellRenderer but i'm not satisfied with it because the checkboxs are not "clickable".

In the GL faq http://www.glazedlists.com/documentation/faq i found:

*Q: How do I specify that my boolean table cells be rendered with a checkbox?
For some reason, Glazed Lists has no getColumnClass() method.

A: If you need to specify the column class, you need to implement the AdvancedTableFormat interface instead of the regular TableFormat class.
If you are using the GlazedLists.tableFormat() factory method, you must specify the base Object's class for a proper implementation of the AdvancedTableFormat.getColumnClass() method.*

Does some of you have experience with this I did not find any links with an example. The essential link in the faq is broken.

I tried with this:

 public class CheckBoxTableModel implements  AdvancedTableFormat {

     public Class getColumnClass(int column) {
         if(column==4)
        return Boolean.class;
         else
             return Object.class;
    }
}

Please help!

EDIT:
I tried with this, the form with the jtable does not show

   private class TicketTableFormat implements AdvancedTableFormat<Ticket>  {

        private final String[] cols = new String[]{"Id", "From", "Subject", "Date", "Incomplete"};

        public int getColumnCount() {
            return cols.length;
        }

        public String getColumnName(int colId) {
            return cols[colId];
        }

        public Class getColumnClass(int col) {
            if (col == 4) {
                return Boolean.class;
            } else {
                return Object.class;
            }
        }


        public Object getColumnValue(Ticket ticket, int colId) {

            switch (colId) {
                case 0:
                    return ticket.getId();
                case 1:
                    return ticket.getFrom();
                case 2:
                    return ticket.getSubject();
                case 3:
                    return ticket.getDate();
                case 4:
                    return ticket.getIncomplete();
            }
            return null;
        }

        public boolean isEditable(Ticket e, int col) {
            if (col < 4) {
                return false;
            } else {
                return true;
            }
        }

        public Ticket setColumnValue(Ticket e, Object o, int i) {
            e.setB((Boolean) editedValue);
            return e;           
           }

        public Comparator getColumnComparator(int i) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }

i call it with this:

 TicketTableFormat tbFormat = new TicketTableFormat();
 TicketsModel = new EventTableModel(textFilteredTickets, tbFormat);
 ticketTable.setModel(TicketsModel);

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

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

发布评论

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

评论(2

·深蓝 2024-11-09 14:13:18

这里的基本点是渲染表格单元格和编辑它们是两件不同的事情。对于表格单元格渲染,渲染器仅充当“标记”,即复选框被初始化为应渲染的状态,然后表格仅获取渲染器的图片(通过调用其绘制方法)并放置该图片在适当的单元格中 - 为了渲染所有单元格,会一遍又一遍地重新配置同一复选框实例以表示相应的单元格值。您在代码中所做的就是指定模型的数据类型风格,以便表选择的默认表格单元格渲染器是复选框渲染器。

为了使复选框可编辑,必须将单元格编辑器附加到表格单元格(例如,通过为特定列配置编辑器)。如果表格单元格接收输入焦点,表格单元格编辑器组件将被初始化并放置在表格单元格内(即在渲染器之前绘制的图像上方)。该单元格编辑器组件(在您的情况下是一个复选框)保留在那里(通过鼠标或键盘接受用户输入),直到用户导航离开该特定表调用。在编辑结束时,单元格编辑器会被要求提供从编辑过程中捕获的值(即在编辑器上调用 getCellEditorValue()),并且表格会获取该值写回该单元格的表模型。

简而言之:渲染器只是在单元格上标记数据,即输出,需要编辑器组件来接收输入。

The basic point here is that rendering table cells and eiting them are two different things. For table cell rendering, the renderer only serves as a 'stamp', i.e. the check box is initialized to the state that should be rendered and aterwards the Table only takes apicture of the renderer (by invoking its paint method) and places the tat picture in the appropriate cell - for rendering all the cells, the same instance of checkbox is re-configured over and over again to represent the according cell value. What you did in your code, is specifying the data type flavor of the model, so that the default table cell renderer chosen by the table is a checkbox renderer.

For the checkbox to be editable, a cell editor must be attached to the table cell (e.g. by configuring an editor for the particular column). In case a table cell receives input focus, the table cell editor component is initialized and placed inside the table cell (i.e. above the image that was painted by the renderer before). This cell editor component (would be a checkbox in your case) is remainig there (accepting user input via mouse or keyboard) until the user navigates away from that particzular table call. In the moment when editing ends, the cell editor is asked for the vale it has captured from the editing process, (i.e. getCellEditorValue() is called on the editor) and hte value is taken by the table and written back to the table model for that cell.

So in short: The renderer is only stamping data on the cell, i.e. outputting, an editor component is necessary to receive input.

虫児飞 2024-11-09 14:13:18

你实现了 isEditable(...) & 吗? setColumnValue(...) 在您的 AdvancedTableFormat 实现中?

实施以下方法对我有用。

public int getColumnCount()
public String getColumnName(int column)
public Object getColumnValue(E model, int column)
public boolean isEditable(E model, int column) // For making the checkbox editable
public IFdsModel setColumnValue(E model, Object value, int column)
public Class getColumnClass(int column) // For making it a Checkbox
public Comparator<E> getColumnComparator(int arg0) // for sorting (if you have a SortedList)

编辑:一个例子(快速而肮脏)

import java.util.Comparator;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.gui.AdvancedTableFormat;
import ca.odell.glazedlists.gui.WritableTableFormat;
import ca.odell.glazedlists.swing.EventTableModel;

public class Tester {

private static class MyBoolean {
    private Boolean b = Boolean.FALSE;

    public Boolean getB() {
        return b;
    }

    public void setB(Boolean b) {
        this.b = b;
    }

}

private static class BooleanTableFormat implements AdvancedTableFormat<MyBoolean>, WritableTableFormat<MyBoolean> {

    @Override
    public int getColumnCount() {
        return 1;
    }

    @Override
    public String getColumnName(int column) {
        return "Bool";
    }

    @Override
    public Object getColumnValue(MyBoolean baseObject, int column) {
        return baseObject.getB();
    }

    @Override
    public Class getColumnClass(int column) {
        return Boolean.class;
    }

    @Override
    public Comparator getColumnComparator(int column) {
        throw new IllegalStateException("Not yet implemented.");
    }

    @Override
    public boolean isEditable(MyBoolean baseObject, int column) {
        return true;
    }

    @Override
    public MyBoolean setColumnValue(MyBoolean baseObject, Object editedValue, int column) {
        baseObject.setB((Boolean) editedValue);
        return baseObject;
    }
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            EventList<MyBoolean> list = new BasicEventList<MyBoolean>();
            list.add(new MyBoolean());
            list.add(new MyBoolean());
            EventTableModel<MyBoolean> etm = new EventTableModel<MyBoolean>(list, new BooleanTableFormat());
            JTable table = new JTable(etm);
            JFrame f = new JFrame("Tester");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(300, 200);
            f.getContentPane().add(new JScrollPane(table));
            f.setVisible(true);
        }
    });

}
}

Did you implement isEditable(...) & setColumnValue(...) in your implementation of AdvancedTableFormat?

Implementing the following methods works for me.

public int getColumnCount()
public String getColumnName(int column)
public Object getColumnValue(E model, int column)
public boolean isEditable(E model, int column) // For making the checkbox editable
public IFdsModel setColumnValue(E model, Object value, int column)
public Class getColumnClass(int column) // For making it a Checkbox
public Comparator<E> getColumnComparator(int arg0) // for sorting (if you have a SortedList)

Edit: an example (quick and dirty)

import java.util.Comparator;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.gui.AdvancedTableFormat;
import ca.odell.glazedlists.gui.WritableTableFormat;
import ca.odell.glazedlists.swing.EventTableModel;

public class Tester {

private static class MyBoolean {
    private Boolean b = Boolean.FALSE;

    public Boolean getB() {
        return b;
    }

    public void setB(Boolean b) {
        this.b = b;
    }

}

private static class BooleanTableFormat implements AdvancedTableFormat<MyBoolean>, WritableTableFormat<MyBoolean> {

    @Override
    public int getColumnCount() {
        return 1;
    }

    @Override
    public String getColumnName(int column) {
        return "Bool";
    }

    @Override
    public Object getColumnValue(MyBoolean baseObject, int column) {
        return baseObject.getB();
    }

    @Override
    public Class getColumnClass(int column) {
        return Boolean.class;
    }

    @Override
    public Comparator getColumnComparator(int column) {
        throw new IllegalStateException("Not yet implemented.");
    }

    @Override
    public boolean isEditable(MyBoolean baseObject, int column) {
        return true;
    }

    @Override
    public MyBoolean setColumnValue(MyBoolean baseObject, Object editedValue, int column) {
        baseObject.setB((Boolean) editedValue);
        return baseObject;
    }
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            EventList<MyBoolean> list = new BasicEventList<MyBoolean>();
            list.add(new MyBoolean());
            list.add(new MyBoolean());
            EventTableModel<MyBoolean> etm = new EventTableModel<MyBoolean>(list, new BooleanTableFormat());
            JTable table = new JTable(etm);
            JFrame f = new JFrame("Tester");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(300, 200);
            f.getContentPane().add(new JScrollPane(table));
            f.setVisible(true);
        }
    });

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