Question

Have been trying to manually write a GUI the past few days and am having a pretty elementary issues. I have a JFrame as the main window and them trying to add JPanels within it with other components in them. Individually the pieces work but having issues adding them together. In the code the issue is in adding a toolbar to a JPanel and then the combination of the two to the JFrame. Followed oracles example on building the toolbar (http://docs.oracle.com/javase/tutorial/uiswing/components/toolbar.html) but I think the issue is either how I am adding it to the frame or the general approach of adding components to a JPanel and then the Frame. Code below.....any thoughts

Frame Class

import javax.swing.*;
import java.awt.*;

public class frmMainMenu {
    public static void main(String main[]){
        //Create Frame
        JFrame frmMainMenu = new JFrame();

        //Size Frame
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int Width = screenSize.width;
        int Height = screenSize.height;

        frmMainMenu.setSize(Width,Height);

        //Add Components
        comLeftToolBar obj1 = new comLeftToolBar();
        frmMainMenu.add(obj1);

        //Display Frame
        frmMainMenu.setVisible(true);
    }
}

Toolbar Class

import java.awt.*;
import javax.swing.*;

public class comLeftToolBar extends JFrame{
    //Create ToolBar
    public void comCreateNavBar() {
        JToolBar comNavToolBar = new JToolBar();
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int Height = screenSize.height;
        comNavToolBar.setSize(50, Height - 100);
        comNavButtons(comNavToolBar);

    }
    //Create Buttons
    public void comNavButtons(JToolBar comNavToolBar) {
        JButton comNavButton = new JButton();
        JButton comProButton = new JButton();

        comNavToolBar.add(comNavButton);
        comNavToolBar.add(comProButton);
    }
    //Create Navigation Bar
    public void comLeftNavBar() {
        JPanel comNavBar = new JPanel();
        comNavBar.add(new comLeftToolBar());

    }
}

Thanks for whatever guidance you all have it is appreciated

Was it helpful?

Solution

There were multiple problems in that code, which indicate to me that you are attempting 'programming by magic'. That just won't work. You need to hit the tutorials and read the JavaDocs in order to have any chance of making a working GUI. This code compiles, runs and displays the tool-bar, but it is still far from ideal.

import javax.swing.*;
import java.awt.*;

public class FrmMainMenu {
    public static void main(String main[]){
        //Create Frame
        JFrame frmMainMenu = new JFrame();

        //Size Frame
        frmMainMenu.setExtendedState(JFrame.MAXIMIZED_BOTH);

        //Add Components
        comLeftToolBar obj1 = new comLeftToolBar();
        obj1.comCreateNavBar();
        frmMainMenu.add(obj1);

        frmMainMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //Display Frame
        frmMainMenu.setVisible(true);
    }
}

class comLeftToolBar extends JPanel{

    //Create ToolBar
    public void comCreateNavBar() {
        setLayout(new BorderLayout());
        JToolBar comNavToolBar = new JToolBar();
        comNavButtons(comNavToolBar);

        add(comNavToolBar, BorderLayout.PAGE_START);
    }

    //Create Buttons
    public void comNavButtons(JToolBar comNavToolBar) {
        JButton comNavButton = new JButton("Nav");
        JButton comProButton = new JButton("Pro");

        comNavToolBar.add(comNavButton);
        comNavToolBar.add(comProButton);
    }

    //Create Navigation Bar
    public void comLeftNavBar() {
        JPanel comNavBar = new JPanel();
        comNavBar.add(new comLeftToolBar());
    }
}

Other tips

  1. Swing GUIs should be created and altered on the EDT (Event Dispatch Thread). See Concurrency in Swing for more details.
  2. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

OTHER TIPS

If the issue is that nothing shows up, then I think you're missing a setContentPane() in there. Try

frmMainMenu.setContentPane(comLeftToolBar);

before executing frmMainMenu.setVisible();.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top