0

I have both texts and icons for my buttons. I want to hide texts, if user resize the frame and make one the buttons invisible. I found a workaround with scroll pane, but I want to know if there is a better solution. Thanks in advance.

My solution:

public class IconButtonTest extends JFrame {

    private JScrollPane buttonScrollPane;

    private JButton button1;
    private JButton button2;
    private JButton button3;

    public IconButtonTest() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        ImageIcon icon = new ImageIcon();

        button1 = new JButton("Button1", icon);
        button2 = new JButton("Button2", icon);
        button3 = new JButton("Button2", icon);

        buttonPanel.add(button1);
        buttonPanel.add(button2);
        buttonPanel.add(button3);

        buttonScrollPane = new JScrollPane(buttonPanel);
        buttonScrollPane.setBorder(null);
        add(buttonScrollPane, BorderLayout.NORTH);

        addComponentListener(new ResizeListener());

        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String[] args) {
        new IconButtonTest();
    }

    private class ResizeListener extends ComponentAdapter {
        @Override
        public void componentResized(ComponentEvent e) {
            button1.setText("Button1");
            button2.setText("Button2");
            button3.setText("Button2");

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if (buttonScrollPane.getHorizontalScrollBar().isVisible()) {
                        button1.setText("");
                        button2.setText("");
                        button3.setText("");
                    }
                }
            });

        }
    }
}

I added buttons to JPanel and add that panel to the frame with a scroll pane. In ResizeListener I am checking if horizontal scroll visible or not. If it is visible it means buttonPanel is overflowed the visible area.

rdonuk
  • 3,921
  • 21
  • 39
  • 1
    Also consider `JToolBar`, which can dock on most L&Fs, or a [size variant](http://stackoverflow.com/a/14599176/230513). – trashgod Apr 13 '16 at 16:40

1 Answers1

1

I found a workaround with scroll pane, but I want to know if there is a better solution.

Don't know if it is better but there is no need for the scroll pane. Just add the listener to the panel, then you can check the preferred width against the actual width:

button1.setText("Button1");
button2.setText("Button2");
button3.setText("Button2");

JPanel panel = (JPanel)e.getComponent();

if (panel.getSize().width < panel.getPreferredSize().width)
{
    button1.setText("");
    button2.setText("");
    button3.setText("");
}

I want to hide texts, if user resize the frame and make one the buttons invisible.

If you are just trying to make sure all the buttons are accessible then you might be able to:

  1. Use the Wrap Layout. The button will wrap to a new line and the height of the panel will be increased.

  2. Use the concept of the ScrollContainer. Buttons will be added to the start/end of the container as required to allow you to scroll through buttons that are not currently visible. See: Moving left right in a JPanel

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • It is definitely better. Because in my solution there was a flashing effect sometimes while resizing (scroll pane was becoming visible). – rdonuk Apr 13 '16 at 20:12
  • And thanks for the `ScrollContainer` it will be helpful, I think. – rdonuk Apr 13 '16 at 20:13