Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Auto-resize the components of JTabbedPane with JPanels

Sambasivarao Medavarapu-OracleOct 6 2014 — edited Jan 15 2015

Hi,

We have swing window with tabbed pane having two JPanels. There are couple of components (button/text

fields/JXTable/Jtree ..etc)added to each panel. so far, every thing is working fine.

Issue is, when we try to expand the winodw size, the components of each panel are static. The size of components

are not changing accorning to window size. All the components are center alligned with increasing the window size. It would be great help, if you can help on this.

Following is the sample code used to prepare  swing window.

public class SOS extends JFrame implements TreeWillExpandListener,

                                           TreeExpansionListener,

    JPanel firstPanel = new JPanel(new GridBagLayout());

    JPanel secondPanel = new JPanel(new GridBagLayout());

    public SOS() throws SQLException, ParserConfigurationException,

                        SAXException, IOException, XPathExpressionException {

        //EHDD Tabbed Pane

        nehObjectTypelist = new JList((createObjectsListData()).toArray());

        nehObjectTypelist.setFont(font);

        nehObjectTypelist.setCellRenderer(new CheckListRenderer());

        nehObjectTypelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        nehObjectTypelist.setBorder(new EmptyBorder(0, 4, 0, 0));

        nehObjectTypelist.addMouseListener(new MouseAdapter() {

            });

        JScrollPane nehObjTypeSP = new JScrollPane(nehObjectTypelist);

        nehObjTypeSP.setSize(200, 200);

        nehOwmerslist = new JList((createOwnersListData()).toArray());

        nehOwmerslist.setFont(font);

        nehOwmerslist.setCellRenderer(new CheckListRenderer());

        nehOwmerslist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        nehOwmerslist.setBorder(new EmptyBorder(0, 4, 0, 0));

        nehOwmerslist.addMouseListener(new MouseAdapter() {

            });

        JScrollPane sp = new JScrollPane(nehOwmerslist);

        sp.setSize(200, 200);

        //JTree with checkbox

        DefaultMutableTreeNode root = createFirstLevel();

        final DefaultTreeModel model = new DefaultTreeModel(root);

        nehTree = new JTree(root);

        QuestionCellRenderer renderer = new QuestionCellRenderer();

        nehTree.setCellRenderer(renderer);

        QuestionCellEditor editor = new QuestionCellEditor();

        nehTree.setCellEditor(editor);

        nehTree.setEditable(true);

        nehTree.addTreeWillExpandListener(this);

        nehTree.setShowsRootHandles(true);

        nehTree.setModel(model);

        nehTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

        JScrollPane scroll = new JScrollPane(nehTree);

        scroll.setPreferredSize(new Dimension(200, 180));

        scroll.setFont(font);

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets = new Insets(1, 1, 1, 1);

        gbc.gridx = 0;

        gbc.gridy = 0;

        //gbc.gridwidth = 2;

        gbc.fill = GridBagConstraints.HORIZONTAL;

        //gbc.anchor = GridBagConstraints.LINE_START;

        JLabel nehHeading =

            new JLabel("Select your criteria and click the Fetch Topics by Criteria button");

        nehHeading.setFont(font);

        firstPanel.add(nehHeading, gbc);

        gbc.insets = new Insets(1, 1, 1, 1);

        gbc.fill = GridBagConstraints.HORIZONTAL;

        gbc.gridx = 0;

        gbc.gridy = 1;

        gbc.gridwidth = 1;

        JLabel nehDocType = new JLabel("Type");

        nehDocType.setFont(font);

        firstPanel.add(nehDocType, gbc);

        gbc.gridx = 1;

        gbc.gridy = 1;

        JLabel nehDocOwner = new JLabel("Owner");

        nehDocOwner.setFont(font);

        firstPanel.add(nehDocOwner, gbc);

        gbc.gridx = 2;

        gbc.gridy = 1;

        JLabel nehProdTree = new JLabel("Tree");

        nehProdTree.setFont(font);

        firstPanel.add(nehProdTree, gbc);

        gbc.weightx = 0;

        gbc.weighty = 0;

        gbc.gridx = 0;

        gbc.gridy = 2;

        firstPanel.add(nehObjTypeSP, gbc);

        gbc.gridx = 1;

        gbc.gridy = 2;

        firstPanel.add(sp, gbc);

        gbc.gridx = 2;

        gbc.gridy = 2;

        firstPanel.add(scroll, gbc);

        gbc.fill = GridBagConstraints.VERTICAL;

        gbc.anchor = GridBagConstraints.FIRST_LINE_START;

        gbc.weightx = 0;

        gbc.weighty = 0;

        gbc.gridheight = 1;

        gbc.gridheight = 1;

        gbc.gridx = 0;

        gbc.gridy = 21;

        JButton fetchButton = new JButton("Fetch");

        fetchButton.setFont(font);

        fetchButton.setSize(new Dimension(175, 35));

        firstPanel.add(fetchButton, gbc);

        fetchButton.addActionListener(new ActionListener() {

            });

        gbc.gridx = 0;

        gbc.gridy = 22;

        JLabel nehOR = new JLabel("- OR -");

        nehOR.setFont(font);

        firstPanel.add(nehOR, gbc);

        JButton searchButton = new JButton("Fetch by Title");

        searchButton.setToolTipText("Enter title search criteria");

        searchButton.setFont(font);

        gbc.gridheight = 1;

        gbc.gridx = 0;

        gbc.gridy = 23;

        gbc.anchor = GridBagConstraints.LINE_START;

        JLabel nehOpenBySVNIdLabel = new JLabel("Open by ID");

        nehOpenBySVNIdLabel.setFont(font);

        firstPanel.add(nehOpenBySVNIdLabel, gbc);

        gbc.gridheight = 1;

        gbc.gridx = 1;

        gbc.gridy = 23;

        gbc.anchor = GridBagConstraints.WEST;

        findTextField.setColumns(20);

        firstPanel.add(findTextField, gbc);

        gbc.gridheight = 1;

        gbc.gridx = 2;

        gbc.gridy = 23;

        gbc.anchor = GridBagConstraints.WEST;

        JButton openButton = new JButton("Open");

        openButton.setFont(font);

        openButton.setPreferredSize(searchButton.getPreferredSize());

        System.out.println("Button Size" + openButton.getSize());

        firstPanel.add(openButton, gbc);

        openButton.addActionListener(new ActionListener() {

            });

        gbc.gridheight = 1;

        gbc.gridx = 0;

        gbc.gridy = 24;

        gbc.anchor = GridBagConstraints.LINE_START;

        JLabel searchLabel =

            new JLabel("Search the repository by title word(s)");

        searchLabel.setToolTipText("Enter title search criteria");

        searchLabel.setFont(font);

        firstPanel.add(searchLabel, gbc);

        gbc.gridheight = 1;

        gbc.gridx = 1;

        gbc.gridy = 24;

        gbc.anchor = GridBagConstraints.WEST;

        searchTextField = new JTextField("", 20);

        searchTextField.setColumns(20);

        searchTextField.setEditable(true);

        firstPanel.add(searchTextField, gbc);

        gbc.gridheight = 1;

        gbc.gridx = 2;

        gbc.gridy = 24;

        gbc.anchor = GridBagConstraints.WEST;

        firstPanel.add(searchButton, gbc);

        searchButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

            });

        gbc.anchor = GridBagConstraints.FIRST_LINE_START;

        gbc.gridwidth = 4;

        gbc.gridx = 0;

        gbc.gridy = 28;

        gbc.fill = GridBagConstraints.HORIZONTAL;

        tableScrollPane.setPreferredSize(new Dimension(200, 200));

        firstPanel.add(tableScrollPane, gbc);

        gbc.fill = GridBagConstraints.NONE;

        gbc.gridheight = 1;

        JRadioButton reviewButton = new JRadioButton("Review");

        reviewButton.setFont(font);

        reviewButton.setMnemonic(KeyEvent.VK_B);

        reviewButton.setSelected(true);

        JRadioButton testButton = new JRadioButton("Test");

        testButton.setFont(font);

        testButton.setMnemonic(KeyEvent.VK_B);

        JRadioButton prodButton = new JRadioButton("Production");

        prodButton.setFont(font);

        prodButton.setMnemonic(KeyEvent.VK_B);

        nehGroup.add(reviewButton);

        nehGroup.add(testButton);

        nehGroup.add(prodButton);

        gbc.gridx = 2;

        gbc.gridy = 30;

        firstPanel.add(reviewButton, gbc);

        gbc.gridx = 2;

        gbc.gridy = 31;

        firstPanel.add(testButton, gbc);

        gbc.gridx = 2;

        gbc.gridy = 32;

        firstPanel.add(prodButton, gbc);

        gbc.gridx = 2;

        gbc.gridy = 33;

        JButton printButton = new JButton("Print");

        printButton.setFont(font);

        firstPanel.add(printButton, gbc);

        printButton.addActionListener(new ActionListener() {

            });

        gbc.fill = GridBagConstraints.NORTHEAST;

        gbc.gridx = 0;

        gbc.gridy = 30;

        JButton openTopicsButton = new JButton("Open Selection");

        openTopicsButton.setFont(font);

        firstPanel.add(openTopicsButton, gbc);

        openTopicsButton.addActionListener(new ActionListener() {

            });

        gbc.gridx = 0;

        gbc.gridy = 32;

        JButton mapLinks = new JButton("Insert");

        mapLinks.setFont(font);

        JButton relatedLinks = new JButton("Insert Links");

        relatedLinks.setFont(font);

        relatedLinks.setPreferredSize(mapLinks.getPreferredSize());

        firstPanel.add(relatedLinks, gbc);

        relatedLinks.addActionListener(new ActionListener() {

            });

        gbc.gridx = 0;

        gbc.gridy = 33;

        firstPanel.add(mapLinks, gbc);

        mapLinks.addActionListener(new ActionListener() {

            });

        tabbedPane.add("NEH", firstPanel);

        //NEH Tabbed Pane

        tabbedPane.add("EHDD", secondPanel);

        add(tabbedPane);

        tabbedPane.addChangeListener(new ChangeListener() {

    });

   

    }

    public static void main(String[] args) throws SQLException,

                                                  ParserConfigurationException,

                                                  SAXException, IOException,

                                                  XPathExpressionException {

        SOS sos = new SOS();

        sos.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        sos.setTitle("SOS");

        sos.setSize(760, 680);

        sos.setVisible(true);

        sos.toFront();

        sos.setMinimumSize(new Dimension(760, 680));

    }

}

Thanks,

MSR.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 12 2015
Added on Oct 6 2014
1 comment
1,426 views