Skip to content

Commit

Permalink
new method for get the main frame with a given component
Browse files Browse the repository at this point in the history
  • Loading branch information
astrapi69 committed Nov 24, 2023
1 parent cbae1ab commit 265b003
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.util.Optional;
import java.util.logging.Level;

import javax.swing.SwingUtilities;

import lombok.NonNull;
import lombok.experimental.UtilityClass;
import lombok.extern.java.Log;
Expand Down Expand Up @@ -96,4 +99,17 @@ public static void setComponentEnabled(Component container, boolean enable)
log.log(Level.INFO, exception.getMessage(), exception);
}
}

/**
* Resolves the application frame from the given Component
*
* @param eventSource
* the event source
* @return the application frame
*/
public static Frame getApplicationFrame(Component eventSource)
{
Frame frame = (Frame)SwingUtilities.getAncestorOfClass(Frame.class, eventSource);
return frame;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package io.github.astrapi69.swing.base;

import java.awt.Component;
import java.awt.Frame;

import javax.swing.Icon;
Expand All @@ -35,6 +36,7 @@
import io.github.astrapi69.model.BaseModel;
import io.github.astrapi69.swing.button.IconButtonFactory;
import io.github.astrapi69.swing.button.builder.JButtonInfo;
import io.github.astrapi69.swing.component.ComponentExtensions;
import io.github.astrapi69.swing.plaf.LookAndFeels;
import io.github.astrapi69.test.object.ApplicationTestModel;

Expand Down Expand Up @@ -122,6 +124,8 @@ protected JToolBar newJToolBar()
JButton hardDriveToolButton = IconButtonFactory.newIconButton(hardDriveIcon);
hardDriveToolButton.addActionListener(event -> {
System.out.println("hardDriveToolButton ....");
Frame applicationFrame = ComponentExtensions
.getApplicationFrame((Component)event.getSource());
ApplicationTestModel<String> applicationTestModel = ApplicationTestModel
.<String> builder().model("bla").build();
LabelBasePanel labelPanel = new LabelBasePanel(BaseModel.of(applicationTestModel));
Expand Down

0 comments on commit 265b003

Please sign in to comment.