0

I am trying to create a custom class that extends another class (JFrame) but forces the assignment of a certain variable upon implementation (I want each JFrame in my application to have a "screen ID"). Java, however, does not have abstract variables. Nor am I able to figure out how to make an interface that extends JFrame. This one's really got my head spinning =D

The class code would look similar to this:

public interface CustomFrame extends javax.swing.JFrame {
    public abstract int screen_id;
}

And the implementation of CustomFrame would look something like this:

public class NewFrame implements CustomFrame {
    public int screen_id = 5;
    NewFrame() {
        setVisible(true);
        // etc...
    }
}

Does this problem even make sense to anyone?? I know what my objective is I am just lost trying to work it out in my brain....

ryvantage
  • 13,064
  • 15
  • 63
  • 112
  • 2
    1) Why would you even want to extend JFrame? 2) It's as if your program will have multiple JFrames, which it really shouldn't. I think that you might want to re-think your overall program design. – Hovercraft Full Of Eels Aug 23 '13 at 22:22
  • @HovercraftFullOfEels, thanks for the suggestion. You can read my $.02 about that here: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/17961122#17961122. – ryvantage Aug 23 '13 at 22:27
  • Make the frame abstract, require implementations to implement an abstract method – MadProgrammer Aug 23 '13 at 22:43
  • interface is the contract you can't extends an interface with concrete implementation... i don't know what you want to achieve, i didnt understand but i think with a `Map` you get what you want.. – nachokk Aug 23 '13 at 23:29
  • why do you need to id the _view_ (vs. the backing data model)? – kleopatra Aug 24 '13 at 10:18

2 Answers2

0

Create an external FrameID generator that generate a new ID at each call (just a simple example implemented as singleton: no synch or generic return type etc):

class FrameIDGenerator {
  private static int nextID = 0;
  private FrameIDGenerator(){}
  private static FrameIDGenerator instance = null;

  public final static FrameIDGenerator getInstance() {
    if(null == instance) instance = new FrameIDGenerator();
    return instance;
  }

  public int getNextID() {
    return ++nextID;
  }
}
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • this doesn't work for my solution because my screen_ids are written to a database. So, the screen_id really needs to be a serial # that only and always refers to one `JFrame` and that `JFrame`'s screen_id never changes, ever. – ryvantage Aug 24 '13 at 02:41
0

From what I am understanding, it is a 3rd party (not JFrame itself) who needs the number. I don't think it's a good idea to try to force JFrame (who already has a lot of concerns) into this burden.

Please consider the alternative of keeping an external IdentityHashMap<JFrame,Integer> to store this information.

Mario Rossi
  • 7,651
  • 27
  • 37
  • that is currently what I'm already doing. But it leaves a lot of room for careless mistakes, especially when things change. What I was hoping for was some sort of "contract" based numbering, i.e., where every `JFrame` was contractually bound to contain a screen_id – ryvantage Aug 24 '13 at 02:38
  • Actually, I don't use an `IdentityHashMap` because identity is established on an equals (`==`) basis, whereas what I need is for classes (not instances) to be equal to each other. So what I use is a `DualLinkedHashBidiMap`. – ryvantage Aug 24 '13 at 03:29