Question

I m trying out some things with java and swing. I am combining different tutorials and example codes. What I intendo to do here is build a jframe with tabs, and each tab will contain a jpanel with some jcomponents. First tab just has a textarea with a picture in a BoxLayout, 2nd and 3rd tabs have a jlist with some items, that when they are clicked they open some files. When I run the code I get the following errors. Any help would be really appreciated.

Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
    at javax.swing.BoxLayout.checkContainer(Unknown Source)
    at javax.swing.BoxLayout.invalidateLayout(Unknown Source)
    at javax.swing.BoxLayout.addLayoutComponent(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at TabbedPaneDemo.createInnerText(TabbedPaneDemo.java:133)
    at TabbedPaneDemo.<init>(TabbedPaneDemo.java:51)
    at TabbedPaneDemo.main(TabbedPaneDemo.java:152)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

public class TabbedPaneDemo extends JPanel {

    public TabbedPaneDemo() {

        JTabbedPane jtbExample = new JTabbedPane();

        JPanel jplInnerPanel1 = createInnerText("Text");
        jtbExample.addTab("General Info", null, jplInnerPanel1, "Tab 1");
        jtbExample.setSelectedIndex(0);

        JPanel jplInnerPanel2 = createInnerList();
        jtbExample.addTab("Files", null, jplInnerPanel2, "Tab 2");

        JPanel jplInnerPanel3 = createInnerList();
        jtbExample.addTab("Files", null, jplInnerPanel3, "Tab 3");

        // Add the tabbed pane to this panel.
        setLayout(new GridLayout(1, 1));
        add(jtbExample);
    }
    protected JPanel createInnerList() {

        JPanel jplPanel = new JPanel(new BorderLayout());

        JLabel label1 = new JLabel("Click to select File to Open");
        jplPanel.add(label1, BorderLayout.PAGE_START );

        String[] pdfstr = {"1st", "2nd"};
        JList list = new JList(pdfstr);
        JScrollPane scrollPane1 = new JScrollPane(list);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                JList list = (JList)evt.getSource();
                if (evt.getClickCount() == 2) {
                    int index = list.locationToIndex(evt.getPoint());
                    if (index==1) {
                    try {

                        File pdfFile = new File(".pdf");
                        if (pdfFile.exists()) {

                            if (Desktop.isDesktopSupported()) {
                                Desktop.getDesktop().open(pdfFile);
                            } else {
                                System.out.println("Awt Desktop is not supported!");
                            }

                        } else {
                            System.out.println("File is not exists!");
                        }


                      } catch (Exception ex) {
                        ex.printStackTrace();
                      }
                    }
                } else if (evt.getClickCount() == 3) {   // Triple-click
                    int index = list.locationToIndex(evt.getPoint());

                }
            }
        });

//      jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(scrollPane1, BorderLayout.CENTER);
        return jplPanel;
    }

    protected JPanel createInnerText(String text) {
        Container jplPanel2 = new Container();
        JPanel jplPanel = new JPanel(new BoxLayout(jplPanel2, BoxLayout.Y_AXIS));
        JTextArea textarea = new JTextArea(200, 200);
                textarea.setName("General Info");
                textarea.setText(text);
                textarea.setEditable(false);
                textarea.setFont(new Font("Serif", Font.ITALIC, 14));
                textarea.setForeground(Color.BLACK);
                textarea.setBackground(new Color(0, 0, 0, 007));
                textarea.setLineWrap(true);
                textarea.setWrapStyleWord(true);

                JScrollPane areaScrollPane = new JScrollPane(textarea);
                areaScrollPane.setVerticalScrollBarPolicy(
                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                areaScrollPane.setPreferredSize(new Dimension(400, 400));
                jplPanel.add(areaScrollPane);

                try {
                BufferedImage myPicture = ImageIO.read(new File(".jpg"));
                JLabel picLabel = new JLabel(new ImageIcon(myPicture));
                jplPanel.add(picLabel);
                } catch (IOException ex) {
                    System.out.println("Error");
               }


                return jplPanel;

    }


    public static void main(String[] args) throws HeadlessException, IOException {
        JFrame frame = new JFrame("TabbedPane Source Demo") ;

        frame.getContentPane().add(new TabbedPaneDemo(),
                BorderLayout.CENTER);
        frame.setResizable(false);
        frame.setSize(800, 700);
        frame.setVisible(true);

}
}
Was it helpful?

Solution

Set the layout for the JPanel jplPanel after the container has been instantiated and without using a LayoutManager argument such that layout manager is managing a single container

JPanel jplPanel = new JPanel();
jplPanel.setLayout(new BoxLayout(jplPanel, BoxLayout.Y_AXIS));

Read: How to Use BoxLayout

OTHER TIPS

Change

    Container jplPanel2 = new Container();
    JPanel jplPanel = new JPanel(new BoxLayout(jplPanel2, BoxLayout.Y_AXIS));

to

      JPanel jplPanel = new JPanel();
      jplPanel.setLayout(new BoxLayout(jplPanel, BoxLayout.Y_AXIS));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top