请问如何将JTable像JButton一样置灰

llaabb 2005-01-11 11:28:07
JTable在JScrollPane中,请问是否可以将它像对JButton一样置灰?如果可以如何实现?
JScrollPane jp = new JScrollPane();
JTable jt = new JTable();
jp.getViewport().add(jt, null);

我用这个语句
jt.setEnabled(false);

可是滚动条还是可用,同时JTable上的列宽也可以调整。
我想将它置灰,不能点击。
谢谢!
...全文
152 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
BasaraTracy 2005-01-13
  • 打赏
  • 举报
回复
jt.setEnabled(false);的意思是不允许选择表格的操作
JScrollPane 的滚动条置灰不大可能吧,如果这样不如不加滚动条了
llaabb 2005-01-13
  • 打赏
  • 举报
回复
楼上的,请问怎样将JScrollPane的滚动条禁掉,显示区置灰
不徻写代码 2005-01-13
  • 打赏
  • 举报
回复
JScrollPane的滚动条禁掉,显示区置灰
popufig 2005-01-13
  • 打赏
  • 举报
回复
我只知道可以设定JTable的Cell不可编辑,但是不知道还可以置为灰色
llaabb 2005-01-13
  • 打赏
  • 举报
回复
这个问题还没解决呢,请高手帮忙一下了
谢谢
llaabb 2005-01-12
  • 打赏
  • 举报
回复
同时使用了
jp.setEnabled(false);
jt.setEnabled(false);
还是不行啊
gwang119 2005-01-12
  • 打赏
  • 举报
回复
是不是应该 :jp.setEnabled(false);
llaabb 2005-01-12
  • 打赏
  • 举报
回复
好像TableModel没有isCellrender这个方法吧,而且TableModel的代码已经封装,也不能随便改了
dulang200x 2005-01-12
  • 打赏
  • 举报
回复
实现模型里的isCellrender() 方法,返回false ,

6应该就是不可编辑了吧,
1
llaabb 2005-01-12
  • 打赏
  • 举报
回复
请高手帮忙啊,谢谢了
llaabb 2005-01-11
  • 打赏
  • 举报
回复
有没有高手回答一下呀
使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧