so i have a very basic java application, there is a panel and inside that is a button. When a user clicks the button, i want a picture to come up in another panel in the same form.
I searched up ways to load images from web/from my folder and this is the code i've come up with:-
private void buttonActionPerformed(java.awt.event.ActionEvent evt)
{
ImageIcon icon = new ImageIcon("URL-of-the-image");
panel2.setIcon(icon);
}
~~what panel 2 looks like~~
where am i going wrong?
it says that the method "setIcon()" is causing a problem but i don't know any other method to do this job. Please Guide!
If panel2 is a JPanel, it doesn't have a setIcon method.
Use a JLabel instead , it has such a method.
I have a main JFrame containing a JPanel with some stuff in it. Among other things, another JPanel with a list of sub-JPanels and JComponents in it.
What I want to do is to refresh the window (or only the list) by pressing a button in another JFrame.
I tried to do it by
Mainclass.frameIWantToRefresh.invalidate();
Mainclass.frameIWantToRefresh.validate();
Mainclass.frameIWantToRefresh.repaint();
But it doesn't work and I have no idea how to do it in another way.
Any clues?
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.
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)
Is this possible to add a JFrame into a JPanel in Java.
i m using a java program which is giving output as a frame which i wanted to display inside another program's JPanel on click of a button.
How can i do this?
Read about JInternalFrame. I think it's the way to go here.
http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
You could call getContentPane() on the JFrame to extract its main contents as a JPanel (usually) and without the menu bar and decorations, and display that as a JPanel though best would probably be to update the original program so that it produces a JPanel and not a JFrame.