I am trying to remove the drag bar across the top of the JFrame. I would like to keep the minimize maximize and close options that appear on this bar available. What I was thinking was to remove the bar (and icons). Then add the icons as embedded images, that implement the JFrame actionlistener. It would also be necessary for this to work with JInternalFrames. Any help would be greatly appreciated.
You need to step back and understand how Swing works.
When you create a JFrame, Swing uses the OS widget for the frame. The title bar that you see is part of the OS component and you have no direct control over it with Swing. You can hide the titlebar (and border) of the frame by using setUndecorated(false) as suggested earlier. In this case you loose all the functionality associated with the title bar (dragging and access to all the buttons) and the Border (resizing). So if you need any of this functionality you need to recreate it all yourself.
On the other hand you can use:
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
and Swing will build a title bar and Border for you and add back all the default functionality. So if you want to prevent dragging you would now need to inspect the JFrame for all its components to find the component that represent the title bar. When you find this component you can then remove the MouseMotionListeners from the component to prevent dragging. This way the title bar will still be there and the buttons will be active, but the dragging will be disabled. I would guess that is easier the adding in all the functionality to an undecorated frame.
As you have already realized a JInternalFrame is a component completely written in Swing so you have access to the child components, which is essentially the approach I'm suggesting for the JFrame as well.
To remove the titlebar, use
setUndecorated(true);
You could then re-add buttons for maximize/minimize. The source for maximize-button could look something like that (just to get an idea). Use JFrame.ICONIFIED for minimize button.
JButton btnMaximize = new JButton("+");
btnMaximize.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(MainFrame.this.getExtendedState() == JFrame.MAXIMIZED_BOTH) {
MainFrame.this.setExtendedState(JFrame.NORMAL);
}
else {
MainFrame.this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
});
Take a look at this article - i think it is pretty much what you need for the JFrame part.
http://www.stupidjavatricks.com/?p=4
It is based on JDialog, but it should be pretty much the same as JFrame. Maximize/minimize should be pretty much the same as the close button.
For JInternalFrames...
javax.swing.plaf.InternalFrameUI ifu= this.getUI();
((javax.swing.plaf.basic.BasicInternalFrameUI)ifu).setNorthPane(null);
Related
I'm wondering, how can I make a menu system (not JMenuBar) with swing components (like in games, where a new 'screen' comes in)? Maybe switching between panels? How?
I've heard about card layout but i'm not sure it's for me, and I don't really understand how should I implement it.
Example (sorry for ugly drawing): http://i57.tinypic.com/14mckkk.png
Based on the screenshot you've provided, you should look into using Swing Dialogs. Dialogs are little pop up windows that can be activated upon button presses, and that can contain as many or as few components as you like.
So your grey JFrame in the image would have three buttons; the action on clicking one would be to open a dialog populated with "Content A". The "Button X" in that dialog could either be a vanilla "Close" button that the dialog API will provide for you for free, or it could be some other button of your design.
If I'm not mistaken, what you are asking is for something like this:
In this case you can split the frame with using a layout of your choice that can fit to the size you want (personally I'd opt for a BoxLayout). Then you can go on with adding the buttons to the side panel and add that to the box panel so you end up with something like this:
Now add a JPanel and you'll have your content section. You'll have to make sure you add Listeners to the buttons so that the content that is displayed is updated. It'll look something like this:
button.addActionListener(
new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(content) {
content.removeAll();
}
populateContent();
content.validate();
}
});
I could be way off because of the little amount of information at hand, this is just my interpretation of the question. I would have commented but I don't have the rep so I'm sorry in advance if this is completely off!
I need to make a custom ui which looks like the following image :-
I know how to change the button to that arrow shape and everything else.But I can't understand how to move those close ,restore and minimize buttons to the center and give them round shape (on Windows).
On Googling ,I found how to make custom shape windows but it doesn't meet my requirements.
Can anyone please tell me how to do this or any link.??
You will need to create your own title bar as another panel with buttons and then remove the window decoration on the frame.
JFrame frame = new JFrame(...);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//This will close the frame and exit
To minimize and maximize, listen for button events and use:
frame.setState(Frame.ICONIFIED);//minimizes
frame.setState(Frame.NORMAL);//restores
See Frame.setState(int) for details.
I'm trying to create an app with a JLayeredPane that scales automatically with the size of the parent JFrame (this is the easy part using BorderLayout as layout manager on the frame's content pane). The hard part is the fact that I want the content of the JLayeredPane to automatically resize with the JLayeredPane (and thus the JFrame too).
In fact the functionality I want to achieve is a lot like the dockable console in Netbeans, that just like comes "on top" of the editor when clicked, and when dismissed hides again and docks into the "console" word in the status bar. Obviously I also want resizing behavior to work correctly. What would be the best way to achieve this (if not with JLayeredPane)?
You'll need to use a ComponentListener added to the JLayeredPane and have its content responds to the void componentResized(ComponentEvent e) method.
I have searched for this problem and haven't found anything close. I will try to be specific and post code however this is a large program so I can't post all the code. The problem in general is this: A JButton on one panel causes components on another panel to shift at first click. This only occurs when there is an action listener added to the button. (clicking the button without an action listener doe noting (obviously)). The problem is that the action listener i add only changes the button background, text, and size (to fit new text).
Here is the action listener as of right now. login is the JButton:
private class LoginListener implements ActionListener{
public void actionPerformed(ActionEvent e){
loggedIn = !loggedIn;
if(loggedIn){
login.setText("Logout");
login.setBackground(Constants.RED);
}
else{
login.setText("Login");
login.setBackground(Constants.UPPER_BOUNDARY);
}
login.setSize(login.getPreferredSize());
}
}
The setup is this. The action listener is a subclass of the loginPanel where the login button is located. That loginPanel is added to the main JFrame at the upper 1/4 of the frame. The lower 3/4 of the main JFrame is mainPanel which has other swing components. The loginPanel and mainPanel do not share components or variables or really know of each other's existence (as far as I have coded). Yet when this actionlistener above is added to the login button components in the mainPanel shift from their positions to other positions. The only happens at first click and then they stay where they are at (not where i want them).
Other factors:
- I use absolute positioning (sorry if you don't like it but I like it better)
- I am using a SynthLookAndFeel but have never had this issue with this look and feel before.
Thanks
Other factors: - I use absolute positioning (sorry if you don't like it but I like it better)
There's nothing to be sorry about, and the solution is simple: Don't use absolute positioning, but instead learn about and use the layout managers to there full abilities. One of the reasons to use them is to avoid pernicious bugs like this one. It's quite possible that your code is in fact using a component's default layout manager even now without you knowing about it. You can find out more about them here. One of the keys to using them well is to nest them by using nested JPanels, each using its own layout manager. Then they can do the heavy layout lifting for you automatically.
I am learning java and building one project to test basics.
I have one menu item FILE and then sub menu item like
1)Front
2)Admin
3)Booking
I have separate gui made in separate files but i want that they should be visible in one area , with click on submenus
I am using swing , JmenuBar . Also the other guis are using Jframe
I have separate gui made in separate files but i want that they should be visible in one area
Most applications should only ever have a single JFrame, which indeed is your requirement since you want the separate GUI to be visible in the same area.
Therefore your other GUI, should not extend JFrame but instead should extend JPanel. Then you can just use a CardLayout on your real GUI to swap the panels in/out depending on which panel is selected from your menu. All these basic are covered in the Swing tutorial. I guess you would start with the section on:
How to Use Card Layout
How to Use Menus
Other people have already talked about ActionListeners and stuff, so that's half of the problem. The other half is how to actually deal with the multiple windows. I would probably not use one JFrame per different GUI, given that the spirit of the JFrame suggests you should only have one instance of it per application. Instead, I would look at using either JDialog or JInternalFrame. I'm not sure what you mean by
...should be visible in one area...
but JInternalFrame will allow you to implement something like a multiple document interface, where all the sub-GUIs would be contained within the frame of the main UI. JDialog would be give you independent windows like JFrame does.
If with "they should be visible in one area" you mean modal, then you should change all your JFrames to JDialogs and leave only the JFrame that contains your main-menu.
To do this, you need an ActionListener for each of the menu items. Then have each listener pass the instance of the JFrame you want to a method that controls where you want to position the window and show it.
//Make menu items
JMenuItem font = new JMenuItem();
font.addActionListener(new ActionListener() {
showWindow(new FontFrame());
});
JMenuItem admin = new JMenuItem();
admin.addActionListener(new ActionListener() {
showWindow(new AdminFrame());
});
...
//define frame handling method
void showWindow(JFrame f) {
...
f.setVistible(true);
}