Add (multiple) forms to ScrollPane after clicking button in java - java

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.

Related

Switch between multiple JFrames

I'm relatively new to Java and I'm trying to make some kind of quiz. I created 3 JFrames, all in the same package. On my main frame, there are two buttons (one for the english version and the other one for the german version). I want to switch JFrames after pressing these buttons (so that I can, by pressing "English", see and interact with my english quiz frame). Looking it up didn't help me the slightest, because I'm not really experienced with it. Is it even possible to do it like this? If not, how could I do it?
The standard approach is to use the Card Layout, which allows you to use the same JFrame as you populate it with different things at different points in your application. So initially, your JFrame would show the loading screen, then the user presses a button and you load a new set of components without discarding the current JFrame you have. In some cases, you might also need to make some size adjustments.
It is difficult to say without seeing any code, but usually, what is done is that you do something like so:
new Frame(args);
this.dispose();
The code above assumes that the constructor of Frame takes care of launching and making the components visible. The this.dispose(); disposes of the current JFrame (assuming your class extends JFrame).
You have two buttons in your frame 1 right? So first, double click the button which says "English". Lets say the variable name for that button is jButton1. Inside that button type this.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
EnglishFrame eng = new EnglishFrame();
eng.setVisible(true);
}
Then double click the other button which says "German" (jButton2). Inside that type this.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
GermanFrame german = new GermanFrame();
german.setVisible(true);
}
this.dispose() - This will cause the jFrame window to close
Then you create an object of the other two forms. (In your case the form for English and Germany)
.setVisible(true) - This will show you the form.
Create a single Jframe window. After that create JPanels with all the compenents such as buttons, textfields and labels you want. Make sure the panel is the same size as your Jframe. Panel's work about the same as JFrame's, code wise.
This code will stitch everything together for you:
panel.setSize(Jframe.getSize()) //That wont
panel.add(button); //Just remember you need to add more code to position the buttons correctly.
//If you using netbeans builder:
//You just have to use this one line in the constructor/intialiser method
frame.add(panel); //This will add the panel to the Jframe/Window
//No need to add extra code for positioning.
If you want to swap between the panels. In the button press method, use this
frame.setContentPane(panel); //panel = panel you want to change too.
frame.repaint(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
frame.revalidate(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
When you first start the java application you have to set the content pane or else it will appear as a blank window.
frame.setContentPane(panel); //Starting Panel
frame.setVisible(true); //Make the frame visible
Sorry if the explanation is bad, I don't have enough time to explain it fully.

JPanel data getting changed on mouse click

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.

how to start, developing swing based application with few panels with next buttons for each

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.

Deleting a JPanel inside a JPanel... inside a JPanel

I am trying to make a slideshow-like application in Java using NetBeans.
I have a JFrame (the main window) and inside it are two buttons (back and next) for navigation, and a JPanel (mainPanel) which acts as holder of the 'slides' of the app. I am also using custom JPanel (SlidePanel) objects which are then placed inside the mainPanel. Some of these SlidePanel objects have JPanels within them.
Now, I've been able to place these SlidePanel objects inside the mainPanel and also navigate through them (for example, if I click the next button, SlidePanel 1 would be replaced by SlidePanel 2, or if I click the back button, SlidePanel 2 would be replaced by SlidePanel 1.) I've done this by using the removeAll(), revalidate(), and repaint() methods.
The problem starts here. The third SlidePanel object contains a JPanel inside it. What happens when you get to the third SlidePanel, it works fine. But from there, if you click the back button (which takes you back to SlidePanel 2), the JPanel from SlidePanel 3 remains on the screen, with the components inside SlidePanel 2 adjusted to fit the mainPanel.
What should I do to fix this?
EDIT: Thank you very much to all those who answered. I tried CardLayout on a dummy project and it worked fine. Now, all I have to do is add it to the original one. Thanks again. :)
I suggest to use CardLayout for that, rather as add and remove JPanels on the Runtime example for CardLayout and together with your idea could be here
I suggest you use a different layout manager (e.g. CardLayout); let the layout manager worry about juggling it's components.

Image not getting displayed on a JPanel

class Deal implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dl.setDeck();
dl.shuffle();
dl.firstDraw(pl);
for(Card c:pl.showHand())
panelplay.add(new JLabel(c.getImageIcon()));
panelplay.validate();
}
}
This is an event handler for a Jbutton. The method pl.showHand() returns a ArrayList of a user defined class 'Card'. Inserting a println() inside the loop shows the print, so the code is being executed but the Panel panelplay isnt showing card Images.
What about the existing labels on the panel? You don't remove them. I'm guessing you are using a FlowLayout and the labels just get added to the end of the panel so you don't see them.
So one solution is to use panel.removeAll() before adding the labels back to the panel. I then use:
panel.revalidate();
panel.repaint();
Or the better option as suggested earlier is to not replace the labels but just replace the Icons using the setIcon() method.
Do as Gilbert says, look at the Swing Tutorial part that concerns Labels.
JLabel has the following methods...
void setIcon(Icon)
Icon getIcon()
Also look at the SplitPaneDemo It does exactly what you want, you can even run it with JNLP to see.
You don't want to add the JLabel in the ActionListener.
You want to use an already added JLabel setText() method in the ActionListener.
You define all the Swing components once, when you create the GUI.

Categories

Resources