user1939168
user1939168

Reputation: 557

A JtabbedPane within a JtabbedPane

I have written a small program for creating a simple gui with tabbed panes.Currently it has two tabs. In one of the tabs i have created a button and in another tabs i have created two buttons(based upon a string in a third class).What i am not able to do is inside the second tab i want to created two tabs ("one" and "two" refered in te code) and the two button that are currently in the tab should be present in each of the sub tabs.Can anybody tell me how can i acheive this?

main class: abc.java

public class abc {
        JFrame frame;
        JTabbedPane tabPane;
        abc_export exp;
        bsm_import2 imp;
        public static void main(String[] args) {
                abc jtab = new abc();
                jtab.start();
        }
        public void start(){
        exp=new abc_export();
        imp=new bsm_import2();
        tabPane.addTab("bsm_export", exp.tab);
        tabPane.addTab("bsm_import2", imp.tab);
        }

        public abc() {
                //  Create a frame
                frame = new JFrame();
                //  Create the tabbed pane.
                tabPane = new JTabbedPane();
                //Adding into frame
                frame.add(tabPane, BorderLayout.CENTER);
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                }

}

Class two:abc_export.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class abc_export {
        JPanel tab;
        public abc_export() {
                //Adding into frame
                tab = new JPanel();
                JButton btn=new JButton("run");
                tab.add(btn);
                tab.setOpaque(false);
                }
};

class three: bsm_import2.java(this is where i need the changes to be done to create sub tabs)

import javax.swing.*;     
import java.awt.*;     
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.StringTokenizer;
import java.util.*;
import java.awt.event.*;


public class bsm_import2 {



    public static JPanel tab;
    public bsm_import2()
    {
    createAndShowGUI();
    } 
    private static void createAndShowGUI() {
           tab=new JPanel(); 
           tab.setLayout(new GridLayout(3,2));
           String line="tab1#one tab2#two";
           String strAry[] = line.split(" ");
           JButton Button[]=new JButton[100];
           final Map<String, String> map = new HashMap<String, String>();
           for(int i =0; i < strAry.length ; i++){
                    String[] parts = strAry[i].split("#");
                    map.put(parts[0],parts[1]);          
                    Button[i] = new JButton(parts[0]);
                    tab.add(Button[i]);
                    tab.setOpaque(false);
            }
                for ( String key : map.keySet() ) {
                System.out.println( key );
                } 
    }
}

enter image description here

Upvotes: 0

Views: 2419

Answers (1)

Harry Joy
Harry Joy

Reputation: 59694

Based on what I understand form your question what you can do is, in the panel of second tab add a new JTabbedPane with 2 tabs having a button in each and you will get what you want. A small demo code will be:

JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane mainTabsPane = new JTabbedPane(); // main tabs pane

//first tab
JButton button = new JButton("Button in first tab");
JPanel jPanel = new JPanel();
jPanel.add(button);
mainTabsPane.addTab("First tab", jPanel);

//second tab
jPanel = new JPanel();
JTabbedPane secondaryTabsPane = new JTabbedPane(); // secondary tabs pane
button = new JButton("Button in second tab ---> first sub tab");
JPanel jPanel2 = new JPanel();
jPanel2.add(button);
secondaryTabsPane.addTab("First tab", jPanel2);
JPanel jPanel3 = new JPanel();
button = new JButton("Button in second tab ---> second sub tab");
jPanel3.add(button);
secondaryTabsPane.addTab("Second tab", jPanel3);
 // add secondary tabs pane to new panel of second tab
jPanel.add(secondaryTabsPane);
 // add new panel to main tabs pane
mainTabsPane.addTab("Second tab", jPanel);

frame.getContentPane().add(mainTabsPane);
frame.setVisible(true);

Other than this you are directly refering to a variable of a class in another class here tabPane.addTab("bsm_export", exp.tab);. You should not do this, instead you should make it private and provide getter/setter to access it. Also why are you having such complicated structure of code for such a small thing, displaying buttons in tabs!!!?? Also always follow Java Coding Convention, class name should not start with small character.

Upvotes: 1

Related Questions