Question

I have a panel which contains a Horizontal Box within the Box there is 2 button. Now i extende this class and want to add new button at the begin of box. What i tried addes button to the end of box.

any body know how to do it?

 private class MyBoxPanel extends BoxPanel {
         public JButton btnPrint;
        public MyConfirmationPanel() {
            btnPrint = new JButton("print");
            add(btnPrint);
            add(Box.createRigidArea(new Dimension(5, 0)));
        }

        protected void confirmActionPerformed(ActionEvent e) {
            for (PrinterInputListener listener : listeners)
                listener.printConfirmed(printerPanel.getPrint().getId());
        }

        protected void cancelActionPerformed(ActionEvent e) {
            for (PrinterInputListener listener : listeners)
                listener.printCancelled();
        }
    }
Was it helpful?

Solution

What i tried addes button to the end of box.

Yes, the add(component) method just adds the component to the end of the container.

If you want to add the component to the beginning then you need to specify an index value of 0. Read the Container API for the appropriate method. I don't remember if it is add(component, index) or add(index, component).

Then once you add the component you need to invoke

panel.revalidate();
panel.repaint();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top