To get the button to look like a JMenu just add a rollover effect and remove the border of the button (See code below for example)
Required imports
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Dimension;
import java.awt.Color;
Code
JButton tstButton = new JButton(); //Button
tstButton.setText("Test"); //Button Text
tstButton.setOpaque(false); //These remove the button filling and border
tstButton.setContentAreaFilled(false);
tstButton.setBorder(null);
tstButton.setFocusable(false);
tstButton.setRolloverEnabled(true); //Allows the button to detect when mouse is over it
tstButton.getModel().addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent e)
{
ButtonModel model = (ButtonModel) e.getSource();
if(model.isRollover())
{
tstButton.setBackground(Color.RED); //Changes the colour of the button
tstButton.setOpaque(true);
}
else
{
tstButton.setBackground(null);
tstButton.setOpaque(false);
}
}
});
Dimension dBt = new Dimension(75,25); //Sets the size of the button in the JMenuBar
tstButton.setMinimumSize(dBt);
tstButton.setPreferredSize(dBt);
tstButton.setMaximumSize(dBt);
tstButton.setMnemonic('T'); //Allows you to press Alt+T on your keyboard to press the button
tstButton.addActionListener(new ActionListener() //Adds action listener so it can do something
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed");
}
});
menuBar.add(tstButton); //Adds the button to the JMenuBar
Edit
There is a much improved way to do this
JButton tstButton = new JButton();
tstButton.setVisible(false);
tstButton.addActionListener(new CustomActionListener());
menuBar.add(tstButton);
JMenu menuButton = new JMenu();
addHotKey(menuButton, "shift C", 'm', "Menu Button","pressed");
menuButton.addMouseListener(new CustomMouseListener());
menuButton.addMenuKeyListener(new CustomKeyListener());
menuBar.add(menuButton);
public void addHotKey(JMenu J, String s, char c, String S, String key)
{
Action buttonAction = new AbstractAction(S)
{
@Override
public void actionPerformed(ActionEvent evt)
{
clcikComponent(tstButton);
}
};
J.setAction(buttonAction);
buttonAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar(c));
J.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(s), key);
J.getActionMap().put(key, buttonAction);
}
class CustomMouseListener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
clcikComponent(m_Invisible);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class CustomKeyListener implements MenuKeyListener
{
@Override
public void menuKeyTyped(MenuKeyEvent e)
{
char c = e.getKeyChar();
if(c==KeyEvent.VK_ENTER)
{
if(m_code.isSelected())
{
clcikComponent(m_Invisible);
}
}
}
@Override
public void menuKeyPressed(MenuKeyEvent e){}
@Override
public void menuKeyReleased(MenuKeyEvent e){}
}
public void clcikComponent(JButton comp)
{
comp.doClick();
}
class CustomActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
//Makes Button Perform Action
}
}
JMenuItem
s reserve space for icons) remained. – Eldrid