/** Create the frame. */ public SearchTable() { addWindowListener( new WindowAdapter() { @Override public void windowActivated(WindowEvent e) { do_this_windowActivated(e); } }); setTitle("\u652F\u6301\u67E5\u627E\u529F\u80FD\u7684\u8868\u683C"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.NORTH); JLabel label = new JLabel("\u5173\u952E\u5B57\uFF1A"); label.setFont(new Font("微软雅黑", Font.PLAIN, 16)); panel.add(label); textField = new JTextField(); textField.setFont(new Font("微软雅黑", Font.PLAIN, 16)); panel.add(textField); textField.setColumns(20); JPanel buttonPanel = new JPanel(); contentPane.add(buttonPanel, BorderLayout.SOUTH); JButton button = new JButton("\u67E5\u627E"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); button.setFont(new Font("微软雅黑", Font.PLAIN, 16)); buttonPanel.add(button); JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); table = new JTable(); table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); table.setRowHeight(30); JTableHeader header = table.getTableHeader(); header.setFont(new Font("微软雅黑", Font.PLAIN, 16)); header.setPreferredSize(new Dimension(header.getWidth(), 35)); scrollPane.setViewportView(table); }
private void initTable() { JTableHeader header = table.getTableHeader(); table.setRowHeight(ROW_HEIGHT); table.setFont(font); header.setFont(font); header.setPreferredSize(new Dimension(header.getWidth(), ROW_HEIGHT)); table.setCellSelectionEnabled(false); table.setOpaque(false); // table.setShowGrid(false); // table.setShowVerticalLines(false); table.setGridColor(gridColor); // 设置表格颜色 setForeground(foreColor); header.setForeground(foreColor); // header.setBackground(headerColor); header.setBorder(new LineBorder(gridColor, 1)); DefaultTableCellRenderer dtr = new DefaultTableCellRenderer() { private static final long serialVersionUID = 1L; @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (row % 2 == 0) { setBackground(backColor1); } else if (row % 2 == 1) { setBackground(backColor2); } else setBackground(backColor2); setForeground(foreColor); return super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); } }; dtr.setHorizontalAlignment(JLabel.CENTER); for (int i = 0; i < headerStr.length; i++) { table.getColumn(headerStr[i]).setCellRenderer(dtr); } header.setDefaultRenderer(dtr); }
public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); Robot robot = new Robot(); robot.setAutoDelay(20); SwingUtilities.invokeAndWait( new Runnable() { public void run() { // just to quickly grab a column model JTable table = new JTable(10, 5); header = new JTableHeader(table.getColumnModel()); checkColumn(0, "A"); JFrame frame = new JFrame("standalone header"); frame.add(header); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); robot.waitForIdle(); Point point = header.getLocationOnScreen(); robot.mouseMove(point.x + 3, point.y + 3); robot.mousePress(InputEvent.BUTTON1_MASK); for (int i = 0; i < header.getWidth() - 3; i++) { robot.mouseMove(point.x + i, point.y + 3); } robot.mouseRelease(InputEvent.BUTTON1_MASK); SwingUtilities.invokeAndWait( new Runnable() { public void run() { TableColumnModel model = header.getColumnModel(); checkColumn(model.getColumnCount() - 1, "A"); } }); }
public void setRowHeight(int h) { this.ROW_HEIGHT = h; table.setRowHeight(ROW_HEIGHT); JTableHeader header = table.getTableHeader(); header.setPreferredSize(new Dimension(header.getWidth(), ROW_HEIGHT)); }