Set the default button in jpanel depending on focus. - java

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

Related

Add JLabel to JPanel in TabbedPane after action performed event in another Jpanel

I want to add JLabel to JPanel (say VisualPanel,which is inside a JFrame) based on the action-performed event (JButton) occurred on another JPanel(say JobPanel) (separate class but add to the JFrame).
How to get the VisualPanel object inside the JobPanel to add the JLabel?
I tried importing the JFrame into JPanel and get the VisualPanel instance but somehow i am getting into infinite recursion.
My question is my design approach correct?. If not how should i go about it?
if my design is correct any suggestion in the right direction is highly appreciated. Thank you.
Give VisualPanel a CardLayout with two panels: one panel that has the JLabel and one that doesn't. Give your JButton an ActionListner that will show() the card you want. Then you can use setSelectedIndex() to select the VisualPanel tab. See the tutorial for examples.

How to refresh a JPanel at runtime?

I have found that there are many similar topics here, but my problem is something more complicated.
Background of my problem:-
I have a JFrame called Main. On this JFrame I have two buttons and one JPanel called WorkingPanel. Then I have another JPanel(called PlayerPanel) but this one is a seprate file (as a class).
Now I want that when I click a button, it should change WorkingPanel to PlayerPanel. I have wrote following code.
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
WorkingPanel = new PlayerPanel();
System.out.println(WorkingPanel.getName());
WorkingPanel.revalidate();
WorkingPanel.repaint();
WorkingPanel.setVisible(true);
Window.revalidate();
Window.repaint();
}
Please guide me, Thanks.
I have found that there are many similar topics here, but my problem is something more complicated.
On the contrary, your description is of a rather basic problem that is very easily solved by using a CardLayout. I suggest that you do this now. If you had it in place your method could be as simple as:
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
cardLayout.show(cardPanel, WORKING_PANEL);
}
where cardLayout is your CardLayout variable, cardPanel is the JPanel that displays the "cards" that displays the swapping JPanels, and WORKING_PANEL is a String constant that you used when you added your WorkingPanel instance to the cardPanel.
Point 2:
Don't use a MouseListener on a JButton as it won't behave correctly. For instance, if you disable the button via setEnabled(true) the button won't truly be disabled. Instead use an ActionListener with JButtons as the tutorials will show you. That is what they are for.
Edit
For examples of CardLayout-using GUI's, please check out:
getting Jcomponent from other class changes frame size
Java CardLayout Main Menu Problem
Change size of JPanel using CardLayout
Java CardLayout JPanel moves up, when second JPanel added
Java swing; How to toggle panel's visibility?
Clear components of JFrame and add new componets on the same JFrame
gui multiple frames switch
JLabel displaying countdown, java
This one is unusual in that it uses a CardLayout and has one panel fading into the other panel:
CardLayout showing two panels, flashing.
You could use CardLayout instead of that approach. You will be able to switch between different panels very easy and efficiently. It's also wort of mentioning that use of CardLayout is less verbose approach.
Use a CardLayout, containing your two panels, but only showing one at a time. The CardLayout is documented, with examples, in the Swing tutorial.

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

JButton twice setText to JLabel after JPanel revalidate()

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)

how can i make check out progress bar in swing

I want to create check out progress bar which to be run step-by-step like below image. how can i make this in swing. please give me some idea.
thanks in advance
this is about customized JTabbedPane (not easy and good job)
override JTabbedPanes methods paint() and with paintBorder()
use two prepared Icons(ImageIcons) into JLabel for every tabs, one for selected, 2nd for unselected
use two prepared Icons(ImageIcons) into JButtons for JPanels layed by CardLayout, one for selected, 2nd for unselected, notice - contents shoudn't be nice for resiziable contents, have to layout by GridBagLayout by using anchors
use JLayeredPane with Icons(ImageIcons) into JButton for JPanels layed by CardLayout, notice - there are max 6 members for JLayeredPane

Categories

Resources