How to programmatically maximize JDesktopPane - swing

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);

Related

Minimize a resizable JDialog

I'm working on an swing application with a main window (which extends JFrame) from which several child windows can be opened (more than 1 contemporarily).
These windows are all non-modal and resizable.
So far, I implemented these 'child' windows as a JFrame. However, I get a new icon on my Windows taskbar for each opened Window.
I therefore tried to implement these windows as a JDialog with type ModalityType.MODELESS.
Looks OK except that a JDialog has no minimize button.
Is there a way to resolve this?
I.e., I need to create non-modal and resizable child windows that can be minimized.
JInternalFrame is not an option since the main frame is not just a container with a JDesktopPane and child windows should be able to cross the borders of the main window.
For those interested:
Child windows register and unregister themselves on the main window when being opened/closed.
The main window has a menu with a 'Windows' item and child windows are added/removed from that menu upon registration/unregistration.
The user can switch between the various windows by selecting an item within this menu.
I am offering two suggestions.
A. Don't use the close button to get rid of the contents.
B. Set the type of child jframes to be utility.
I think having the JDialog close button destroy data is setting your users up for data loss. I would instead use the close to just hide the window, and then have controls inside of the dialog to cancel/finish/restart.
import java.awt.*;
public class DiFrame{
static JDialog log;
static JFrame ame;
public static void main(String[] args){
JFrame frame = new JFrame("Father of two");
JButton one = new JButton("dialog");
one.addActionListener( evt->{
if(log==null){
log = new JDialog(frame, "dialog child", false);
log.add(new JTextArea("fresh start"));
log.pack();
log.setVisible(true);
} else{
log.setVisible(true);
}
});
JButton two = new JButton("frame");
two.addActionListener( evt->{
if(ame==null){
ame = new JFrame("frame child");
ame.add( new JTextArea("fresh start") );
ame.setType(Window.Type.UTILITY);
ame.pack();
ame.setVisible(true);
} else{
ame.setVisible(true);
}
});
frame.add(one, BorderLayout.NORTH);
frame.add(two, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Click the dialog button and it shows the dialog. Then the text area can be modified. When the dialog is closed it can be re-opened.
Click the frame button and a jframe is shown. ( I actually cannot check if this shows up as a new application because it doesn't on my computer anyways. )

LibGDX physical scroll bar ScrollPane

I have created a ScrollPane but there is no bar to scroll with (like the one on the side of your browser) you have to drag with your mouse. How can I get a scroll bar?
What I did to add a scrollbar was use libgdx's ScrollPane.ScrollPaneStyle to set the scroll bar as a ninepatch.
ScrollPane.ScrollPaneStyle scrollStyle;
/...
scrollTexture = new Texture(Gdx.files.internal("Scroll9.png"));
scrollNine = new NinePatch(new TextureRegion(scrollTexture,6,6),2,2,2,2);
then I created the vertical scroll knob
scrollStyle = new ScrollPane.ScrollPaneStyle();
scrollStyle.vScrollKnob = new NinePatchDrawable(box);
and applied the style to my scrollable table
scroll = new ScrollPane(test, scrollStyle);
source:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ScrollPane.ScrollPaneStyle.html
The bar is enabled via the skin:
Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
ScrollPane scrollPane=new ScrollPane(resultsTextArea, skin);
I tried. It works now...
This is untested!
It seems that using the following enables scrollbars for a scrollpane.
boolean enable_x = true;
boolean enable_y = true;
scrollpane.setForceScroll(enable_x,enable_y);
source: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ScrollPane.html

change popup height

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);

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)

Setting the size of a ContentPane (inside of a JFrame)

I want to set the size of a JFrame such that the contentPane is the desired size. JFrame.setSize() doesn't take the window decorations into account, so the contentPane is slightly too small. The size of the window decorations are platform and theme specific, so it's bad news to try to manually account for them.
JFrame.getContentPane().setSize() fails because it's managed.
Ideas?
Thanks!
In Java 5 and later this is the easiest method:
JFrame frame = new JFrame("Content Pane Size Example");
frame.getContentPane().setPreferredSize(new Dimension(400, 300));
frame.pack();
frame.setVisible(true);
As always the above should be executed in the EDT.
If you want to do it on your own then in your Frame type:
this.setLayour(null);
this.pack();
Insets insets = this.getInsets();
this.setBounds(0,0, insets.left + width + insets.right, insets.top + height + insets.bottom);
this.setLocationRelativeTo(null);