JTable AutoCreateRowSorter导致Swing错误地绘制表

时间:2017-03-24 22:44:42

标签: java swing jtable

我在java中创建了一个JTable,并使用了AutoCreateRowSorter方法来帮助对表的列进行排序。

问题是,当我单击列标题对表进行排序时,它重新绘制了表错误。显示的第一张图片是打开时的外观。第二张图显示了单击一次后会发生什么。请注意,数据与列标题重叠。

Initial JTable

Repainted JTable

public class DataTable {
    String[] colNames = {"Aircraft",
             "Track ID",
             "Runway",
             "Operation Type",
             "Number Daily of Operations"};

    public DataTable(){}

    public static void main(String[] args) {
        DataTable tble = new DataTable();
        JFrame window = new JFrame();
        window.setBounds(0, 10, 1000,700);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setLayout(new BorderLayout(0, 0));
        window.setContentPane(tble.getfinalPane());
        window.setVisible(true);
    }

    public JPanel getfinalPane(){
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        GridBagConstraints b = new GridBagConstraints();
        b.gridx = 0;
        b.gridy = 0;
        b.fill = GridBagConstraints.BOTH;
        b.anchor = GridBagConstraints.FIRST_LINE_END;
        b.weighty = 500;
        panel.add(getTable(),b);

        return panel;
    }

    public JScrollPane getTable(){
        Object[][] data = {{"Boeing 717",(long) 2459,"01L","Arrival",0.13},{"Boeing 727",(long) 2439,"01R","Arrival",0.12}};
        JTable table = new JTable(data,colNames);
        table.setFont(new Font("Serif",Font.BOLD,20));
        table.getTableHeader().setFont(new Font("Serif",Font.BOLD,20));
        table.getTableHeader().setBackground(new Color(0,0,0,100));
        table.getTableHeader().setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLACK));
        table.getTableHeader().setForeground(Color.WHITE);
        table.setRowHeight(table.getRowHeight()+table.getFont().getSize());
        table.setBackground(new Color(214, 217, 223));
        table.setAutoCreateRowSorter(true);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

        for(int i = 0; i < colNames.length; i++){
            table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
        }

        JScrollPane scroll = new JScrollPane(table);
        scroll.setBackground(new Color(0,0,0,100));
        table.setFillsViewportHeight(true);

        return scroll;
    }

}

我想知道如何解决这个问题。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

Swing不知道如何处理基于alpha的颜色作为背景颜色,它设计为仅使用完全透明或完全不透明的组件。

你可以通过扩展组件,使它们透明然后自己执行填充操作来伪造它

半透明$options['ssl']['verify_peer'] = FALSE;

$options['ssl']['verify_peer_name'] = FALSE;

Translucet JScrollPane

public class TranslucentScrollPane extends JScrollPane {

    public TranslucentScrollPane() {
        setOpaque(false);
        getViewport().setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(getBackground());
        g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
        super.paintComponent(g2d);
        g2d.dispose();
    }

}

半透明JTableHeader

public class TranslucentTableHeader extends JTableHeader {

    public TranslucentTableHeader() {
        setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(getBackground());
        g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
        super.paintComponent(g2d);
        g2d.dispose();
    }


}

完全放弃......

Example

JTable