I'm a java newbie... I need to know how to reload a JPanel? I extended the JPanel class and created a panel that will run in a cardlayout in an Applet. I want this panel to reload/refresh after the user clicks a button in this panel. I tried including the revalidate() and repaint() methods (methods I don't understand well) in the ActionListener for the button but nothing happened. Can anyone throw some light on how to correctly refresh the whole panel?
This works fine for me. But after an other
_panel.revalidate();
_panel.repaint();
Related
I have a button called btnDisplay on a JPanel called TempPanel. When the button is clicked, it should display a JTable that is created manually.
However the table is only visible to me after I resize the panel manually with a mouse. Even if I make the panel smaller than it originally was, it shows the table, otherwise it doesn't.
What is the reason for this? And how can I fix it?
I'm writing the comment as an answer for clarity purpose:
Call revalidate and repaint on the container to which the JTable is been added
I'm a java beginner, and I'm now on the GUI.
My problem is that to make my gui I've made several panel in different classes like connexion panel, menu panel etc... and for each panel I've a button which should interact with my JFrame window. So I don't know for example how the button in the menu will interact with my JFrame window (which is in a different class) to refresh it and then to put another panel to replace it .If someone can help please?
i'm a java beginner , and i'm now on the GUI , my problem is that to
make my gui
please to read offical Oracle tutorials Using Swing Components before asking a question here in Creating a GUI With JFC/Swing
i've made several panel in different class like connexion panel , menu
panel etc.... and for each panel i've a button which should interact
with my JFrame window .So i don't know for exemple how the button in
the menu will interact with my JFrame window (which is in a different
class )to refresh it and then to put another panel to replace it
seems like as CardLayout is best solution without unwanted side effects
I'm using netbeans IDE and I created a Jframe with two Jpanels one is for Jbuttons and other one is for load another Jpanels to it when clicks those buttons.
I tried to do it from buttonclick action.
Jpanel2 j2=new Jpanel2();
JPanel1.add(j2);
j2.setVisible(True);
but this code is not working. I want to know how can I do this.
(I think this is also same as loading JinternelFrames)
Try to call revalidate() method.
Use a card layout and do it correctly.You can learn how to use card layout here
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.
I would like to create a "wizard" on a JDialog using the CardLayout, triggered by user pressing the New button from the menubar. In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. In my "New" menu item I wrote the following code to initiate the jDialog as follows,
CardLayout cl = (CardLayout) jDialogNew.getLayout();
cl.preferredLayoutSize(jDialogNew);
cl.show(jDialogNew, "card1");
However, the compiler comes up with the following error,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.awt.BorderLayout cannot be cast to java.awt.CardLayout
If anyone is out there that can take me through creating a wizard on "Netbeans" I'd be eternally grateful
Your jDialogNew has a BorderLayout set as its layout and and not a CardLayout, meaning that when you call getLayout() to try to fit it into a variable that cant hold a BorderLayout an exception is thrown. The classes are different so you cannot cast from one to another, causing a ClassCastException.
A possible solution to this is to set your own layout for the jDialogNew. I dont have code infront of me so I cant check myself, but try looking for a method like setLayout(), and pass in a new layout of your choice.
you can do with following
create JFrame -> Add "CARD LAYOUT"
add JPanels to project. Design JPanels. Customize init code of JFrame. Insert JPanels with this.add(jpanel name). for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with.
The way I did it in Netbeans was very easy! All I had to do was to was to introduce a separate JFrame in my resources package (being a part of my overall package) and in that JFrame I created a JPanel with the CardLayout, under which I created all my other JPanels relating to that top JPanel. Now having the JFrame I could set my fixed canvas plus everything else I needed to construct and activate my CardLayout "Wizard" dialogue box! Then I had to call the new JFrame from with my application whenever the event was triggered. It made life a whole lot easier and it works just great!