JButton twice setText to JLabel after JPanel revalidate() - java

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)

Related

JFrame contains ToolBar with a JButton, press button put an existing panel on frame

It's been 13 years since I last worked with Java and I'm lost. Someone please toss me a life saver.
I want to add the JPanel requesting the address to display on the JFrame by adding it from a JToolBar. Each component is in a separate class.
Question is, the button is on a toolbar and the panel I want to add is a separate class. Any ideas?

JTable only showing after I resize the JPanel in Eclipse

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

show one panel after button is clicked and other panel when second button is clicked in the same frame

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();

How to add JTextField in time running in Java?

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.

Set the default button in jpanel depending on focus.

i have several JPanels which each implement their logic, buttons and fields on their own. This panels are all added on a JFrame but sometimes on another Panel which is then added on the frame...
What i want to achive is that hte default button is the ok button for panel1 when any successor of it has the focus. And same for all the others...
How to do this? The panels itself dont have a JRootPane and I don't want to bother the Frame with the logic for that.
Every tip is welcome ;). Thanks in advance.
i have several JPanels which each implement their logic, buttons and
fields on their own. This panels are all added on a JFrame but
sometimes on another Panel which is then added on the frame...
use CardLayout
What i want to achive is that hte default button is the ok button for
panel1 when any successor of it has the focus. And same for all the
others...
use CardLayout (Q&A on SO) and add separate Swing Action corresponding with each of card, use setEnabled(true/false) for Swing Action rather than add / remove Swing Action to JButton on then runtime
you can to use EventHandler, then there you can firing an action folded from String value (e.g. CardName + ButtonName + accesible context + UsersAccess + etc),
The panels itself dont have a JRootPane and I don't want to bother the
Frame with the logic for that.
I think you are right, to use CardLayout

Categories

Resources