楼主: futo9239

[精华] JTable 与 Vector

[复制链接]
论坛徽章:
0
21#
 楼主| 发表于 2005-12-21 21:50 | 只看该作者
我变性了人家也不要我的拉,
这个帖什么时候变精华了,庆贺一下我把头像换了,漂亮点了把

现在谈技术,我的的显示图片已经实现了,并没用到TableCellRenderer

lang_m 是斑竹吗?表扬一下,呵呵

使用道具 举报

回复
论坛徽章:
41
Heart of PUB
日期:2006-09-26 17:30:03马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
22#
发表于 2005-12-22 07:02 | 只看该作者
不用TableCellRenderer?除非JTable对你的类有默认的Renderer

那你Column中的类是什么类?(呵呵,在程序上的处理有点避重就轻阿)

使用道具 举报

回复
论坛徽章:
0
23#
 楼主| 发表于 2005-12-22 15:44 | 只看该作者
Vector rowData = new Vector();
                for( int i=0; i < pics.size(); i++ )
                {
                        Vector temp = new Vector();
                        temp.add( new ImageIcon( getClass().getResource( "../../../pic/smallhead/" +  pics.get( i ) + "-1.gif" ) ) );
                                                                                           temp.add( nickname.get( i )  );
                        rowData.add( temp );
                }
                String names[] = { "头像",  "昵称" };
                Vector columnNames = new Vector( Arrays.asList( names ) );
               
                MyTableModel model = new MyTableModel( rowData, columnNames );
                table = new JTable( model );


class MyTableModel extends AbstractTableModel
{
        Vector rowData, columnNames;
   
    public MyTableModel( Vector rowData, Vector columnNames )   
        {
                this.rowData = rowData;
                this.columnNames = columnNames;
        }
               
        public int getColumnCount()
        {
        return columnNames.size();
    }

        public int getRowCount()
    {
        return rowData.size();
    }

    public String getColumnName(int col)
    {
        return columnNames.get( col ).toString();
    }

    public Object getValueAt(int row, int col)
    {
        return ( ( Vector )rowData.elementAt( row ) ).elementAt( col );
    }

    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }
   
    public boolean isCellEditable(int row, int col)
    {
                return false;
    }  
}   
大致大代码就这些,本来不用图片的话就直接table = new JTable( rowData, columnNames  );了

使用道具 举报

回复
论坛徽章:
41
Heart of PUB
日期:2006-09-26 17:30:03马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
24#
发表于 2005-12-22 16:10 | 只看该作者
?奇怪 ?
看下面这段话

The javax.swing.table package includes a default renderer that produces a JLabel to display
text for each cell in the table. The JTable class uses this renderer to display Numbers, Icons, and
Objects. JTable creates a new default renderer and then aligns it correctly and attaches an
appropriate icon, depending on the type of data. Object objects are shown as regular labels, Number
objects are shown right-aligned, and Icons are shown using centered labels. Boolean values do not
use DefaultTableCellRenderer; instead, they use a private renderer class that extends
JCheckBox.

按理说,你的ICON应该可以显示,并不用扩展这个table model阿。
你getColumnClass所作的事情和正常的没有区别阿,这点我很奇怪,除非DefaultTableModel在这里的实现和你不一样。


呵呵,找到原因了,因为最基础的AbstractTableModel在这里的实现和你不一样,他的getColumnClass不管你列中的类是什么都返回Object.class,而DefaultTableModel并没有覆盖这个方法,JTable中的DefaultTableCellRenderer对Object类的显示处理就是取Object.toString,然后将内容显示,呵呵,所以你的ImageIcon会显示路径。

哈哈,搞清楚了,你这么做是对的

使用道具 举报

回复
论坛徽章:
0
25#
 楼主| 发表于 2005-12-23 23:07 | 只看该作者
研究的好深啊,你解释给我听,我是可以理解,如果让我自己想,估计希望不大,我的道路好坎坷啊,还有好象学编程的外语要很好呀,看这段话,我查了好多单词的

使用道具 举报

回复
论坛徽章:
0
26#
 楼主| 发表于 2005-12-23 23:30 | 只看该作者
问一下,我想在JComboBox里显示一组不同颜色的JLabel,
Color colors[] = { Color.black, Color.gray, Color.blue, Color.pink };
JLabel colorLabel[] = new JLabel[ colors.length ];
               
for (int i = 0; i < colors.length; i++)
        {
            colorLabel = new JLabel();
            colorLabel.setBackground( colors[ i ] );
        }
JComboBox colorComboBox = new JComboBox( colorLabel );
ComboBoxRenderer renderer= new ComboBoxRenderer();
colorComboBox.setRenderer( renderer );
然后
class ComboBoxRenderer extends JLabel implements ListCellRenderer
{
    public ComboBoxRenderer()
    {
        setOpaque(true);
        setHorizontalAlignment(CENTER);
        setVerticalAlignment(CENTER);
    }

    public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        if (isSelected)
        {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        }
        else
        {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }

        JLabel label = (JLabel)value;
        return this;
    }
}
显示出来的没有颜色的一组JLabel,还应该怎么处理一下呢

使用道具 举报

回复
论坛徽章:
41
Heart of PUB
日期:2006-09-26 17:30:03马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
27#
发表于 2005-12-24 07:55 | 只看该作者
这样修改一下
Color colors[] = { Color.black, Color.gray, Color.blue, Color.pink };

JComboBox colorComboBox = new JComboBox( colors);
ComboBoxRenderer renderer= new ComboBoxRenderer();
colorComboBox.setRenderer( renderer );

class ComboBoxRenderer extends JLabel implements ListCellRenderer
{
public ComboBoxRenderer()
{
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}

public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
if ( value instanceof Color )
{
setBackground(value);
}

return this;
}
}

注:这里没有考虑选中时的显示区别,你可以自己该一下(你先运行一下看看效果)

使用道具 举报

回复
论坛徽章:
127
Heart of PUB
日期:2008-01-02 14:43:06问答徽章
日期:2013-10-16 18:19:34Jeep
日期:2014-02-17 05:11:352014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14阿斯顿马丁
日期:2013-09-08 00:03:55
28#
发表于 2005-12-24 17:18 | 只看该作者
最初由 lang_m 发布
[B]

如果能把任何一本基础书吃透,你在一年内可以在80%的程序员之上了 [/B]


那是依此逻辑要是把,在两年内就可以在120%的程序员之上了,呵呵
问题是你啃的时候别的程序员也在啃啊,而且现在的趋势好象是啃的人越来越多了

使用道具 举报

回复
论坛徽章:
41
Heart of PUB
日期:2006-09-26 17:30:03马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
29#
发表于 2005-12-24 17:23 | 只看该作者
最初由 dearmeiw 发布
[B]

那是依此逻辑要是把,在两年内就可以在120%的程序员之上了,呵呵
问题是你啃的时候别的程序员也在啃啊,而且现在的趋势好象是啃的人越来越多了 [/B]


嘿,我没说是匀速的阿,一年80%,两年有可能还不到85%阿,呵呵,这东西不就是月来越慢么

使用道具 举报

回复
论坛徽章:
0
30#
 楼主| 发表于 2005-12-24 23:51 | 只看该作者
效果可以接受,然后就是选好了没有在JComboBox里显示,呵呵,和你说的一样
那么如何可以把选好的显示出来呢,为什么要卖个馆子

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表