JMenuBar

Hi i am trying to create a JMenuBar on my small prog in swing, i can do it in AWT but in swing i am getting this error,apart from the Menu code i think all other code is correct.
Any pointers will be helpfull.
---------------- JDK Release Build ------------------
Compiling C:\WINDOWS\Desktop\PlafTest\TroisTest.java
C:\WINDOWS\Desktop\PlafTest\TroisTest.java:19: Method setJMenuBar(javax.swing.JMenuBar) not found in class TroisPanel.
setJMenuBar(menuBar);
^
1 error
Finished
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TroisPanel extends JPanel
     implements ActionListener
{  public TroisPanel()
          setLayout(null);
          setBackground(Color.green);
          JMenuBar menuBar = new JMenuBar();
          setJMenuBar(menuBar);
          JMenu fileMenu = new JMenu("File");
          menuBar.add(fileMenu);
          closeItem = new JMenuItem("Close");
          //closeItem.addActionListener(this); not using this yet //
          fileMenu.add(closeItem);
          englishButton = new JButton("English");
          add(englishButton);
          englishButton.setBounds(140,140,90,30);
          englishButton.addActionListener(this);
          polishButton = new JButton("Polish");
          add(polishButton);
          polishButton.setBounds(140,210,90,30);
          polishButton.addActionListener(this);
          italianButton = new JButton("Italian");
          add(italianButton);
          italianButton.setBounds(140,280,90,30);
          italianButton.addActionListener(this);
          JComboBox list1 = new JComboBox();
          list1.setEditable(false);
          list1.setBounds(390,140,160,30);     
          list1.addItem("English_");
          list1.addItem("Polish_");
          list1.addItem("Italian_");
          list1.addItem("Empty_");
          list1.addItem("Empty_");
          add(list1);
          list1.addActionListener(this);
     public void paintComponent(Graphics g){
          super.paintComponent(g);
          Font p = new Font ("Ariel",Font.BOLD, 24);
          g.setFont(p);
          g.drawString("My First Swing Application",170,50);
          Toolkit kit = Toolkit.getDefaultToolkit();     
          Image image3 = kit.getImage("de-t.gif");
          g.drawImage(image3,50,137, this);     
          Image image = kit.getImage("uk.gif");
          g.drawImage(image,50,206, this);
          Image image2 = kit.getImage("fr-t.gif");
          g.drawImage(image2,50,276, this);
     public void actionPerformed(ActionEvent evt)
     {  Object source = evt.getSource();
          if (source == englishButton){
               java.awt.FileDialog fd = new java.awt.FileDialog(new java.awt.Frame());
               fd.show();
     private JButton englishButton;
     private JButton polishButton;
     private JButton italianButton;
     private JComboBox list1;
     private JMenuItem closeItem;
class TroisFrame extends JFrame
{  public TroisFrame()
     {  setTitle("Trois Swing");
          setBounds(90,30,660,520);
          setResizable(false);
          addWindowListener(new WindowAdapter()
               {  public void windowClosing(WindowEvent e)
                    {  System.exit(0);
          Container contentPane = getContentPane();
          contentPane.add(new TroisPanel());
public class TroisTest
{  public static void main(String[] args)
     {  JFrame frame = new TroisFrame();
          frame.show();

There's no setMenu method in JPanel. You can add the JMenuBar into your JPanel. Set your JPanel to borderlayout, and add the menubar to the north, it will look the same as menus in windows.

Similar Messages

  • Can i call a JMenuBar in a number of JFrame(s) ?

    Hi ,
    I've a program with a number of JFrames.
    I wanted to add a JMenuBar for each frame of my program, this menu bar has the same design code and the same events code in all the program frames, so is there a way to create this JMenuBar once only & call it some how in every JFrame to load it directly instead of writing its same code a lot of times for every JFrame ?
    Thanks ,
    Hesham

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    class Test{
         public static void main( String[] argv ) throws Throwable {
              for(int i=0; i<10; i++){
                   SuperFrame f = new SuperFrame();
                   f.setLocation(i*10,i*10);
                   f.setSize(400,300);
                   f.setVisible(true);
    class SuperFrame extends JFrame{
         private static final long serialVersionUID = 1L;
          * All the menu item goes here
         JMenuBar mbar = new JMenuBar();
         JMenu menu = new JMenu("Menu");
         JMenuItem menuItem1  = new JMenuItem("Close");
         JMenuItem menuItem2  = new JMenuItem("Exit");
         public SuperFrame(){
              super("Testing");
              this.menu.add(menuItem1);
              this.menu.add(menuItem2);
              this.mbar.add(menu);
              this.setJMenuBar(mbar);
              menuItem1.addActionListener(new ActionListener(){
                   @Override
                   public void actionPerformed(ActionEvent arg0) {
                        dispose();
              menuItem2.addActionListener(new ActionListener(){
                   @Override
                   public void actionPerformed(ActionEvent arg0) {
                        System.exit(0);
    }

  • Problems getting data from JMenuBar to JInternalFrame

    I have modified an example to show my problem. I am trying to take text input from the menu bar and print it into a JTextArea. I don't know how to reference the data in my ActionListener.
    The ActionListener is incomplete, but this is basically what I am trying to do:
    import javax.swing.JInternalFrame;
    import javax.swing.JDesktopPane;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JMenuBar;
    import javax.swing.JFrame;
    import javax.swing.KeyStroke;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    * InternalFrameDemo.java is a 1.4 application that requires:
    *   MyInternalFrame.java
    public class InternalFrameDemo extends JFrame
                                   implements ActionListener {
        JDesktopPane desktop;
        JTextField memAddrBox;
        JTextArea menuText;
        JTextArea frame;
        String textFieldString = " Input Text: ";
        ActionListener al;
        public InternalFrameDemo() {
            super("InternalFrameDemo");
            //Make the big window be indented 50 pixels from each edge
            //of the screen.
            int inset = 50;
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds(inset, inset,
                      screenSize.width  - inset*2,
                      screenSize.height - inset*2);
            //Set up the GUI.
            desktop = new JDesktopPane(); //a specialized layered pane
            frame = createFrame(); //create first "window"
            setContentPane(desktop);
            setJMenuBar(createMenuBar());
            //Make dragging a little faster but perhaps uglier.
            desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
        protected JMenuBar createMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            JLabel memAddrLabel = new JLabel(textFieldString);
            memAddrLabel.setLabelFor(memAddrBox);
            menuBar.add(memAddrLabel);
            JTextField memAddrBox = new JTextField();
            memAddrBox.addActionListener(this);
            memAddrBox.setActionCommand("chMemAddr");
            menuBar.add(memAddrBox);
            return menuBar;
        //React to menu selections.
        public void actionPerformed(ActionEvent e) {
            if ("chMemAddr".equals(e.getActionCommand())) 
                JMenuBar bar = getJMenuBar();
    //            JTextField memAddrBox = bar.getParent();
                String memStartString = memAddrBox.getText();
                update(memStartString);
        public void update(String temp)
            frame.setText(temp);
        //Create a new internal frame.
        protected JTextArea createFrame() {
            JInternalFrame frame = new JInternalFrame("Memory",true,true,true,true);      
            frame.setSize(650, 500);
            frame.setVisible(true);
            frame.setLocation(200, 0);
            desktop.add(frame);  
            JTextArea textArea = new JTextArea();
            frame.getContentPane().add("Center", textArea);
            textArea.setFont(new Font("SansSerif", Font.PLAIN, 12));
            textArea.setVisible(true);
            textArea.setText("Initial Text");
            return textArea;
        //Quit the application.
        protected void quit() {
            System.exit(0);
        public static void main(String[] args) {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
            //Create and set up the window.
            InternalFrameDemo frame = new InternalFrameDemo();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Display the window.
            frame.setVisible(true);
    class MyInternalFrame extends JInternalFrame {
        static int openFrameCount = 0;
        static final int xOffset = 30, yOffset = 30;
        public MyInternalFrame() {
            super("Document #" + (++openFrameCount),
                  true, //resizable
                  true, //closable
                  true, //maximizable
                  true);//iconifiable
            //...Create the GUI and put it in the window...
            //...Then set the window size or call pack...
            setSize(300,300);
            //Set the window's location.
            setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }I want to take the input from the "memAddrBox" and put it into the "frame" using the update() method. Probably a simple solution, but I have not found a similar problem in the Forums.

    I knew it had to be something simple, here is the fix I found:
    import javax.swing.JInternalFrame;
    import javax.swing.JDesktopPane;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JMenuBar;
    import javax.swing.JFrame;
    import javax.swing.KeyStroke;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    * InternalFrameDemo.java is a 1.4 application that requires:
    *   MyInternalFrame.java
    public class InternalFrameDemo extends JFrame
                                   implements ActionListener {
        JDesktopPane desktop;
        JTextField memAddrBox;
        JTextArea menuText;
        JTextArea frame;
        String textFieldString = " Input Text: ";
        ActionListener al;
        public InternalFrameDemo() {
            super("InternalFrameDemo");
            //Make the big window be indented 50 pixels from each edge
            //of the screen.
            int inset = 50;
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds(inset, inset,
                      screenSize.width  - inset*2,
                      screenSize.height - inset*2);
            //Set up the GUI.
            desktop = new JDesktopPane(); //a specialized layered pane
            frame = createFrame(); //create first "window"
            setContentPane(desktop);
            setJMenuBar(createMenuBar());
            //Make dragging a little faster but perhaps uglier.
            desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
        protected JMenuBar createMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            JLabel memAddrLabel = new JLabel(textFieldString);
            memAddrLabel.setLabelFor(memAddrBox);
            menuBar.add(memAddrLabel);
            JTextField memAddrBox = new JTextField();
            memAddrBox.addActionListener(this);
            memAddrBox.setActionCommand("chMemAddr");
            menuBar.add(memAddrBox);
            return menuBar;
        //React to menu selections.
        public void actionPerformed(ActionEvent e) {
            if ("chMemAddr".equals(e.getActionCommand())) 
               JTextField textField =
                 (JTextField)e.getSource();
                String memStartString = textField.getText();
                update(memStartString);
        public void update(String temp)
            frame.setText(temp);
        //Create a new internal frame.
        protected JTextArea createFrame() {
            JInternalFrame frame = new JInternalFrame("Memory",true,true,true,true);      
            frame.setSize(650, 500);
            frame.setVisible(true);
            frame.setLocation(200, 0);
            desktop.add(frame);  
            JTextArea textArea = new JTextArea();
            frame.getContentPane().add("Center", textArea);
            textArea.setFont(new Font("SansSerif", Font.PLAIN, 12));
            textArea.setVisible(true);
            textArea.setText("Initial Text");
            return textArea;
        //Quit the application.
        protected void quit() {
            System.exit(0);
        public static void main(String[] args) {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
            //Create and set up the window.
            InternalFrameDemo frame = new InternalFrameDemo();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Display the window.
            frame.setVisible(true);
    class MyInternalFrame extends JInternalFrame {
        static int openFrameCount = 0;
        static final int xOffset = 30, yOffset = 30;
        public MyInternalFrame() {
            super("Document #" + (++openFrameCount),
                  true, //resizable
                  true, //closable
                  true, //maximizable
                  true);//iconifiable
            //...Create the GUI and put it in the window...
            //...Then set the window size or call pack...
            setSize(300,300);
            //Set the window's location.
            setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }Note the e.getSource() change in the ActionListener.

  • How to add a JMenubar and a JTable in a JFrame in a single application

    Hi all,
    I require an urgent help from you.I am a beginer in programming Swing.I want to add a menu,combobox,and a table in a single application.I did coding as below:
    package com.BSS;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.border.*;
    import javax.swing.event.*;
    public class newssa extends JFrame
         public JMenuBar menuBar;
         public JToolBar toolBar;
         public JFrame frame;
         private JLabel jLabel1;
         private JLabel jLabel2;
         private JLabel jLabel3;
         private JLabel jLabel4;
         private JLabel jLabel5;
         private JLabel jLabel6;
         private JComboBox jComboBox1;
         private JComboBox jComboBox2;
         private JComboBox jComboBox3;
         private JComboBox jComboBox4;
         private JComboBox jComboBox5;
         private JComboBox jComboBox6;
         private JTable jTable1;
         private JScrollPane jScrollPane1;
         private JPanel contentPane;
         public newssa()
              super();
              initializeComponent();
              this.setVisible(true);
         private void initializeComponent()
              jLabel1 = new JLabel();
              jLabel2 = new JLabel();
              jLabel3 = new JLabel();
              jLabel4 = new JLabel();
              jLabel5 = new JLabel();
              jLabel6 = new JLabel();
              jComboBox1 = new JComboBox();
              jComboBox2 = new JComboBox();
              jComboBox3 = new JComboBox();
              jComboBox4 = new JComboBox();
              jComboBox5 = new JComboBox();
              jComboBox6 = new JComboBox();
              frame=new JFrame();
              //Included here
              JMenuBar menuBar = new JMenuBar();
              JMenu general = new JMenu("General");
         menuBar.add(general);
         JMenu actions =new JMenu("Actions");
         menuBar.add(actions);
         JMenu view=new JMenu("View");
         menuBar.add(view);
         JMenu Timescale=new JMenu("TimeScale");
         menuBar.add(Timescale);
         Timescale.add("Today CTRL+D");
         Timescale.add("Current Week CTRL+W");
         Timescale.add("Current Month CTRL+M");
         Timescale.add("Current Quarter CTRL+Q");
         Timescale.add("Current Year CTRL+Y");
         Timescale.add("Custom TimeScale CTRL+U");
         JMenu start=new JMenu("Start");
         menuBar.add(start);
         JMenu options=new JMenu("Options");
         menuBar.add(options);
         JMenu help=new JMenu("Help");
         menuBar.add(help);
         JFrame.setDefaultLookAndFeelDecorated(true);
         frame.setJMenuBar(menuBar);
         frame.pack();
         frame.setVisible(true);
         toolBar = new JToolBar("Formatting");
         toolBar.addSeparator();
              //Before this included new
              String columnNames[] = { "ColorStatus", "Flash", "Service Order","Configuration","Configuration Description"};
              // Create some data
              String dataValues[][] =
                   { "blue", "flash", "ORT001" },
                   { "AVCONF", "av configuration with warrenty"}
              // Create a new table instance
              //jTable1 = new JTable( dataValues, columnNames );
              jTable1 = new JTable(dataValues,columnNames);
              jScrollPane1 = new JScrollPane(jTable1);
              contentPane = (JPanel)this.getContentPane();
              //scrollPane = new JScrollPane( table );
              //topPanel.add( scrollPane, BorderLayout.CENTER );
              // jLabel1
              jLabel1.setText("Service Centers");
              // jLabel2
              jLabel2.setText("Service Areas");
              // jLabel4
              jLabel3.setText("Skills");
              jLabel4.setText("Availablity Types");
              // jLabel5
              jLabel5.setText("From Date");
              // jLabel6
              jLabel6.setText("To");
              // jComboBox1
              jComboBox1.addItem("Coimbatore");
              jComboBox1.addItem("Chennai");
              jComboBox1.addItem("Mumbai");
              jComboBox1.addItem("New Delhi");
              jComboBox1.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox1_actionPerformed(e);
              // jComboBox2
              jComboBox2.addItem("North Zone");
              jComboBox2.addItem("South Zone");
              jComboBox2.addItem("Central Zone");
              jComboBox2.addItem("Eastern Zone");
              jComboBox2.addItem("Western Zone");
              jComboBox2.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox2_actionPerformed(e);
              // jComboBox3
              jComboBox3.addItem("Microsoft Components");
              jComboBox3.addItem("Java Technologies");
              jComboBox3.addItem("ERP");
              jComboBox3.addItem("Others");
              jComboBox3.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox3_actionPerformed(e);
              // jComboBox4
              jComboBox4.addItem("One");
              jComboBox4.addItem("Two");
              jComboBox4.addItem("Three");
              jComboBox4.addItem("Four");
              jComboBox4.addItem("Five");
              jComboBox4.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox4_actionPerformed(e);
              // jComboBox5
              jComboBox5.addItem("12/12/2004");
              jComboBox5.addItem("13/12/2004");
              jComboBox5.addItem("14/12/2004");
              jComboBox5.setEditable(true);
              jComboBox5.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox5_actionPerformed(e);
              // jComboBox6
              jComboBox6.addItem("12/11/2004");
              jComboBox6.addItem("13/11/2004");
              jComboBox6.addItem("14/11/2004");
              jComboBox6.setEditable(true);
              jComboBox6.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e)
                        jComboBox6_actionPerformed(e);
              // jTable1
              jTable1.setModel(new DefaultTableModel(4, 4));
              // jScrollPane1
              jScrollPane1.setViewportView(jTable1);
              // contentPane
              contentPane.setLayout(null);
              addComponent(contentPane, jLabel1, 2,29,84,18);
              addComponent(contentPane, jLabel2, 201,33,76,18);
              addComponent(contentPane, jLabel3, 384,32,59,18);
              addComponent(contentPane, jLabel4, 2,77,85,18);
              addComponent(contentPane, jLabel5, 197,79,84,18);
              addComponent(contentPane, jLabel6, 384,80,60,18);
              addComponent(contentPane, jComboBox1, 85,32,100,22);
              addComponent(contentPane, jComboBox2, 276,32,100,22);
              addComponent(contentPane, jComboBox3, 419,30,100,22);
              addComponent(contentPane, jComboBox4, 88,76,100,22);
              addComponent(contentPane, jComboBox5, 276,79,100,22);
              addComponent(contentPane, jComboBox6, 421,78,100,22);
              addComponent(contentPane, jScrollPane1, 33,158,504,170);
              // newssa
              this.setTitle("SSA Service Scheduler");
              this.setLocation(new Point(0, 0));
              this.setSize(new Dimension(560, 485));
         /** Add Component Without a Layout Manager (Absolute Positioning) */
         private void addComponent(Container container,Component c,int x,int y,int width,int height)
              c.setBounds(x,y,width,height);
              container.add(c);
         // TODO: Add any appropriate code in the following Event Handling Methods
         private void jComboBox1_actionPerformed(ActionEvent e)
              int index = jComboBox1.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("Area Coimbatore Selected "); break;
                   case 1: System.out.println("Area Chennai selected"); break;
                   case 2: System.out.println("Mumbai being selected"); break;
                   case 3: System.out.println("New Delhi being selected"); break;
         private void jComboBox2_actionPerformed(ActionEvent e)
              int index = jComboBox2.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("North Zone Selcted "); break;
                   case 1: System.out.println("South Zone being selected"); break;
                   case 2: System.out.println("Central Zone being selected"); break;
                   case 3: System.out.println("Eastern Zone being selected"); break;
                   case 4: System.out.println("Western Zone being selected"); break;
         private void jComboBox3_actionPerformed(ActionEvent e)
              int index = jComboBox3.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("Microsoft Components being selected"); break;
                   case 1: System.out.println("Java Technologies being selected"); break;
                   case 2: System.out.println("ERP Tehnologies being selected"); break;
                   case 3: System.out.println("Other's selected"); break;
         private void jComboBox4_actionPerformed(ActionEvent e)
              int index = jComboBox4.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("One selected"); break;
                   case 1: System.out.println("Two selected"); break;
                   case 2: System.out.println("Three selected"); break;
                   case 3: System.out.println("Four selected"); break;
                   case 4: System.out.println("Five selected"); break;
         private void jComboBox5_actionPerformed(ActionEvent e)
              int index = jComboBox5.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("12/12/2004 being selected"); break;
                   case 1: System.out.println("13/12/2004 being selected"); break;
                   case 2: System.out.println("14/12/2004 being selected"); break;
         private void jComboBox6_actionPerformed(ActionEvent e)
              int index = jComboBox6.getSelectedIndex();
              switch(index)
                   case 0: System.out.println("12/11/2004 being selected"); break;
                   case 1: System.out.println("13/11/2004 being selected"); break;
                   case 2: System.out.println("14/11/2004 being selected"); break;
         public static void main(String[] args)
              newssa ssa=new newssa();
              //JFrame.setDefaultLookAndFeelDecorated(true);
              //JDialog.setDefaultLookAndFeelDecorated(true);
              //JFrame frame = new JFrame("SSA Service Scheduler");
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setJMenuBar(ssa.menuBar);
    //frame.getContentPane( ).add(ssa.toolBar, BorderLayout.NORTH);
    //frame.getContentPane( ).add(ssa.pane, BorderLayout.CENTER);
    //frame.pack( );
    //frame.setVisible(true);
              try
                   //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                   UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
              catch (Exception ex)
                   System.out.println("Failed loading L&F: ");
                   System.out.println(ex);
    But as a O/P ,I am getting menu in a seperate windos and the rest of the combobox and jtable in a seperate window.Kindly help me to solve the application.
    VERY URGENT PLEASE..
    Thanks in advance
    with kind regds
    Satheesh.K

    But did u mean this as the next problem,Which I will come across..Yes, the second setVisible(true) seemed to be producing a smaller frame behind the main frame.
    And your JMenuBar is declared twice, but not sure if this will affect the code - haven't read it all.

  • JCombobox with JMenuBar (as drop down menu)

    Hello, i'm trying to make a new component (as generic as possible) to display a tree structure in a combo-like style.
    So i finally choose a JTextfield to display the current item + a JMenuBar with Menu and MenuItem generated from a TreeModel (i'm not sure this is the better choice to make, but tha's not my present problem).
    2 questions :
    1) am I reinventing the wheel ???
    2) i've got a problem of pack when displaying the compent and i don't find where.
    Thanks. Cyril.
    So, the code... two main classes : TreeComboBox + ArrowMenu
    the TreeComboBox :
    package fr.emanation.util.gui.treeComboBox;
    import javax.swing.*;
    import javax.swing.tree.TreeModel;
    import java.awt.*;
    import java.awt.event.FocusListener;
    public class TreeComboBox<TNode> extends JPanel
      private TNode theSelectedNode;
      private ArrowMenuBar theMenuBar;
      private JTextField theTextField;
      private int theCompactWidth;
      public TreeComboBox(TreeModel aTreeModel)
        new BorderLayout(0, 0);
        theTextField = new JTextField();
        theTextField.setEditable(true);
        theTextField.setBackground(Color.white);
        setFocusable(false);
        add(theTextField, BorderLayout.CENTER);
        MyArrowMenuInvoker anInvoker = new MyArrowMenuInvoker();
        theMenuBar = new ArrowMenuBar<TNode>(anInvoker, aTreeModel);
        JPanel menuBarPanel = new JPanel(new BorderLayout(0, 0));
        menuBarPanel.add(theMenuBar, BorderLayout.CENTER);
        add(menuBarPanel, BorderLayout.EAST);
      public TNode getNode()
        return (TNode) theSelectedNode;
      public void setSelectedNode(TNode aNode)
        theSelectedNode = aNode;
        theTextField.setText(aNode.toString());
      public TreeModel getModel()
        return theMenuBar.getModel();
      public JComponent[] getFocusableComponents()
        return new JComponent[]{theTextField, theMenuBar};
      //-- JComponent overriden methods
      public Dimension getMinimumSize()
        final Dimension minSize = super.getMinimumSize();
        minSize.width = theCompactWidth;
        return minSize;
      public void setCompactWidth(final int aCompactWidth)
        theCompactWidth = aCompactWidth;
      public void setPreferredSize(Dimension aSize)
        super.setPreferredSize(aSize);
        if (theTextField != null)
          theTextField.setPreferredSize(aSize);
      public void setForeground(Color fg)
        super.setForeground(fg);
        if (theMenuBar != null)
          for (int index = 0; index < theMenuBar.getComponentCount(); index++)
            theMenuBar.getComponents()[index].setForeground(fg);
        if (theTextField != null)
          theTextField.setForeground(fg);
      public void setBackground(Color bg)
        super.setBackground(bg);
        if (theMenuBar != null)
          for (int index = 0; index < theMenuBar.getComponentCount(); index++)
            theMenuBar.getComponents()[index].setBackground(bg);
        if (theTextField != null)
          theTextField.setBackground(bg);
      public void setFont(Font aFont)
        super.setFont(aFont);
        if (theMenuBar != null)
          theMenuBar.setFont(aFont);
        if (theTextField != null)
          theTextField.setFont(aFont);
      public void setToolTipText(String aToolTip)
        super.setToolTipText(aToolTip);
        if (theTextField != null)
          theTextField.setToolTipText(aToolTip);
      public void setEnabled(final boolean bEnabled)
        super.setEnabled(bEnabled);
        if (theTextField != null) return;
          theTextField.setEnabled(bEnabled);
          if (bEnabled)
            theTextField.setBackground(Color.WHITE);
          else
            JLabel tmpLabel = new JLabel();
            tmpLabel.setEnabled(false);
            theTextField.setBackground(tmpLabel.getBackground());
        if (theMenuBar != null)
          theMenuBar.setEnabled(bEnabled);
      public synchronized void addFocusListener(FocusListener l)
        super.addFocusListener(l);
        if (theMenuBar != null)
          theMenuBar.addFocusListener(l);
        if (theTextField != null)
          theTextField.addFocusListener(l);
      public synchronized void removeFocusListener(FocusListener l)
        super.removeFocusListener(l);
        if (theMenuBar != null)
          theMenuBar.removeFocusListener(l);
        if (theTextField != null)
          theTextField.removeFocusListener(l);
      public boolean isFocusOwner()
        if (theMenuBar == null || theTextField == null) return super.isFocusOwner();
        boolean bM = (theMenuBar == null) && theMenuBar.isFocusOwner();
        boolean bT = (theTextField == null) && theTextField.isFocusOwner();
        return (bM || bT);
      //-- inner classes
      private class MyArrowMenuInvoker implements IArrowMenuInvoker<TNode>
        public Component getComponent()
          return theTextField;
        public void setNode(TNode aNode)
          setSelectedNode(aNode);
    }the ArrowMenuBar :
    package fr.emanation.util.gui.treeComboBox;
    import javax.swing.*;
    import javax.swing.border.EtchedBorder;
    import javax.swing.plaf.UIResource;
    import javax.swing.tree.TreeModel;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class ArrowMenuBar<TNode> extends JMenuBar
      private NodeMenu theMenu;
      private TreeModel theModel;
      private IArrowMenuInvoker<TNode> theInvoker;
      public ArrowMenuBar(IArrowMenuInvoker<TNode> anInvoker, TreeModel aModel)
        super();
        theModel = aModel;
        theInvoker = anInvoker;
        TNode root = (TNode) theModel.getRoot();
        if (!theModel.isLeaf(root))
          theMenu = new ArrowMenu(root);
          theMenu.setBackground(new JPanel().getBackground());
        MenuItemListener listener = new MenuItemListener();
        setListener(theMenu, listener);
        add(theMenu);
      private void setListener(JMenuItem anItem, ActionListener aListener)
        if (anItem instanceof JMenu)
          JMenu menu = (JMenu) anItem;
          int n = menu.getItemCount();
          for (int index = 0; index < n; index++)
            setListener(menu.getItem(index), aListener);
        else if (anItem != null)
          // null means separator
          anItem.addActionListener(aListener);
      public TreeModel getModel()
        return theModel;
      public Action getLaunchArrowMenuBarAction()
        return new LaunchArrowMenuBarAction();
      //-- overrides JComponent
      public Dimension getPreferredSize()
        return theMenu.getPreferredSize();
      private Dimension getItemSize(JMenu aMenu)
        Dimension dimension = new Dimension(0, 0);
        int n = aMenu.getItemCount();
        for (int index = 0; index < n; index++)
          Dimension itemD;
          JMenuItem item = aMenu.getItem(index);
          if (item instanceof JMenu)
            itemD = getItemSize((JMenu) item);
          else if (item != null)
            itemD = item.getPreferredSize();
          else
            itemD = new Dimension(0, 0); // separator
          dimension.width = Math.max(dimension.width, itemD.width);
          dimension.height = Math.max(dimension.height, itemD.height);
        return dimension;
      //--- inner classes
      private class LaunchArrowMenuBarAction extends AbstractAction
        public LaunchArrowMenuBarAction()
          super("...");
        public void actionPerformed(ActionEvent e)
          if (e.getActionCommand() == "Launch")
            System.out.println("Launch");
      private class MenuItemListener implements ActionListener
        public void actionPerformed(ActionEvent anEvent)
          NodeMenuItem item = (NodeMenuItem) anEvent.getSource();
          theInvoker.setNode(item.getNode());
          theMenu.requestFocus();
      private class NodeMenu extends JMenu
        private TNode theNode;
        public NodeMenu(TNode aNode)
          this(aNode.toString(), aNode);
        public NodeMenu(String theText, TNode aNode)
          super(theText);
          theNode = aNode;
          add(new NodeMenuItem("[.]", aNode));
          for (int index = 0; index < theModel.getChildCount(aNode); index++)
            TNode childNode = (TNode) theModel.getChild(aNode, index);
            if (theModel.isLeaf(childNode))
              add(new NodeMenuItem(childNode));
            else
              add(new NodeMenu(childNode));
        public void setNode(TNode aNode)
          theNode = aNode;
        public TNode getNode()
          return theNode;
      private class NodeMenuItem extends JMenuItem
        private TNode theNode;
        public NodeMenuItem(TNode aNode)
          super(aNode.toString());
          theNode = aNode;
        public NodeMenuItem(String aText, TNode aNode)
          super(aText);
          theNode = aNode;
        public TNode getNode()
          return theNode;
      private class ArrowMenu extends NodeMenu
    //    private ArrowIcon theIconRenderer;
        private Color shadow = UIManager.getColor("controlShadow");
        private Color darkShadow = UIManager.getColor("controlDkShadow");
        private Color highlight = UIManager.getColor("controlLtHighlight");
        public ArrowMenu(TNode aNode)
          super("", aNode);
    //      theIconRenderer = new ArrowIcon(SwingConstants.SOUTH, true);
          setBorder(new EtchedBorder());
    //      setIcon(new BlankIcon(null, 11));
          setHorizontalTextPosition(JButton.LEFT);
          setFocusPainted(true);
        public void paint(Graphics g)
          Color origColor;
          boolean isEnabled;
          int w, h, size;
          w = getSize().width;
          h = getSize().height;
          origColor = g.getColor();
          isEnabled = isEnabled();
          g.setColor(getBackground());
          g.fillRect(1, 1, w - 2, h - 2);
          /// Draw the proper Border
          if (getBorder() != null && !(getBorder() instanceof UIResource))
            paintBorder(g);
          else
            // Using the background color set above
            g.drawLine(0, 0, 0, h - 1);
            g.drawLine(1, 0, w - 2, 0);
            g.setColor(highlight);  // inner 3D border
            g.drawLine(1, 1, 1, h - 3);
            g.drawLine(2, 1, w - 3, 1);
            g.setColor(shadow);     // inner 3D border
            g.drawLine(1, h - 2, w - 2, h - 2);
            g.drawLine(w - 2, 1, w - 2, h - 3);
            g.setColor(darkShadow);   // black drop shadow  __|
            g.drawLine(0, h - 1, w - 1, h - 1);
            g.drawLine(w - 1, h - 1, w - 1, 0);
          // If there's no room to draw arrow, bail
          if (h < 5 || w < 5)
            g.setColor(origColor);
            return;
          // Draw the arrow
          size = Math.min((h - 4) / 3, (w - 4) / 3);
          size = Math.max(size, 2);
          paintTriangle(g, (w - size) / 2, (h - size) / 2,
                  size, SwingConstants.SOUTH, isEnabled);
          // Reset the Graphics back to it's original settings
          g.setColor(origColor);
        public Dimension getPreferredSize()
          return new Dimension(16, 16);
        public Dimension getMinimumSize()
          return new Dimension(5, 5);
        public Dimension getMaximumSize()
          return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
        public void paintTriangle(Graphics g, int x, int y, int size,
                      int direction, boolean isEnabled)
          Color oldColor = g.getColor();
          int mid, i, j;
          j = 0;
          size = Math.max(size, 2);
          mid = (size / 2) - 1;
          g.translate(x, y);
          if (isEnabled)
            g.setColor(darkShadow);
          else
            g.setColor(shadow);
          switch (direction)
            case NORTH:
              for (i = 0; i < size; i++)
                g.drawLine(mid - i, i, mid + i, i);
              if (!isEnabled)
                g.setColor(highlight);
                g.drawLine(mid - i + 2, i, mid + i, i);
              break;
            case SOUTH:
              if (!isEnabled)
                g.translate(1, 1);
                g.setColor(highlight);
                for (i = size - 1; i >= 0; i--)
                  g.drawLine(mid - i, j, mid + i, j);
                  j++;
                g.translate(-1, -1);
                g.setColor(shadow);
              j = 0;
              for (i = size - 1; i >= 0; i--)
                g.drawLine(mid - i, j, mid + i, j);
                j++;
              break;
            case WEST:
              for (i = 0; i < size; i++)
                g.drawLine(i, mid - i, i, mid + i);
              if (!isEnabled)
                g.setColor(highlight);
                g.drawLine(i, mid - i + 2, i, mid + i);
              break;
            case EAST:
              if (!isEnabled)
                g.translate(1, 1);
                g.setColor(highlight);
                for (i = size - 1; i >= 0; i--)
                  g.drawLine(j, mid - i, j, mid + i);
                  j++;
                g.translate(-1, -1);
                g.setColor(shadow);
              j = 0;
              for (i = size - 1; i >= 0; i--)
                g.drawLine(j, mid - i, j, mid + i);
                j++;
              break;
          g.translate(-x, -y);
          g.setColor(oldColor);
    }

    Darkness and blindness are away !
    First line of the constructor :
    public TreeComboBox(TreeModel aTreeModel)
        new BorderLayout(0, 0);
    }I will reborn as a pumpkin !

  • How do I add a JMenuBar to a GridBagLayout

    Hi All,
    I've got a GridBagLayout on a JFrame and want to add a menu bar to row 0. How do I do that? What I've tried to do is create a JPanel, add the JPanel (menuPanel) to my GridBagLayout in column 0, row 0, width 4 and height of 1. Then add the JMenuBar (bar) to menuPanel. I get the JPanel on the screen because it takes up space, but now File menu.
    Please help.
    // create JPanel
    menuPanel = new JPanel();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.weightx = 0;
    constraints.weighty = 0;     
    addComponent(menuPanel, 0, 0, 4, 1);  // object, row, column, width, height          
    // set up  menu bar
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');
    newSearch = new JMenuItem("New Search");
    newSearch.setMnemonic('N');
    fileMenu.add(newSearch);
    exit = new JMenuItem("Exit");
    exit.setMnemonic('x');
    fileMenu.add(exit);
    bar = new JMenuBar();
    setJMenuBar(bar);
    menuPanel.add(bar);Thanks,
    Karl

    You don't add a MenuBar to the layout, you add directly to the frame using
    setJMenuBar(myMenuBar);HTH,
    Radish21

  • Changing the highlight color of a JMenuBar

    I'm trying to change the highlight color of a JMenuBar. I�m using the METAL lookandfeel which by default highlights with a purple color. I would also like to know how its done on a JTable, I assume it is quite similar.

    UIDefaults uid = new com.sun.java.swing.plaf.windows.WindowsLookAndFeel().getDefaults();</code>
    Note that this will print the default properties for the Windows Look&Feel, not the Metal Look&Feel.                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to do a dropdownlist in java Swing much like a JMenuBar items

    Hi sir,
    Here i want to know how to do a dropdownlist in java swing much like a JMenuBar items falling from the top to bottom .Here i should not use JMenuBar in any way.
    So that i need to do the dropdownlist by using other feature only.Since i am new to this.I need ur help urgently.Pls do provide the code for this so that i will be so gratefull to u.It is very Urgent.Since i have involved in a project which involves this feature.Pls. do provide the answer as well as the code quickly as possible.
    Thanx
    m.ananthu

    use JComboBox
    http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html

  • Adding a JMenuBar

    I am trying to create a simple JMenuBar in the application enlosed below.
    I keep getting the following error when ever i compile
    "Screen.java": Error #: 300 : method setJMenuBar(javax.swing.JMenuBar) not found in class random.Screen at line 67, column 9". Can some one please enlighten me.
    Class Screen is the main class. This is where the GUI is set up. it calls class Canvas which in turn calls class Nodes. GUI draws a bus network which holds a minimum of two nodes and a maximum of 10. The node number is under user control. The aim is to allow a user to select the configure menu to update the number of nodes on display in the GUI.
    package random;
    import javax.swing.JPanel;
    import javax.swing.border.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    * <p>Title: Simulation of the random load balancing algorithm </p
    public class Screen extends JPanel {
    static int screenWidth = (int)(Toolkit.getDefaultToolkit().getScreenSize().width*0.85);
    static int screenHeight = (int)(Toolkit.getDefaultToolkit().getScreenSize().height*0.95);
    private Container container;
    JButton add;
    Canvas canvas;
    JMenuBar menubar;
    JMenu configure;
    Thread animatorThread;
    RandomApplet RApplet;
    public Screen(RandomApplet RApplet){
         this.RApplet = RApplet;
         makeGui();
    public Screen(){
         makeGui();
    public void makeGui(){
    addMenu();
    GridBagLayout gridbag = new GridBagLayout();
         setLayout(gridbag);
         GridBagConstraints c = new GridBagConstraints();
         c.insets = new Insets(1,1,1,1);
         //add drawing canvas
         canvas = new Canvas();
         c.gridx = 0;
         c.gridy = 0;
         c.weightx=1.0;
         c.weighty=1.0;
         c.fill=GridBagConstraints.BOTH;
         gridbag.setConstraints(canvas, c);
         add(canvas);
         c.gridx=0;
         c.gridy=1;
         gridbag.setConstraints(canvas,c);
         add(add=addButton("Add"));
    private void addMenu(){
         menubar = new JMenuBar();
    setJMenuBar(menubar);
         configure = new JMenu("Configure");
         configure.add(new JMenuItem("add"));
         menubar.add(configure);
    private JButton addButton(String name) {
              JButton b = new JButton(name);
              ButtonHandler handler=new ButtonHandler();
              b.addActionListener(handler);
              b.setFont(new Font("Dialog", Font.BOLD, 12));
              b.setMargin(new Insets(0,0,0,0));
              return b;
    class ButtonHandler implements ActionListener{
         public void actionPerformed(ActionEvent e){
              if (e.getSource()== add){
                   if (canvas.addNode()){
                   repaint();
    public void run(){
         Thread thisThread = Thread.currentThread();
         while (animatorThread == thisThread){
              try {
              Thread.sleep(100);}
              catch (Exception e){
              break;}
    public static void main(String[] args) {
    JFrame f=new JFrame("Random");
         f.setSize(screenWidth, screenHeight);
         f.getContentPane().add(new Screen());
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    //class Canvas
    package random;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.util.*;
    import java.awt.Graphics;
    import java.awt.image.*;
    import javax.swing.*;
    public class Canvas extends JPanel{
    int width, height;
    Dimension size;
    Image canvasImage = null;
    Graphics canvasGraphics;
    Image networkMem;
    int NodeWidth, NodeHeight;
    Nodes[] n;
    int numberOfNodes;
    int x1, y1,x2,y2;
    public Canvas(){
    // canvas a perfect square
         height = 400;
         width = 400;
         size=new Dimension(width,height);
         setBackground(Color.white);
         networkMem=Toolkit.getDefaultToolkit().createImage("images/computer6.gif");
         NodeWidth = networkMem.getWidth(this);
    NodeHeight = networkMem.getHeight(this);
         //network must hold a minimum of 2 nodes
         n = new Nodes[11];
         n[0] = new Nodes(0,this);
         n[1] = new Nodes(1,this);
         numberOfNodes = 2;
    public void paintComponent(Graphics g)
    super.paintComponent(g);
         //create canvas image
         if (canvasImage==null){
              canvasImage=createImage(getSize().width,getSize().height);
              canvasGraphics = canvasImage.getGraphics();
         //adjust above to fit canvas window
    if ((canvasImage.getWidth(this) != getSize().width) || (canvasImage.getHeight(this) != getSize().height)){
         canvasImage = createImage(getSize().width,getSize().height);
              if (canvasImage!=null){
              canvasGraphics.dispose();
              canvasGraphics = canvasImage.getGraphics();
         //create a rectangular border outlining canvas area
         canvasGraphics.drawRect(0,0,getSize().width-1,getSize().height-1);
         x1=width-100;
         y1=height-100;
         x2=width-300;
         y2=width-100;
    canvasGraphics.drawLine(x1,y1,x2,y2);
    //canvasGraphics.drawLine(width-300,height-100,width-300,height-50);
         //canvasGraphics.drawLine(width-100,height-100,width-100,height-50);
         for(int i=0; i<numberOfNodes; i++){
         n.drawNode(canvasGraphics);
         g.drawImage(canvasImage, 0, 0, this);
         super.paintComponent(canvasGraphics);
    public int getNumberOfNodes()
         return numberOfNodes;
    public boolean addNode(){
         //add a node to network
         if (numberOfNodes < 10) {
         n[numberOfNodes]= new Nodes(numberOfNodes, this);
         numberOfNodes++;
         repaint();
         return true;
         return false;
    public boolean removeNode(){
         boolean removed = false;
         if (numberOfNodes >2) {
         numberOfNodes=numberOfNodes - 1;
         removed = true;
         else{
         removed = false;
         repaint();
         return removed;
    //class Nodes
    package random;
    import javax.swing.JPanel;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Graphics;
    public class Nodes{
    int nodeNumber;
    Canvas canvas;
    public Nodes(int number, Canvas c) {
    //each node has a number id
         nodeNumber=number;
         canvas=c;
    public void drawNode(Graphics g){
         if (nodeNumber%2 == 0){
    canvas.x1=canvas.x1-10;
         System.out.println(""+canvas.x1);
         g.drawLine(canvas.x1,canvas.y1,canvas.x1,canvas.y2-50);
         g.drawImage(canvas.networkMem,canvas.x1,canvas.y1-80,canvas);
         else{
         canvas.x1=canvas.x1-30;
         System.out.println(""+canvas.x1);
         g.drawLine(canvas.x1,canvas.y1,canvas.x1,canvas.y2+50);
         g.drawImage(canvas.networkMem,canvas.x1,canvas.y2+50,canvas);

    Thanks for the reply but i figured it out in the end.
    I can make use of the following commands
    setMenuBar(MenuBar mb)
    getMenuBar()
    to set the main frame menubar to that declared in addmenu() .
    if I add the following code to main() in the screen class the menu appears
    Screen screen = new Screen(null);
    f.setJMenuBar(screen.getMenuBar());

  • Remove JMenuBar area from JFrame

    hi there,
    I have an application with a JFrame undecorated and it has a JMenuBar.
    I add a JPanel in the JFrame, and I change the JMenuBar to JPanel,
    panel.add(Menu);
    setJMenuBar(Menu);the JMenuBar it visible into the JPanel
    but, in the JFrame I have the JMenuBar area blank at the TOP.
    How do I do to remove the JMenuBar area from JFrame.
    thanks.

    I am trying make a JFrame undecorated,
    I want create my own title bar, with my title message,
    my icons, etc.
    I found an image from a SAP Application frame
    here
    http://help.sap.com/saphelp_46c/helpdata/en/73/69ee7d55bb11d189680000e829fbbd/Image84.gif
    I want make a frame like this
    can you help me?
    thanks

  • JMenuBar setFont problem.

    Hi, I'm trying to change font for my menu bar, but it takes no effect.
    Actually, my purpose is to set normal(not bold) font for menu.
    Code:
    JMenuBar menu=new JMenuBar();
    menu.setFont(new Font("Serif", Font.PLAIN, 12));
    //Adding menu items to the menubar.
    I have tried to change font of menu items but it took no effect either.
    Can anybody help me?
    Thanks.

    Setting the font on the individual menu items should hve worked unless you don't have the specified font installed on your sytem.
    However, if you want to change the font globally, use
    UIManager.put(MenuItem.font, yourFont);
    UIManager.put(Menu.font, yourFont);
    This will change the fonts for all menus and menu items to whatever font you specify.
    Sai Pullabhotla

  • Vertical JMenuBar width problem

    When I flip my JMenuBar vertical instead of horizontal by doing
    BoxLayout bl = new BoxLayout(this,BoxLayout.PAGE_AXIS);
    setLayout(bl);
    in a constructor for a class that extends JMenuBar. When I do that only 3 characters of each JMenu in the menubar are show. The button size is also ruffly half the width of the actual window. I am doing pack() on the JFrame that houses the JMenuBar. At the moment nothing else is in the JFrame. If I do not set the layout of my JMenuBar then I get a regular horizontal one and width is fine. It's only when I want the Menus vertical instead of horizontal. Been experimenting with all kinds of stuff and no go so far. Looking for suggestions?

    this produces a menubar, with the screen items vertical, but it takes up too much real estate
    import java.awt.*;
    import javax.swing.*;
    class Testing extends JFrame
      public Testing()
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(400,200);
        JMenu file = new JMenu("File");
        JMenuItem newItem = new JMenuItem("New");
        JMenuItem open = new JMenuItem("Open");
        JMenuItem preview = new JMenuItem("Preview");
        JMenuItem print = new JMenuItem("Print");
        JMenuItem exit = new JMenuItem("Exit");
        file.add(newItem);
        file.add(open);
        file.add(new JSeparator());
        file.add(preview);
        file.add(print);
        file.add(new JSeparator());
        file.add(exit);
        JMenu options = new JMenu("Options");
        JMenuItem font = new JMenuItem("Font");
        JMenuItem color = new JMenuItem("Color");
        options.add(font);
        options.add(color);
        JMenuBar menuBar = new JMenuBar();
        menuBar.setLayout(new GridLayout(0,1));
        menuBar.add(file);
        menuBar.add(options);
        setJMenuBar(menuBar);
        getContentPane().add(new JTextArea(5,20));
        pack();
      public static void main(String[] args){new Testing().setVisible(true);}
    }

  • Set background color of JMenuBar

    Hello,
    I'm trying to change the background color of a JMenuBar but i can't. I tried jMenuBar.setBackground(Color color), but it doesn't do anything. I'm using the Nimbus look and feel, so i tried:
    UIDefaults defaults = new UIDefaults();
    defaults.put("MenuBar.background", new Color(0,0,153));
    menuBar.putClientProperty("Nimbus.Overrides", defaults);
    menuBar.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
    But it still doesn't change anything. If i change "MenuBar.background" with "MenuBar[Enabled].backgroundPainter" it makes the menuBar transparent, so i can set the background color of the parent (a JFrame) and it will look like the menuBar is colored. But, this way, i can't change the foreground color, so it's still not helpful.
    So, how can i change the background color of a JMenuBar?
    Thank you.

    I am not sure if this solution works for you, but you can bind empty data to dataprovider.

  • Displaying icons in JMenuBar

    Is it possible to display icons instead of text in the JMenuBar? I know you can make menu items with icons/text, but the menu itself I'd like to use an icon.
    The only possible workaround I can think of is to use a toolbar (with icon buttons) and attach action events to load a JPopupMenu... however, I have to add code to figure out the dynamic positioning and to hide/show the menus appropriately.
    Is there a simplier way?

    I do not remember making changes to the CLASSPATH, either way it would be good that you double check your steps against the metalink note 232413.1 - How to Deploy Iconic Images via a JAR File in Forms 9i/10g.
    As a reference, you can also take a look at this thread icons not displaying in oracle forms.

  • JMenuBar is hidden by Canvas

    Hi,
    I have a class that extends JFrame, this has a JMenuBar set to it (set with setJMenuBar method of JFrame) and is displayed fine until i try to add a java.awt.Canvas object to the center of the JFrame(using a BorderLayout) . When this canvas has been added then you can still click on the JMenu names in the JMenuBar but the JMenu itself is no hidden behind the canvas. Here is some of the code
    Class that extends JFrame's constructor
    //call the super constructor
              super("My Image Viewer");
              //get the content pane
              c = getContentPane();
              //set the layout of the frame
              c.setLayout(new BorderLayout());
              //create a new MenuHandler
              menuHandler = new MenuHandler();
              //create a new JFileChooser
              fileChooser = new JFileChooser("C:/Documents and Settings/"
                        + "Irene Stephanie/My Documents");
              //create a new file filter
              filter = new ImgFilter();
              //assign the file filter
              fileChooser.setFileFilter(filter);
              //create a new window canvas
              canvas = new WindowCanvas();
              add("Center", canvas);
              //set up the menus
              initMenu();
              //set the JMenuBar
              setJMenuBar(menuBar);
              //Set the window size
              setSize(640, 480);
              //make the window visible
              setVisible(true);Class that extends Canvas
    public class WindowCanvas extends Canvas{
         public WindowCanvas(){
              super();
         public void paint(Graphics g){
         }Please not that it works if you add the JMenuBar to the South position, and that you can draw in the canvas, but you cannot see the JMenu's.
    Any help will be greatly apreciated
    Andy

    A Canvas is a heavyweight component. By default, Swing menus are lightweight components. You need to call
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);before creating your menus.

Maybe you are looking for

  • "Internal error (-5002) occurred" On Pick List Update Picked Quantity

    Hi Experts, I have an SDK program that simply tries to update the Picked Quantity in SAP. Previously it has been working fine, until I try to use the same logic for items managed by Batch or Serial Number. In this scenario, I have created a PickList

  • Downloading Report output to a Local File

    Hi, I want to download the my ALV Report output to a local file. I am following the menu options from ALV output:   LIST --> Export --> Local File --> Unconverted Format  --> I entered a text file name on the desktop. The data is being downloaded to

  • Show clients ASA 5505

    Hi, I have a question about our Cisco ASA 5505. It is installed at a customers network and it has a license for 50 clients. The problem is that they were yesterday with more than the 50 devices and wanted to go on the Internet. This caused that some

  • Cannot get ringtones to work on incoming calls

    I cannot get ringtons on incoming calls to work on my new IPhone. Have I overlooked a mute button, because I have no sound except when I try different ringtones?

  • T-code to send PO to XI

    Hello, Is there a t-code like bd10 and others, to send Purchase Orders, Purchase Requistion to SAP XI system. i know some standarda IDoc are provided by SAP ORDERS01 and PREQCR101 regards Abhishek