Why is my table not visible when it's in a JScrollPane?

前端 未结 2 761
名媛妹妹
名媛妹妹 2020-12-21 09:09

I\'ve been trying for hours, different things and searching everywhere for a solution, but I can not get my table in addScoreCardUpper() to show up. I get an ho

相关标签:
2条回答
  • 2020-12-21 09:32

    Try any one

    scrollPane = new JScrollPane(table);
    

    or

    scrollPane = new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    

    or

    scrollPane.getViewport().add(table);
    
    0 讨论(0)
  • 2020-12-21 09:48

    This is your problem:

    scrollPane.add(table);
    

    This does not add the JTable to the JScrollPane's viewport's view which is where you want it, but rather completely replaces the JScrollPane's viewport with the JTable, making the JScrollPane completely nonfunctional. Instead set the table as the JScrollPane's viewportview:

    scrollPane.setViewportView(table);
    

    Do either this or pass the table into the JScrollPane's constructor which does pretty much the same thing:

    JScrollPane scrollPane = new JScrollPane(table,
              ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    

    This is all well explained in the JScrollPane API, and you will want to give it a look for the important details.


    Edit
    Your code also calls setVisible(true) on the JFrame before adding all components which can lead to trouble. You'll want to avoid doing this.

    0 讨论(0)
提交回复
热议问题