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)
Related
I want to create a customised title bar for my JFrame. I can remove the default title bar with
JFrame.setUndecorated(true)
Now i need to create a customised title bar for my JFrame with a close button?
Without having done that ever, I think I would go this way:
Indeed set the JFrame to undecorated
Extend JRootPane to add an additional field titleBar
Create a TitleBar component holding the title, the close button, etc...
Set a new LayoutManager on that JRootPane (have a look at JRootPane.RootLayout) and layout the components in the appropriate order (first the title bar, then below the menubar, then below the content pane)
Set an instance of that extends RootPane on your JFrame
There are maybe better ways.
I'm not quite sure of how you want to customize the close button, but maybe this can point you in the right direction: How can I customize the title bar on JFrame?
EDIT: Here's an updated working link to a forum about customizing his GUI and one user posted code on his creation of a simple GUI: Here
It looks like you can just modify his removeComponents method and create an addComponents method to fit your needs.
The Code According to the Above Link :
(Edited for Java 8)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
class Testing {
public void buildGUI() throws UnsupportedLookAndFeelException {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame();
f.setResizable(false);
removeMinMaxClose(f);
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("Exit");
p.add(btn, new GridBagConstraints());
f.getContentPane().add(p);
f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener((ActionEvent ae) -> {
System.exit(0);
});
}
public void removeMinMaxClose(Component comp) {
if (comp instanceof AbstractButton) {
comp.getParent().remove(comp);
}
if (comp instanceof Container) {
Component[] comps = ((Container) comp).getComponents();
for (int x = 0, y = comps.length; x < y; x++) {
removeMinMaxClose(comps[x]);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
new Testing().buildGUI();
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
}
may Work Fine but what if user also Want to set a L&F
such as nimbus
There are really three ways to approach this:
Set the frame to undecorated and implement everything, which includes control buttons, snapping, resizing and moving.
Get the root pane of the JFrame and directly edit that pane. You will need to add the control buttons and the snapping behaviour.
Use JNI to get the window's handle at the creation of a JFrame to get the control of it's attributes. This is better explained in this post. I have also built a little project which is basically an extension of the JFrame class that handles everything that needs to be dealt with... This last approach does not break native functions like snapping and resizing. But you do need to create the control buttons again since you have a new title bar if you want to build it from scratch.
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.
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!
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);
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);