change popup height - swing

I have a Java swing popupmenu with a couple of menuItems.
Is there any way to increase the size of the popup keeping the same number of menuItems? For example, add 10px before the 1st menuItem and 10px after the last menuItem.
How can I do this? Can someone give me an hint?
Thanks

This is pretty simple. Since JPopupMenu is a Container the following code will produce the effect you desire
JPanel p1 = new JPanel();
p1.setPreferredSize( new Dimension(100,10));
JPanel p2 = new JPanel();
p2.setPreferredSize( new Dimension(100,10));
menu.add(p1);
menu.add(new JMenuItem("Item 1"));
menu.add(new JMenuItem("Item 2"));
menu.add(new JMenuItem("Item 3"));
menu.add(p2);

Related

libGDX button layout with no spacing

I'm using a table to layout my menu buttons, but the spacing between buttons is big enough to drive a car through. What can I do to gain more control over the apparent cell padding? I've searched StackOverflow posts, read through the API, typed '.' after table and actor to browse the autocompletes, manually adjusted values in the skin file, and looked at other people's projects on Github. Some of the methods I've tried include sizeBy(), scaleBy(), pad(), setFillParent(), space(), fillY(), and grow()
A helpful answer would describe how to get buttons to stack on top of each other, touching.
Gdx.input.setInputProcessor(stage);
// Create a table that fills the screen. Everything else will go inside this table.
Table table = new Table();
table.setFillParent(true);
table.setDebug(true);
stage.addActor(table);
table.setBackground(new TiledDrawable(background));
//create buttons
TextButton newGame = new TextButton("New Game", skin);
TextButton preferences = new TextButton("Preferences", skin);
TextButton exit = new TextButton("Exit", skin);
TextButtonStyle tbs = new TextButtonStyle(newGame.getStyle());
NinePatch np = new NinePatch(skin.getPatch("button"));
np.setColor(np.getColor().sub(0f, 0f, 0f, .4f));
tbs.up = new NinePatchDrawable(np);
newGame.setStyle(tbs);
preferences.setStyle(tbs);
exit.setStyle(tbs);
table.bottom().right().padBottom(40f).padRight(40f);
//add buttons to table
table.add(newGame).bottom().right().fillX().uniformX();
table.row();//.pad(1, 0, 1, 0);
table.add(preferences).bottom().right().fillX().uniformX();
table.row();
table.add(exit).bottom().right().fillX().uniformX();
It was my skin. I used the sample "Rusty Robot UI", but it doesn't crop it's button images tightly. Tried another one and saw the stacking I was looking for.

How to add editable JComboBox and Button on same Frame..?

I have created a JTable with 5 columns in it .I want the sixth column to have all cells as JComboBox from where an user can select his choice and the change will get appended in databasefor which i need a button on whose action i can fire the query to my database. So please please let me know how to add JComboBox and ButTon on JFrame...?? I am very new to Swings so do let me know how to get this donea detailed explanation about the same will be very thankful...!!! Thanks in advance..!!!
How to add JComboBox and JButton on a JFrame is rather trivial. Linking this question to the first part of your question with the table is something I did not manage. But for the part I did understand, you can have something like
JFrame frame = new JFrame( "TestFrame" );
JPanel contents = new JPanel( new BorderLayout() );
JComboBox comboBox = createComboBox();
contents.add( comboBox, BorderLayout.CENTER );
JButton button = createButton();
contents.add( button, BorderLayout.EASTH );
frame.add( contents );
frame.pack();
frame.setVisible( true );
The code illustrates how to add a combobox and a button. Note that I opted for the very simple BorderLayout. Other layouts are certainly possible, but it all depends on the requirements of your layout.

JMenuItems don't show in JDialog

This is probably a dumb question, but I just can't see it! I have Swing app that uses a popup menu. It works fine, but I want to make the menu persistent (i.e. until I close it). I have basically changed the JPopupMenu to JDialog, and I am getting the JDialog panel, but the menu items are invisible! It's probably something very obvious, so I'll probably be embarrassed! Here is part of the code:
JDialog buildNewItemMenu(DrawFBP base) {
JDialog jd = new JDialog();
jd.setSize(200, 300);
JMenuItem menuItem = null;
JLabel label2 = new JLabel();
label2.setForeground(Color.BLUE);
JMenu menu = new JMenu();
jd.add(menu);
jd.setVisible(true);
menu.setVisible(true);
menu.add(label2);
menu.addSeparator();
menuItem = new JMenuItem("Component");
menuItem.addActionListener(base);
menu.add(menuItem);
....
menu.addSeparator();
menuItem = new JMenuItem("Enclosure");
menuItem.addActionListener(base);
menu.add(menuItem);
return jd;
I think I will close this - as I said in the comment, changing the JMenu to a JPanel and adding
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
fixed the problem, but I plan to tackle the problem a different way. Thanks anyway!

adding a jpanel to a jframe

Is it possible to add a JPanel to a JFrame? How can it be done?
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Sure it's possible - you can do it like this:
frame.setContentPane(panel);
This panel will be the container in which you'll put all the components of your UI(except the menubar).
yes, sure, same way as you would add any component to any container:
frame.add(panel)

How to programmatically maximize JDesktopPane

I am using JDesktopPane, and I want it to be maximized when I load it. How can I do it?
Set the encompassing container's layout to BorderLayout and add the JDesktopPane to the CENTER. Then set the window to maximized.
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
JDesktopPane jDesktopPane = new JDesktopPane();
f.add(jDesktopPane, BorderLayout.CENTER);
...
f.setExtendedState(MAXIMIZED_BOTH);