Results 1 to 6 of 6
  1. #1
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Question How to add buttons below a menu bar

    Hello there

    I split up my frame with the following code:


    Java Code:
    JFrame frame = new JFrame("DashingPhoenix 1.1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(createMenuBarPanel(), BorderLayout.NORTH);       
    frame.add(createCenterPanel(), BorderLayout.CENTER);
    frame.add(createStatusBarPanel(), BorderLayout.SOUTH);
    Inside of createMenuBarPanel I created a simple drop down menu.
    Below I want to add some buttons horizontally.

    At the moment I have a a menubar at the top in the center a JTree on the left side and some other content on the right side
    and at the bottom a staus bar.

    How can I add the buttons below the menubar?

    Thank you,
    roobiks

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,656
    Rep Power
    9

    Default Re: How to add buttons below a menu bar

    Please submit an Short, Self Contained, Correct Example that demonstrates the problem. And remember the operative words are short and compilable.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,373
    Blog Entries
    7
    Rep Power
    25

    Default Re: How to add buttons below a menu bar

    Does a JToolBar help? (see the API documentation).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: How to add buttons below a menu bar

    Okay I found many links on how to combine a menubar and a toolbar.
    Unfortunately the toolbar disappears when I move the seperator created in createCenterPanel().
    When I detach de toolbar and put it back under the menubar it is fixed properly.
    How can I fix it like this?
    Please have a look at the code and screenshots for further Information.



    Java Code:
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.Border;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class createpanel4 extends JFrame{
    	
    
    	protected JButton b1;
    	protected static JButton b2;
    	protected static JButton b3;
    	
    	public static Component createCenterPanel(){
    		
    
    		
    		final JSplitPane splitPane = new JSplitPane();
    		splitPane.setLeftComponent(new JScrollPane(new JTree()));
    		splitPane.setRightComponent(new JScrollPane(new JTree()));
    		
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerLocation(300);
            splitPane.setPreferredSize(new Dimension(400, 200));
    		
    		return splitPane;
    		
    	}
    	
    	
    
    	
        private static void createAndShowGUI() {
        	 
      	
            //Create and set up the window.
            JFrame frame = new JFrame("Testframe");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            // Menubar
            JMenuBar menubar = new JMenuBar();
            
            ImageIcon iconNew = new ImageIcon("new.png");
            ImageIcon iconOpen = new ImageIcon("open.png");
            ImageIcon iconSave = new ImageIcon("save.png");
            ImageIcon iconExit = new ImageIcon("exit.png");
    
            JMenu fileMenu = new JMenu("File");
    
            JMenu impMenu = new JMenu("Import");
    
            JMenuItem newsfMi = new JMenuItem("Import newsfeed list...");
            JMenuItem bookmMi = new JMenuItem("Import bookmarks...");
            JMenuItem mailMi = new JMenuItem("Import mail...");
    
            impMenu.add(newsfMi);
            impMenu.add(bookmMi);
            impMenu.add(mailMi);
    
            JMenuItem newMi = new JMenuItem("New", iconNew);
            JMenuItem openMi = new JMenuItem("Open", iconOpen);
            JMenuItem saveMi = new JMenuItem("Save", iconSave);
    
            JMenuItem exitMi = new JMenuItem("Exit", iconExit);
            exitMi.setToolTipText("Exit application");
    
    
            fileMenu.add(newMi);
            fileMenu.add(openMi);
            fileMenu.add(saveMi);
            fileMenu.addSeparator();
            fileMenu.add(impMenu);
            fileMenu.addSeparator();
            fileMenu.add(exitMi);
    
            menubar.add(fileMenu);
     
            //Toolbar
            JToolBar tbar = new JToolBar();
            tbar.setSize(800, 30);
     
            // Buttons
            tbar.add(new JButton("Button1"));
            tbar.add(new JButton("Button2"));
            tbar.add(new JButton("Button3"));
            tbar.add(new JButton("Button4"));
            tbar.add(new JButton("Button5"));
            tbar.add(new JButton("Button6"));
            tbar.add(new JButton("Button7"));
     
            // Menu
            frame.setJMenuBar(menubar);
            
            // Toolbar
            frame.add(tbar);
            
            
            frame.add(createCenterPanel(), BorderLayout.CENTER);
           // frame.add(createStatusBarPanel(), BorderLayout.SOUTH);
            
            //Display the window.
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.pack();
            frame.setVisible(true);
        }
    
    
    	public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Hopefully I have created a better post now.


    Thank you in advance,
    roobiks
    Attached Thumbnails Attached Thumbnails How to add buttons below a menu bar-pic1.png   How to add buttons below a menu bar-pic2.png   How to add buttons below a menu bar-pic3.png  

  5. #5
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: How to add buttons below a menu bar

    I have found a solution.
    Sorry for the inconvenience.

    Kind regards,
    roobiks

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,656
    Rep Power
    9

    Default Re: How to add buttons below a menu bar

    No inconvenience at all! Glad you found a solution. You may want to post it so others may learn.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 9
    Last Post: 07-03-2015, 04:34 PM
  2. Making buttons for a button menu
    By Alkor in forum New To Java
    Replies: 2
    Last Post: 03-21-2012, 06:40 AM
  3. Fill a menu dynamically when menu is shown
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •