1

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

2

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.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for the insight and especially the tips at the bottom, I know my code is far from ideal but guidelines like your other tips let me know what is ideal. Thanks – Rich Becker Jun 12 '13 at 23:00
0

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();.

Adam Smith
  • 20
  • 3
  • That is definatly one of the issues I had but I just got a compile error "Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container" and at the bottom of the code it ties back to the .add method. Changed a few things in the code to reflect the setContentPane() , but still looks like i am adding the components wrong..... //Add Components comLeftToolBar obj1 = new comLeftToolBar(); frmMainMenu.add(obj1); //Display Frame frmMainMenu.setContentPane(obj1); frmMainMenu.setVisible(true); – Rich Becker Jun 12 '13 at 10:20