In order to develop a edit text in java for study. I'm with a problem: My program once opened by the user, if the user to click in button "Search", then the ActionListener will add a field in Jpanel.
For example: I have a class JToolBar that sets up jtoolbar menu that extends JPanel. Then, I add it in JFrame. Within that JToolbar there's a button "Search", if the user to click at this button, a JTextField will appears side this menu instantly.
I try to create a class private within that JToolBar class. So, I just add the JTextField to JPanel contains the JToolbar. However, is not working. There's no error. Simply not appears the JTextField. What I do to solve that problem ?
When you add a component to a visible GUI the general code is:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
You need the revalidate() to tell the layout manager that a component has been added.
Related
I've been looking for a while and I can't find an helpful answer for my problem.
I'm using the Netbeans GUI Builder but right now I'm stuck: my intention is to use a button to make visible a jPanel object.
I've tried to directly insert this jPanel inside the JFrame (leaving it not visible until the button is pressed). The thing is, I could access the jPanel without the need of pressing the button: the panel wasn't visible but I could access the textfield if I tried to click over the blank space the panel is supposed to appear onto.
So now I have a separated class jPanel1 but I don't know how to add it to the JFrame.
This is the code I'm using for my button
private void modifyActionPerformed(java.awt.event.ActionEvent evt) {
JPanel newpanel=new modifyUserData(this.thisUser);
this.getContentPane().add(newpanel);
newpanel.setVisible(true);
}
but when I run it and press the button I get an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
Any ideas?
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 am creating a Swing based application , which actually consist of two buttons as shown below -
now what i want is when first button is clicked it must perform a action like showing a panel containing label, textfields , and some buttons in the same frame as shown below -
and when second button is clicked it will show another panel in the same frame as shown below ..
the thing is i am not understanding how to make this interface in action by providing event handler and action listener .. So please letme me know how can i archive this . Any help would be appreciated .. Thanks
There are 2 approaches.
CardLayout based.
Create all the panels (empty, panel with fields, panel with list) and add them in a container with CardLayout (the empty one is default). On click buttons swap visible cards (panels) showing necessary one.
Recreation based.
On click button create a new panel with new content. Remove the old one from container and add the newly created pane. After then call:
container.revalidate();
container.repaint();
When I click button I want to add one button to panel, which I did, but my program doesn't work like before. This is my program before:
South panel with pink buttons is cardlayout panel. When I click gray buttons on east, card panels changed. When I click pink button, this happened:
One pink button setText to one label. After I add one button to cardpanel "TOPLI NAPICI" this happened:
As you can see, one button "moka" add to panel,which I want, but when I click one button, it setText twice, only new button (in this case "MOKA") setText once. This happened also with other panels in cardpanel:
this is part of my code when I click button to add new button
if (enter == JOptionPane.OK_OPTION) {
try{
Double price1=Double.parseDouble(priceField.getText());
String name1= productField.getText();
Product name = new Product(name1, price1);
Application .manu.add(name);
if (field.getSelectedItem().equals("TOPLI NAPICI")){
for(Controller c:Controller) {
c.tnp.add(new JButton(name1));//c is panel , holds all panels
c.tn.revalidate();//c.tn is panel which is changed( adding one button), cardpanel
c.removeAll();
c.panels();//create all panels which is removed
c.revalidate();
System.out.print( c.tnp.size());//tnp is a list with buttons, shows that add one more button, that is correct
}
Like it duplicate panels, or when I click button it clicked twice. Is it possible? And although I changed only one panel in cardlayout ("TOPLI NAPICI"),it changed in all panels in cardlayout ("SOKOVI" and "ALKOHOL")
What is wrong?
Thanks for any help.
no idea whats LayoutManager is used, nothing talking about CardLayout, for better help sooner post an SSCCE, short, runnable, compilable, just about JPanel with CardLayout and one JButton for code posted here,
images posted here without code not helped
CardLayout by default never required call for revalidate() & repaint(), all those notifiers are implemented in API by default
this isn't proper way
.
c.tn.revalidate();//c.tn is panel which is changed( adding one button), cardpanel
c.removeAll();
c.panels();//create all panels which is removed
c.revalidate();
apply all changes to already visible GUI (remove, add, modify, relayout), then last two code lines should be revalidate() & repaint(), for 1st. container where are changes done, or if there are changed a few containers, then to call their 1.st joined container
up to Java7 is required to use validate() & repaint() for JFrame, JDialog, JWindow, in the Java7 is implemented revalidate() for Top Level Containers too,, the same as for JPanel (for example)
i have jFrame = frame
it have jcombobox = combo
then i have jpanel = panel
i have many component inside this panel
i try to add this panel into combobox popupmenu
so if combobox clicked,
panel that have many components will show up
it is possible to add panel into combobox popup menu?!?!
how to do it???
i already read
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
and
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java
but still not have any clue
how to do it?
thankz a lot for any help...
So from your description, you have a panel that is not visible, that you would like to appear if the combobox is clicked? So it will appear for any option in the combobox?
That should be simple enough. Lets modify the JLabel in this ComboBoxDemo from the Java Tutorials. Since they both inherit from JComponent, we will be able to make the JLabel and the JPanel visible in the same way.
First, make sure you understand what the demo is doing. The Combobox options are changing the format of the date's text in the JLabel. We want to edit the demo in such a way that this JLabel is not visible until after we select any option in our JComboBox.
First, we will want to include a boolean as a class variable, so that we can access it in any of our methods.
boolean visibleComp;
Next, in the constructor, you will want to change the JLabel "result" to be invisible by default. We can do this by using the setVisible method of the JComponent.
result.setVisible(false);
Now we need to control when and how result becomes visible -- as we continue through the code, we see that the actionPerformed method handles our events, and passes the formatting details off to another method called reformat.
Since reformat is also called in our constructor, we will want to set our boolean value in the actionPerformed method.
visibleComp = true;
We will then want to add a conditional statement to the try block in reformat -- this will check to see if our boolean is true, which would only occur if an action had been performed by the user. We can use this to set our component's visibility.
if(visibleComp){
result.setVisible(true);
}
You can easily interchange a JPanel with this example. Hope that helps.