notify the caller class something has happened - java

Say, you have a subclass of JFrame, and use it to create your own custom JFrame. In this class (we'll call it mainFrame), we create a reference to another custom JFrame class (we'll call this one sidePanel).
In sidePanel, you have different buttons, radio buttons,..
My question is, is there a way to notify mainFrame the user presses on a button?
I've created a (untested) example of what I mean:
class mainFrame extends JFrame {
public mainFrame() {
super("main frame");
//...........
sidePanel panel = new sidePanel();
//...........
}
public static void main(String[] args) {
mainFrame mainF = new mainFrame();
//.........
}
}
And the sidePanel class:
class sidePanel extends JFrame {
public sidePanel() {
super("sidePanel frame");
//...........
JButton button1 = new JButton();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Notify mainFrame somehow button is pressed
}});
//...........
}
}

To notify mainFrame of an event, the SidePanel instance (really bad name for a Frame) must have a reference to mainFrame. Pass mainFrame as an argument of the SidePanel constructor, and callback mainFrame from the actionPerformed method in SidePanel:
SidePanel panel = new SidePanel(this);
and in SidePanel:
public void actionPerformed(ActionEvent e) {
mainFrame.buttonHasBeenClicked();
...
}
This tightly couples both classes though. A way to decouple them is to make the SidePanel object accept listeners for custom events, and to fire such an event when the button is clicked. The mainFrame would construct the SidePanel instance, and add itself (or an inner anonymous class instance) as a listener to the sidePanel.
See this page for an example.

Related

How to organize java swing application architecture?

I am making a small project in Java. By design, the application will be similar to QTranslate - it is a GUI for various translator APIs. The application will have 1 main form, the translator itself and several auxiliary forms (Settings, language selection, saved words, etc.).
The moment is not clear how to implement the interaction of auxiliary forms with the main form? Each auxiliary form will affect the main form. For example, when choosing a language, a new button will be added to the main form. Or when changing the settings, the hot keys will change. Each form is a separate class. Do I understand correctly that the only way to implement this is to make the main form class static and access it from other non-static classes of auxiliary forms?
screenshot of my application
Making the main form class static would not be the appropriate OOP way i suppose. Instead I would pass your main container to the Constructor of your auxiliary form class, which would be more the OOP way.
Example Code (Parent Frame):
public class ParentFrame extends JFrame {
public ParentFrame() {
this.setSize(400, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button = new JButton("Open Window");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ChildFrame(getContentPane()).setVisible(true);
}
});
this.getContentPane().add(button);
}
public static void main(String[] args) {
new ParentFrame().setVisible(true);
}
}
Example Code (Child Frame):
public class ChildFrame extends JFrame {
private Container parentContainer;
public ChildFrame(Container parentContainer) {
this.parentContainer = parentContainer;
this.setSize(400, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button = new JButton("Change Frame Color");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frame = (JFrame) SwingUtilities.getRoot(parentContainer);
frame.getContentPane().setBackground(Color.RED);
}
});
this.getContentPane().add(button);
}
}
This just creates a parent frame with a button which opens a child frame with a button. If you click on the button in the child frame it changes the background colour of the parent frame.

ActionListener in parent class

I am struggling with the ActionListener in Java in a parent class, I tried a bunch of possible solutions but could not get it work. This here also did not help:
Java actionlistener actionPerformed in different class
The problem is as follows:
Class2 extends Class1, I have a button in Class2. As soon as the button in Class2 is pressed, Class1 should be notified through action listener and perform the event.
I'm struggling to let Class1 know that the event has happened. It looked pretty simple to me, but nevertheless I'm struggling.
Your help will be much apprechiated, thank you!
Parent Class
package test;
//imports removed for better visibility
public class ParentClass extends JPanel implements ActionListener{
JFrame frame;
public void createParentGui() {
frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel mainCard = new JPanel(new CardLayout(20, 20));
ChildClass card1 = new ChildClass();
mainCard.add(card1);
frame.add(mainCard, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed, action!");
}
}
Child Class
package test;
//imports removed for better visibility
public class ChildClass extends ParentClass {
ActionListener listener = null; //this is probably not right, how to do
//with a local variable when passing it to the parent class?
public Child() {
createGui();
}
private void createGui() {
final JButton b = new JButton("press me");
b.addActionListener(listener);
add(b);
}
}
ChildClass has all of the fields and methods that ParentClass does (in addition to its own unique fields and methods). This is how inheritance works.
So, since ParentClass is an ActionListener, that means that ChildClass is too. More specifically, ChildClass has inherited the public void actionPerformed(ActionEvent e) method of ParentClass.
Therefore, change b.addActionListener(listener); to b.addActionListener(this). (you can also remove the listener field of ChildClass)
The new code will pass "this" ChildClass object to b, which will then call actionPerformed(ActionEvent e) whenever the button is pressed. And since any ChildClass object has the actionPerformed(ActionEvent e) of ParentClass, that means that ParentClass#actionPerformed(ActionEvent) will be called (as you intended).

adding to a textfield in panel1 from a button in panel2

Ok so I have 2 jPanels.
one of them has a number of buttons that when pressed should add text to the the textfield that is in the second jPanel.
I am brand spanking new to swing with previously only having to write back end code and web based code so I am having difficulty seeing how you would accomplish this.
I only have buttons created in one panel and a textfield in another so i suspect code would be irrelevant.
Any articles that someone could point me to or examples are greatly appreciated.
So I had this problem ones,
So Lets say you have two JFrame JFrame1 and JFrame2
In order to communicate with each other at runtime both has to have most recent initialized object of each individual frame.
Now lets say this is your first frame where is your textbox,
public class JFrame1 extends JFrame{
JTextField jTextField= null;
public JFrame1() throws HeadlessException {
super("JFrame");
setSize(200, 200);
jTextField = new JTextField();
add(jTextField);
setVisible(true);
}
public void setValueToText(String value){
jTextField.setText(value);
}
}
Then This is second and where is your Button,
public class JFrame2 extends JFrame{
JButton jButton= null;
JFrame1 frame1=null;
public JFrame2() throws HeadlessException {
super("JFrame");
frame1=new JFrame1();
jButton = new JButton("Clieck Me");
add(jButton);
setVisible(true);
jButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
frame1.setValueToText("Hi");
}
});
setVisible(true);
}
public static void main(String[] args) {
JFrame2 jf= new JFrame2();
jf.setSize(200, 200);
}
}
Now Just run second class file and click one button which will set hi on your textbox which is in second frame.
So As you see answer lay's in Initialized second object in frame.
My execution is like,
Run JFrame2
Initialized JFrame1 in JFame2 const.
you can make the JTextField an instance variable of the enclosing JFrame and make the two panels inner classes of it. By this, the two panels will have a reference to the same field which belongs to the outer class.
So, you will end up having something similar to:
public class Outer extends JFrame{
private JTextField text = new JTextField();
...
public Outer(){
this.add(new Inner1(), BorderLayout.NORTH);
this.add(new Inner2(), BorderLayout.SOUTH);
}
class Inner1 extends JPanel{
...
public Inner1(){
this.add(text);
}
}
class Inner2 extends JPanel implements ActionListener{
private JButton button = new JButton();
public Inner2(){
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == button)
text.setText("Hello StackOverFlow");
}
}
}
add your code to change the text in another panel, when a button clicked in the first panel.
mybutton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//do your logic to change the text in another panel
}
});

JTextField focus

I have a frame and a panel.Permanently I remove the panel and add another panel.After adding a new panel I need the JTextField to get focused.How can I do this?
I tried panel.requestFocus() method but it didnt work.
Example Code:
public class Test{
public static void main(String[] args){
JFrame frame = new JFrame();
// ... frame options
// MyPanel extends JPanel
// and has a JTextField
contentPane.add(new MyPanel());
// Permanently I need to add another panel
contentPane.removeAll();
contentPane.add(new MyPanel());
}
}
Calling panel.requestFocus() attempts to give focus to the container itself rather than on any of its child components.
Use requestFocusInWindow on the JTextField after the component has been added to the JFrame. Add an public method in MyPanel for calling this method.
Avoid using requestFocus. From the docs:
requestFocus, is discouraged because it tries to give the focus to the component's window, which is not always possible. As of JDK 1.4, you should instead use the requestFocusInWindow method, which does not attempt to make the component's window focused.
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Focus JTextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel myPanel = new MyPanel();
frame.add(myPanel);
frame.setVisible(true);
frame.pack();
myPanel.focusTextField();
}
});
}
}
class MyPanel extends JPanel {
private JTextField textField;
public MyPanel() {
textField = new JTextField(20);
add(textField);
}
public void focusTextField() {
textField.requestFocusInWindow();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 100);
}
}
Add a method like this to MyPanel:
public void gainFocus() {
tf.requestFocus();
}
Call it from the main method, or elsewhere whenever you need it to be focused.
Use. textfield.setText(""); when you need to get the focus or try something like you will take your control to your field try
You will need a method either to get the TextField from MyPanel, like getTextField, or a method to just directly focus on the TextField. These methods must be inside your MyPanel class.
Example method:
public class MyPanel extends JPanel {
private JTextField textField;
//your code here
public void getTextFieldFocus() {
textField.requestFocus();
}
}
Then you call this getTextFieldFocus method when you need to focus.
Else, if you extract the TextField from the MyPanel class using a getTextField method, you call this when you need the focus:
panel.getTextField().requestFocus();

Swing - calling events from inside panel

I have simple Swing GUI with main window JFrame and its main panel derive from JPanel. The panel has some buttons that can be clicked and generate events.
I want these events affect data stored in JFrame because it is my main application - it has some queues for thread, open streams and so on.
So how do I make my button in panel invoke callbacks in its parent frame? What is best practice of this for Java/Swing?
To invoke methods in the parent frame you need a reference to the parent frame. So your JPanel's constructor can be declared like this:
public MyPanel(MyFrame frame){
super();
this.frame = frame;
//the rest of your code
}
And in the JFrame you invoke this constructor like this:
panel = new MyPanel(this);//this refers to your JFrame
In the event handlers attached to your buttons you now have access to the frame and can invoke the various methods as needed.
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//do some stuff
frame.someMethod();//invoke method on frame
//do more stuff
}
});
Have a look on this tutorial for using SwingWorker.
Use addActionListener method on desired buttons specifying the class implementing ActionListener.
ActionListenerClass actionListenerObject = new actionListenerClass();
JButton b = new JButton("Button");
b.addActionListener(actionListenerObject);
public class ActionListenerClass implements ActionListener(){
//or better : actionListenerClass extends AbstractAction
public void actionPerformed(ActionEvent e) {
}
}
EDIT:
Yes, I know this. But the action
listener I want to be in parent JFrame
class - this is the problem
then extends JFrame class making the new derived class implementing the desired interface.
You can implement the ActionListener in your class that has the JFrame (or extends it):
class MyPanelClass {
public MyPanelClass(ActionListener al)
{
//...
JButton myButton = new JButton("Button");
myButton.addActionListener(al);
//...
}
}
class MainClass extends JFrame implements ActionListener {
public void someMethod() {
MyPanelClass mpc = new MyPanelClass(this);
}
#Override
public void ActionPerformed(ActionEvent ev) {
// your implementation
}
}

Categories