I am using three JButtons in my swing application. When I click on each button, the corresponding data (formatted in JTable with JScrollPane) will display on JPanel.
Problem: when I resize the JFrame, the JPanel is replacing with default button (the button which i was clicked first) information instead of current JButton information.
My sample code:
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actioPerformed(ActionEvent e){
if(e.getActionCommand.equals("button1"))
JPanel.add(table1);
}
if(e.getActionCommand.equals("button2"))
JPanel.add(table1);
}.......
Resizing the JPanel will not suddenly replace components or add other components to the panel.
My best guess (and it is a guess due to the limited information in the question) is that none of your buttons actually work and just show the wrong information.
The code you posted only contains an add without any revalidation of the layout. Consult the javadoc of the Container#add method. When you resize, the layout gets revalidated and you see what is actually contained in the JPanel.
Possible solutions:
Call invalidate and repaint on your panel as well in your ActionListener
Use a CardLayout to switch between the different components
I personally prefer the CardLayout option, but it might depend a bit on the situation.
Note that in the code you posted, you add table1 for both buttons. Might be a copy-paste problem, or a problem with your actual code.
I was unable express problem clearly.Sorry for your inconvenience.
JPanel.removeAll() method has fixed my problem.
I added this method before adding any new component to JPanel. That fixes JPanel unexpected behavior.
Related
So I have a program that at the start only contains an 'add movie' button at the bottom of the frame.
Above it I inserted a scrollpane.
I also made a seperate JPanel form which contains labels and textfields where you have to input the data of the movie.
Every time I click the 'add'-button I want a form to appear inside the scrollpane (next to previously made forms).
So I figured I just needed to do this:
private void AddMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {
MovieForm movie = new MovieForm();
MovieScrollPane.add(movie);
}
But nothing new appears.
I tried validate() and repaint(), but so far these don't seem to work.
I made the interface in Eclipse btw.
Anyone who can help me?
Thanks anyway!
MovieScrollPane.add(movie);
Don't add components directly to the scrollpane. Normally a JPanel is added the the viewport of the scrollpane.
Then, whenever you add a component to a visible GUI the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
This makes sure the layout manager is invoked to the preferred size can be recalculated.
Also, follow Java naming conventions. Variable names should NOT start with an upper case characters.
I encountered a problem with OverlayLayout.
Basically, I created a component which allows to unroll an overlay content above a main content (like a drop down menu). It works!
The problem is: if I put a JButton on the main content (the content below), when I click on this button, then the button starts to appear above the overlay content !
I don't know why?
Does anyone know what the problem is ? Is it maybe the focus ?
I finally found the answer!
It comes from another post on stackoverflow...
When components overlap on the panel then you need to tell the panel so
it can make sure it repaints the components in their proper ZOrder:
You do this by overriding the isOptimizedDrawingEnabled() method of
the JPanel to return false.
I have a left Panel with multiples Jlabels which i use them as buttons to change a Main Panel's content which is layouted with a CardLayout.
I cant work perfectly with these events:
mouseEntered : to make highlight effect to the jlabel
mouseExited : to take off the highlight effect.
mouseClicked : to change the content of the main Panel and start some threads
The problem here that can't found an event or a method tell me that another Jlabel has been clicked so i can stop my threads started in the mouseClicked event,
OR
an event or method tell me that a JPanel in the CardLayout has been displayed or hidden.
Your problem is not finding an appropriate event. I think you are doing this using a visual GUI builder and expect to solve everything out-of-the-box. It's not going to work that way, you will need to write some real code. For example, write a method that you will call from the mouse click listener of each of the three JLabels. Thus you will have arranged for this method to be called for each JLabel click. Then in the method do the appropriate handling. This is just a rough outline, you haven't provided much detail to give any further advice.
It sounds like you need FocusEvents and FocusListeners. These are supported by all JComponents like JPanel, JLabel, and JButton, such as by calling addFocusListener();
Basically a FocusListener can tell you when a JComponent gains focus (such as by clicking on the JComponent) and when it looses focus (such as by clicking on a different JComponent).
Refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusListener.html for further information
I'm new to java.I'm creating a swing based UI. I've created 2 frames, each one in separate .java file inside same package.
These two frames represents 2 screens (panels) of application. When Next button in first frame is clicked, it should move to second frame.
When I checked, these two classes are having main method, I think it should be correct way for creating applications. there should be only one main method.
When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?
As I'm beginner, Can somebody suggest me in how to start up with these kind of applications? what are the guidelines that need to be followed? And please help me in finding documentation related to starting up with the development of such applications.
After going through the answers, My comments are:
I used the following code to go to next panel from first panel, but didn't worked.
private void gotoNextPanel(){
// jPanelFirstScreen.setVisible(false);
JPanelSecondScreen jpanelSecondScreen= new JPanelSecondScreen();
jpanelSecondScreen.setVisible(true);
UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
upgradeUtilityGUI.removeAll();
validate();
repaint();
// upgradeUtilityGUI.add(jpanelSecondScreen);
upgradeUtilityGUI.getContentPane().add(jpanelSecondScreen, "card2");
jpanelSecondScreen.setVisible(true);
validate();
repaint();
}
I'm using netbeans, and 've added two panels to the cardlayout of frame. And when I use the above code to change panels, Nothing is happening, the first panel is still appearing. Can somebody tell me, how to write code for moving from one panel to another when both the panels 've been added to cardlayout of jFrame ?
Use a CardLayout, as shown here (and one frame) as mentioned by others.
When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?
Make the panels public access level and they will be available from other packages.
One problem in that code snippet is implied by the line:
UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
It goes out of scope before ever being added to a container. Also, their should be no need to remove anything when adding a new card to the layout, and no need to call repaint().
If your application is as simple as having only two panels you shouldn't create two JFrames. You should create a JFrame with two JPanel each of them contains the neccessary information for you. If you are ready with your first panel you can call setVisible(false) on it, and call setVisible(true) on the 2nd frame. It is the one of the most easy-to-understand solution.
But, it only depends on you if it is good for you or you would like to use some more detailed solution.
Don't use two or more JFrames, nor with separated and compiled Jar files, this is road to the hell, better would be look at CardLayout,
What you should do is have a single JFrame for the application, then you add and remove JPanels as you want to move between screens.
Each of your JPanels should basically have the following...
1. A JButton called "Next"
2. A ButtonListener for each button, that tells the JFrame to load panel2, panel3, etc.
As part of the ButtonListener, you basically just want to call something like JFrame.removeAll() to remove the existing panel, then JFrame.add(JPanel) to add the next panel.
By having 1 JFrame, you also only have 1 main() method.
In my Netbeans code I have JPanels and JDialog which are driving me crazy at times. Some of the controllers on these containers decide not to show up or automatically change size even though I have set up both their size and contents within the code and through using the IDE properties. For instance some of my jButtons on a certain JPanel does not show its text label or the sizes of some of my text field change.
Any solution to this would be grately appreciated!
When you create GUI using the NetBeans IDE wizards the Layout manager attached with JPanel and JFrame is GroupLayout and it works as expected. It keep the size of your JPanel and JFrame as you have specified.
Now if you change the LayoutManager of the JPanel or JFrame then you are on your own. You must know the consequences of changing the LayoutManager and update / add the required code to make the code to run as expected.
I will suggest you to keep the default LayoutManager as GroupLayout if you want to get what you see in the NetBeans component designer.
Unfortunately you did not provide any code snippet that can show your problem. But let me assume that you are confused with layout behavior. Typically we use Layout manager and delegate to it the responsibility of placing and re-sizing the graphical elements. Layout manager does it work when the parent element is being painted, i.e. during execution of method paint() that happens asynchronously and may be caused by various events (e.g. changing focus, re-sizing of window etc).
In this case all your attempts to change size of specific element by calling its setSize() could be overridden by layout manager that decides to change size of the same element differently.
So, if my assumption is correct learn to use layout managers and ask more specific questions if you have any difficulties with them.