2

Given

import javax.swing.*;

public class TestCornerComponent {

    public static void main(String args[]) {
        JTable table = new JTable();
        final JScrollPane scrollPane = new JScrollPane(table);

        /* button to put in corner */
        JButton cornerButton = new JButton("#");
        scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER,
            cornerButton);

        scrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame("Test corner component");
                frame.getContentPane().add(scrollPane);
                frame.setVisible(true);
            }
        });
    }
}

how might one make the cornerButton always visible, irrespective of whether the JTable is empty or not?

I see that the JXTable in SwingX accomplishes this however, I wasn't able to decipher from the source code how it was done.

Thanks

mKorbel
  • 108,320
  • 17
  • 126
  • 296
hiraethus
  • 43
  • 1
  • 6
  • AFAIK this is possible only in the case that JScrollBar is visible – mKorbel Jun 13 '13 at 13:51
  • Thanks for the suggestion, however, that is why I added `scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);`. My issue is that the JScrollPane only shows the button when there is something in the ScrollPane's column header viewport. I've tried putting an empty JPanel in there but that doesn't seem to work. – hiraethus Jun 13 '13 at 13:54
  • agreed probably doesn't works without JTableHeader, but is there reason to create a JTable without JTableHeader (empty JTable as is demonstrated in your SSCCE +1), if yes then not easy, never tried – mKorbel Jun 13 '13 at 14:04
  • It's for a custom Pivot Table similar to one in Excel such that if there are no columns chosen to pivot, the table is empty. I'll keep looking. Thanks again for your help. – hiraethus Jun 13 '13 at 14:18

1 Answers1

4

My issue is that the JScrollPane only shows the button when there is something in the ScrollPane's column header viewport.

I think that is half the problem. You need a component in the viewport to take up some space.

I've tried putting an empty JPanel in there but that doesn't seem to work

Try the empty JPanel in the viewport along with the panel in the column header:

import java.awt.*;
import javax.swing.*;

public class TestCornerComponent {

    public static void main(String args[])
    {
        final JScrollPane scrollPane = new JScrollPane();

        /* button to put in corner */
        JButton cornerButton = new JButton("#");
        scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER, cornerButton);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JPanel column = new JPanel();
        column.setPreferredSize( new Dimension(100, cornerButton.getPreferredSize().height) );
        scrollPane.setColumnHeaderView( column );

        JPanel view = new JPanel();
        view.setPreferredSize( new Dimension(100, 100) );
        scrollPane.setViewportView( view );

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame("Test corner component");
                frame.add(scrollPane);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
camickr
  • 308,339
  • 18
  • 152
  • 272