Jbutton is moving components in different panel - java

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.

Related

JPanel deforms my Layout

Im using NetBeans to do a work for school. There i have an huge JPanel that contains a huge JFrame. That JFrame as 5 small JFrames, 1 is the menu with buttons, the other ones are boxes with text that will swap when i choose in the buttons.
When one box is showing the other ones are invisible im using the following code (dont know if it is the best):
public ConversorUI() {
initComponents();
PanelVazio.setVisible(true);
PanelTemp.setVisible(false);
PanelComp.setVisible(false);
PanelMoedas.setVisible(false);
this.pack();
}
My problem is, when i run my program i have a big space with nothing and only below it the components appear. I want them to appear in the top of my window. What can I do ?
ANSWER
After some time searching i just realized i could Set Layout from JPanel to Card Layout and create JPanels over each other activating them with the code:
private void DinheiroButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Remove Panels
CAIXA.removeAll();
CAIXA.repaint();
CAIXA.revalidate();
//Add Panels
CAIXA.add(DinheiroBox);
CAIXA.repaint();
CAIXA.revalidate();
}
Looks like you are using Java Swing , right ?
Any way you do something wrong if you have JPanel that contains JFrames. To build correct UI you have to add JPanels inside JFrame.
Also, to reach correct component order and placing you need configure corresponded layout, here is description.
You can load one jframe at e time
Because every jframe you added have own place and visibility doesnt do anything to remove
Try to save every jframe and for changing
Delete old one and add new one
Why are you using multiple JFrames to do this? From what I can see it would be a better idea to use JPanels that can take care of the individual tasks, such as the menu and buttons etc.
I'm relatively new to using javax.swing myself, but from my knowledge you can only have 1 frame at a time per window (like the other person said).
From what i've been able to discern from your project, you possibly don't even need multiple panels. You just need one panel for the menu with buttons, and multiple Labels or JLabels that display text according to the button. You can use the setText method in writing your addActionListener, something like this:
buttonName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
labelName.setText("blah blah blah");
//and whatever else you may need to do
}
});

How to change JPanel using KeyListener? [duplicate]

I am developing a game, where you first get to the main screen, there are multiple selections to go, for example, Singleplayer, Twoplayer, Credits, etc.
I have one big problem. If I click a button in the menu, (not JButton) the JPanels switch, but the keyListener is lost. The Keylistener is in the same class as the game code, which implements JPanel. I tried everything to get the Keylistener to work, but it just won't.
Here is how the things are called: Main class --> Menu --> Game. I tried adding the keylistener to the main class, but it's not working.
So, JPanel switching is ok, but the Keylisteners are gone. I was developing the game before with new JFrames, so when I clicked a menu, a new frame was created. I didn't insert a code here, because it's too long (2000+ lines), and the KeyListener is working, but only when it is in a new JFrame. I set the mode int in the Menu class, by clicking a button.
This is currently my panel switch:
public void setJPanel() {
switch (mode) {
case 1:
getContentPane().add(s);
validate();
break;
case 2:
getContentPane().removeAll();
getContentPane().add(sp);
validate();
break;
}
}
Thanks for your help in advance!
Rather than use a KeyListener, have you given thought to or tried using Key Bindings? KeyListeners require that the component being listened to has focus, and focus may be lost for many reasons, especially when swapping views (are you using a CardLayout for this?). Key Bindings on the other hand can be set to be responsive even if the bound component doesn't have focus but when it is only held within a window that has focus. Tutorial: Using a CardLayout
Edit
I see that you're not using a CardLayout, and I suggest that you use this as it can make your view swapping cleaner and easier.
Edit 2
I agree that you don't want to post your entire 2000+ line program here as no one will have the time to read it, but consider condensing your question/problem into a single small class that is compilable and runnable by any and all of us, and demonstrates your problem. In other words, a Short, Self Contained, Compilable, Example or SSCCE .
Remember, the code should be compilable and runnable for many of us to be able to understand it fully.
Cardlayout actually is screwy while refocusing.
#op, try calling requestFocusInWindow() after the new jpanel was added
Try using myPanel.requsetFocusInWindow();
before using setVisible(true);

Swing menu in Java

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!

Java: single frame vs multiple frames

Think about the classic installation process, where you have a "next" button and when you click it the content of the window changes. To represent this situation I thought of two possible solutions:
-when "next" is clicked destroy the current JFrame and create a new JFrame, maybe passing to his constructor useful information (e.g. actual window size, content inserted by the user in the current frame, ...)
-when "next" is clicked remove all the components from the current JFrame and add new components as needed
The first solution looks way better about OOprogramming, because I can keep separate classes for different frames and I can avoid huge methods that empty the frame and repopulate it. However the first solution sounds a bit "dirty" and I should pass lots of parameters to the new frame. To represent this situation I would choose the second solution.
Now think about a menu with an "option" component: in this situation I would create a new JFrame when "option" is clicked, so that I can populate it with option items. Is this a correct solution? Is there a way I can always know which one is the best solution? Are there any solutions I didn't think about?
Destroying the main JFrame would be silly -- not to mention jarring for the user. Just use a single JFrame and change its contents.
To implement an installer wizard, use a single JFrame containing one large JPanel on top and a smaller one containing the "Next", "Back", "Cancel" buttons along the bottom. When the Next or Back buttons are pressed, you replace the large JPanel. You can have many different JPanel subclasses, one for each "page" of the wizard.
There's a LayoutManager called CardLayout which is ideal for implementing this scenario -- it manages a "stack" of components, and only shows one of those components at a time. Use a BorderLayout in the JFrame. Into the center position put a JPanel with a CardLayout. Then add the individual pages of the wizard to that JPanel, so the CardLayout can manage them.
The CardLayout is well suited for this. You just swapout the JPanel contents when the "Next" button is pressed.

How could I implement new JFrame functionality

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

Categories