-4

How can I access class name with respect to JFrame object ?

My source code is following

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TestClose extends methodClass implements WindowListener {

    private JFrame jfrm;

    public static void main(String[] args) {
        new TestClose();
    }

    public TestClose() {
        jfrm = new JFrame();
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.setBounds(100, 100, 450, 300);
        jfrm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        jfrm.addWindowListener(this);
        jfrm.setVisible(true);
        jfrm.getClass();
        //JOptionPane.showMessageDialog(null, jfrm.getClass().getSimpleName());
    }

    @Override
    public void windowActivated(WindowEvent evt) {}
    @Override
    public void windowClosed(WindowEvent evt) {}

    @Override
    public void windowClosing(WindowEvent evt) {
        JFrame frame = (JFrame)evt.getSource();
        int optionChoosen = JOptionPane.showConfirmDialog(frame, 
                "Are you sure you want to exit?", "Exit Application", 
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (optionChoosen == JOptionPane.YES_OPTION){
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }else if (optionChoosen == JOptionPane.NO_OPTION){
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        }
    }

    @Override
    public void windowDeactivated(WindowEvent evt) {}
    @Override
    public void windowDeiconified(WindowEvent evt) {}
    @Override
    public void windowIconified(WindowEvent evt) {}
    @Override
    public void windowOpened(WindowEvent evt) {}

}

I want to show class name "TestClose" with respect to JFrame jfrm.

schlebe
  • 3,387
  • 5
  • 37
  • 50
ArifMustafa
  • 4,617
  • 5
  • 40
  • 48
  • You already **are** getting the JFrame's class name (which is `JFrame`), so your question confuses me. What are you trying to display, and what is the reason for needing what you're needing. I smell a possible [XY Problem](http://mywiki.wooledge.org/XyProblem) where you ask how to solve a specific code problem when the best solution is to use a completely different approach. Better that you tell us the overall problem that you're trying to solve rather than how you're currently trying to solve it. – Hovercraft Full Of Eels Nov 20 '14 at 15:37
  • what i need is simple, getting current class name using 'this' keyword is easy, but with jfrm (what i need) is unknown for me? – ArifMustafa Nov 20 '14 at 18:23

2 Answers2

3

Please correct me if I'm wrong, but you appear to be asking for the name of the class that the JFrame was launched from, but as far as I understand things, this information is not automatically part of the JFrame. But having said that, if the need arises, you can associate any property with a key-value association with any Swing component that derives from JComponent via the putClientProperty(Object key, Object value) JComponent method. Unfortunately, this does not include JFrame, since it does not derive from JComponent, but it does include the JFrame's contentPane. So if you wanted to associate a JFrame (actually its contentPane) with the calling class, you could do something like this:

class ClassThatLaunchesJFrame {
   public static final Object CLASS_NAME = "class name";
   private JFrame myFrame;

   public ClassThatLaunchesJFrame() {
      myFrame = new JFrame("My Frame");
      myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      // ***** key here
      ((JPanel) myFrame.getContentPane()).putClientProperty("class name", getClass().getSimpleName());

   }

Then later in your code, if you wanted to get that String back and had a reference to the JFrame, you could always do:

  String className  = ((JPanel) myFrame.getContentPane()).getClientProperty("class name");

But still, more important to me is what this information is needed for as I still suspect that your current question as it stands is in fact a type of XY Problem, and I strongly suspect that if you try to give us an idea of what your overall problem is, not how you're trying to solve it with class names, then I can almost guarantee you a better way of solving this.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    i think, the XY Problem fact will not apply on me, well thanks it gives me a way.... – ArifMustafa Nov 20 '14 at 19:13
  • @ArifMustafa: you're welcome, but again, if you tell us the purpose of this information, the over-riding problem that is causing you to ask your question, then we'd know for sure if a better solution exists. Please consider giving us this information. – Hovercraft Full Of Eels Nov 20 '14 at 19:25
1

Try this.getClass().getSimpleName(). See this answer for more information.

Community
  • 1
  • 1
Eliza Weisman
  • 823
  • 9
  • 17
  • @ArifMustafa: you're already getting this. Again, please clarify your question. – Hovercraft Full Of Eels Nov 20 '14 at 16:33
  • what i am trying to do, that is how to get the current class name with respect to JFrame's object jfrm, and i want to get the current class name using jfrm, but not with this keyword.... – ArifMustafa Nov 20 '14 at 16:41
  • @ArifMustafa: why no use of `this`, and again, what need do you have for the class name to be displayed? Again, consider telling us the **overall** problem that you're trying to solve to get a better solution from us. – Hovercraft Full Of Eels Nov 20 '14 at 17:15
  • Sir, b'coz what i have displayed above that is for demonstrate the simple to get the class name through jframe object, but now i am using reference of jfrm in another class, and that is the reason i can't use 'this' keyword there...plz try to understand and give a solution for this... – ArifMustafa Nov 20 '14 at 17:25
  • @ArifMustafa: I am trying to understand, but again your request is very unusual. Class names are rarely ever needed, and what is more often needed are object references (and in fact if you passed a reference of the current object of interest, then you can get its class name). Again, please try to give us an idea of what your overall problem is, not how you're trying to solve it with class names. If you do this, I can almost guarantee you a better way of solving this. Please believe me that I'm really trying to help you here. – Hovercraft Full Of Eels Nov 20 '14 at 17:31