How to place a JPanel in a JMenuItem on the left or right (referring to the position relative to the JMenuItem text), and not in the center (directly over the text), how does this happen automatically?

I explain:

There is a regular JMenuItem created like this:

 new JMenuItem("menu item text") 

How to put a JPanel left or right, and not in the center right on top of this text?

  • one
    I read the question a couple of times - I didn't understand it better , some sort of words)) - Gorets

2 answers 2

To place an item is the method SETBOUNDS(); whose parameter is 4 integers: the first two X, Y are the coordinates of the upper left point of the element, the other two parameters are the height and width of the element.

This method works correctly when the form is zeroed out.

  Frame.GetContentpane (setLayout (null)); 

To the JMenuItem account, as far as I know, only text is placed in it, and no other elements. To place this text on the left or right, you just need to use methods to format the text.

  • In JMenuItem not only the text is placed, it turned out that the necessary components were placed inside the JMenuItem (JLabel with text and several JPanel). - alnasfire
  • so the problem is solved? sample solution possible? - Vitaliy
  • protected JMenuItem createColorPanel (Vector colors, Action action) {JPanel jColorButton; int limit; JMenuItem panel = new JMenuItem (action); panel.setLayout (new GridBagLayout ()); JLabel jLabel = new JLabel (i18n.getMessage ("colorsByCondition")); panel.add (jLabel, new GridBagConstraints (0,0,10,1,1,0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets (0,0,0,0), 0,0,0)); if (colors! = null &&! colors.isEmpty ()) {if (colors.size ()> 5) limit = 5; else limit = colors.size (); - alnasfire
  • for (int i = 0; i <limit; i ++) {jColorButton = new JPanel (); jColorButton.setPreferredSize (new Dimension (10, 10)); jColorButton.setMaximumSize (new Dimension (10, 10)); jColorButton.setBackground ((((ConditionalColorAttribute) colors.get (i)). getColor ()); panel.add (jColorButton, new GridBagConstraints (i + 10,0,1,1,0,0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (0,0,0,0), 0,0)); }} panel.set PreferredSize (new Dimension (150, 20)); return panel; } - alnasfire

in the two previous comments the answer to my question. I am sorry that in this form.