简体   繁体   中英

Java JTable - Transparence with Nimbus/System L&F

I have this JTable:

    List<MyTuple> l= new ArrayList<>();
    l.add(new Element(1,"One"));
    l.add(new Element(2,"Two"));
    l.add(new Element(3,"Three"));
    l.add(new Element(4,"Four"));
    l.add(new Element(5,"Five"));
    l.add(new Element(6,"Six"));
    l.add(new Element(7,"Seven"));

    JScrollPane pane = new JScrollPane();
    pane.setOpaque(false);
    pane.setBorder(new EmptyBorder(0, 0, 0, 0));
    pane.setBackground(new Color(0,0,0,0));
    pane.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
    pane.setBounds(3, 101, 707, 297);

    MyTableModel tm=new MyTableModel(l);
    table = new MyTable(tm);
    table.setBorder(new EmptyBorder(0, 0, 0, 0));
    table.setOpaque(false);
    //table.getTableHeader().setBackground(new Color(0,0,0,0));
    table.getTableHeader().setOpaque(true);
    table.getTableHeader().setBorder(new EmptyBorder(0,0,0,0));
    table.setBackground(new Color(0,0,0,0));
    table.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
    table.setFillsViewportHeight(false);
    table.getTableHeader().setFont(new Font("Segoe UI Semibold", Font.PLAIN, 13));
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    pane.setViewportView(table);
    pane.getViewport().setOpaque(false);
    table.setGridColor(Color.GRAY);
    table.setShowGrid(true);
    getContentPane().add(pane);

MyTuple, MyTable and MyTableModel classes don't set any custom rendering.

This is how my table looks like under System L&F (which I really like):

系统外观和感觉

This is how my table looks like under Nimbus L&F (note the added big rectangle):

Nimbus 外观和感觉

I want my table to look like the one with System L&F. The problem is that if I set one L&F like I want, the other gets ugly, I want a sort of "compatibility", is there a way to do so?

Maybe using the UIManager.put() method?

You forgot to set the border around the viewport of the scrollpane . Possible solutions:

  • pane.setViewportBorder(null);
  • pane.setViewportBorder(BorderFactory.createEmptyBorder());
  • pane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));

Note that pane.getViewport().setBorder(...) will not work, because a JViewport doesn't actually have a border, setViewportBorder just draws a border around the viewport. ( Reference )

To make the table header transparent:

pane.setColumnHeader(new JViewport());
pane.getColumnHeader().setOpaque(false);

table.getTableHeader().setOpaque(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM