我给表头设定了自定义的绘制器后,表头的网格线不见了?有什么办法可以显示吗?
表头的绘制器用label.
还就是表头的行高怎么设置?
解决即给分
------解决方案--------------------
setShowGrid(boolean showGrid)
设置表是否绘制单元格周围的网格线。
setRowHeight(int row, int rowHeight)
将 row 的高度设置为 rowHeight,重新验证并重新绘制它。
这两个...
------解决方案--------------------
DefaultTableCellRenderer render = (DefaultTableCellRenderer) tbModules
.getTableHeader().getDefaultRenderer();
Dimension d = render.getSize();
d.height = 25;
render.setPreferredSize(d);
这是表头高的设置
------解决方案--------------------
网格布局
一下是我在做图书管理系统是用的表格,可以参考一下:
final Object[] bb={"编号","条形码","名称","索书号","分类号","一级分类","二级分类","存放位置","出版社","作者","译者","数量"};//列标题
Object[][] rowData=new Object[32][12];
table=new JTable(rowData,bb); //实例化表格
JScrollPane pane = new JScrollPane (table);
JPanel panel = new JPanel (new GridLayout (0, 1));//以矩形网格形式对容器的组件进行布置
panel.setPreferredSize (new Dimension (600,400));//设置panel首选大小
panel.setBackground (Color.black);
panel.add (pane);
panel.setBounds(0,200,880,300);
c.add(panel);
------解决方案--------------------
网格线指的是什么?一条条的竖线吗?列的分隔线?
------解决方案--------------------
http://www.zaoxue.com/article/tech-71429.htm
有个tableHeader,你查下API
------解决方案--------------------
自定义绘制器
能不能把代码帖帖
------解决方案--------------------
你重写了getTableCellRendererComponent方法吧?那么你只需要在这个方法最后加上
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
就可以了.
------解决方案--------------------
第一个问题已经解决了吧?没绘制border
第二个:可以设置prefferdSize
- Java code
import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.UIManager;import javax.swing.WindowConstants;import javax.swing.table.DefaultTableCellRenderer;public class Test extends JFrame { public Test() { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(new JTable(10, 10)); JTable t = new JTable(10, 10); headerRender render = new headerRender(); t.getTableHeader().setDefaultRenderer(render); t.getTableHeader().setPreferredSize(new Dimension(0, 50)); contentPane.add(new JScrollPane(t)); } public static void main(String args[]) { GraphicJavaWindowHandler.launch(new Test(), "Tables and Scrollpanes", 100, 100, 850, 700); }}class GraphicJavaWindowHandler extends WindowAdapter { public static void launch(final JFrame f, String title, final int x, final int y, final int w, int h) { f.setTitle(title); f.setBounds(x, y, w, h); f.setVisible(true); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } }); }}class headerRender extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { label.setText(value.toString()); label.setBorder(UIManager.getBorder("TableHeader.cellBorder")); Dimension d = label.getSize(); d.height = 36; label.setSize(d); return label; } JLabel label = new JLabel();}