1

Similar to this question: Dynamically adding JTable to JScrollPane

I am trying to add a JTable every time that the user clicks a button. Firstly my problem was the same as in that question; nothing would display, once I used the getViewport I could get it to display, except now I can't see any of my buttons.

I should have buttons above and below each JTable. (they are also in the scroll pane. I'm guessing that they are removed because getViewport will get the entire scroll pane (that I can see) and put my JTable over that).

Why is it necessary to getViewport()? I can easily do what I want if I simply change the JTable for a JButton for example.

Also if I move the button to outside the scrollpane then clicking to add another JTable looks like it does nothing. I'm assuming that it just keeps putting another JTable over the previous one.

So to sum up: how I can use buttons within a JScrollPane to add JTables to the same JScrollPane?

Thank you for your help.

Edit: Action that happens when user clicks the button: The button works correctly, being added on each click but no tables ever appear.

public void actionPerformed(ActionEvent e)
    {
        JTableList.add(new JTable(getModel()));
        panelClass.this.panel.add(JTableList.get(JTableList.size()-1));
        panelClass.this.panel.add(new JButton("test"));

        panelClass.this.validate();
        panelClass.this.repaint();
    }
Community
  • 1
  • 1
Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • 1. for better help sooner post an SSCCE/MCVE, short, runnable, compilable, 2. reuse model, replace model, instead of remove/add a new JTable at runtime – mKorbel Jun 10 '15 at 05:29
  • @mKorbel I don't want to replace though, I want to keep adding JTables, (with different models). In the scrollpane I have just two buttons, clicking either of them will add a table with the appropriate model, as well as another two buttons. to add a JTable before or after each JTable. The two buttons are just to get a table model in different ways – Aequitas Jun 10 '15 at 05:32
  • see JTable.setModel() – mKorbel Jun 10 '15 at 05:33
  • @mKorbel I am setting the model, the problem is when I try to add the JTable, if I getViewport, then the JTable will display properly except that it covers all my buttons – Aequitas Jun 10 '15 at 05:38
  • 1. for better help sooner post an SSCCE/MCVE, short, runnable, compilable, – mKorbel Jun 10 '15 at 05:44
  • @mKorbel i added some code to help explain what I mean, it's not compilable but should illustrate what I'm trying to do – Aequitas Jun 10 '15 at 09:34

1 Answers1

0

It seems that a JTable must be in a scroll pane by itself.

So to solve the problem, instead of adding the JTable to the panel, first add it to a new JScrollPane and then add that scroll pane to the panel. Validate and repaint and it will work.

See Code snippet:

public void actionPerformed(ActionEvent e)
{
    JTableList.add(new JTable(getModel()));
    JScrollPane tableContainer = new JScrollPane(JTableList.get(JTableList.size()-1));
    panelClass.this.panel.add(tableContainer);
    panelClass.this.panel.add(new JButton("test"));

    panelClass.this.validate();
    panelClass.this.repaint();
}

Only problem is that the list of tables won't scroll if the mouse is over any of the tables. See here: Why JScrollPane does not react to mouse wheel events? for solving this issue.

Community
  • 1
  • 1
Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • This is not an answer; please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that shows your revised approach. – trashgod Jun 11 '15 at 00:41
  • 1
    @trashgod How isn't it an answer? "First add it to a new JScrollPane and then add that scroll pane to the panel" doing it this way works. Edited to show code snippet of that – Aequitas Jun 11 '15 at 00:55
  • The `JTable` need _not_ be in a scroll pane by itself. You indicate that "it _should_ work," but no one can tell without a [complete example](http://stackoverflow.com/help/mcve), as requested by several correspondents. The `JTable` handles mouse events itself, preempting scrolling until focus shifts. As you have incorporated my contemporaneously down-voted [answer](http://stackoverflow.com/a/30753280/230513) suggesting `validate()`, I'll defer to the moderator on which answer to delete. – trashgod Jun 11 '15 at 01:23
  • 1
    I don't understand, all the code that is necessary is there, I've said that the button adds correctly so the only changes needed will be in that action. I had most of my code before, but edited it out as it is too long and not needed to illustrate the problem. I did seem to have missed one method, but I was already doing (re)validate, changing to validate did not help and using a list also still produced the same issue as I noted in the updated code in the edit. However changing it as seen in this answer, ie. adding the table in a scroll pane before adding it into a super scroll pane works – Aequitas Jun 11 '15 at 01:38
  • 1
    wrong decision ..., by default you would not need that, never need to add JTable by intention to replace another JTable, everything is about model, JScrollPane (JViewport) doesn't required notifier for LayoutManager, – mKorbel Jun 11 '15 at 12:54