I want to have a list of objects with different objects.
I want a super class that has an abstract getOptions() method. Then I can create subclasses of the super class and when you select a object of the subclass it will make buttons for each option of in the subclass.
I want to be able to choose the options buttons will be made for. Like if I made a subclass that represented a file it would have two options like open and edit and the program would automatically make a open and edit button, but it also needs to support other subclasses that have different options and the program would make buttons for them instead.
It don't matter if it's a button, I just wanna know how I can get a list of all options in a subclass.
Have the child class return an instance of an Action instead. The actions can be put into a menu or toolbar as needed to form JMenuItem and JButton instances.
You can create an Option class and hold just a List<Option> variable in your entity classes like file and whatever...
But maybe I did misunderstand what you really want.
If you want list of your objects' methods or properties you can use Java Reflection. However I'm not sure if this is the best solution to implement your idea.
Related
I am new to stackoverflow and am sorry, if this question was already asked, but when I searched for one containing my problem, I could not find one.
Here is my question:
In Java I am creating a game (just for fun and am learning to code). You start my game with a launcher to log in. However, if you do not have an account, you can register. What I like more is to use one frame. I have a panel containing the launcher elements and one containing the register elements. I am trying to learn how to code, if another one is working on my project, he does not necessarily need to look all over the code to do some work. In other words, I am using packages to sort classes. Example: Classes for the launcher are in package Launcher. Classes for register are in package register.
In my code, I have a class called Display extending JFrame. Display is instanciated in the class with the main method. Display has 2 Methods for removing a panel and adding a panel, requiring you to pass a JPanel, if you use any of both methods. After Display is instantiated, it instantiates a jpanel inside the constructor. This has all the components to Display. The event listeners are in another class. So I am passing the buttons and Display to that class, because here is the eventlistener code for the button register. In my register class I have defined and instantiated all necessary components. I passed the display through all of the classes until it reached the register class which extends jpanel. I even passed the Launcher class. Now i can use the methods in display to remove the launcher panel and add my register panel.
My Question:
is this good code or overkill?
I never instantiated Display after the main method again. I always declared it and set it to the passed display. I did this, because instantiated a new Display would mean unnecessary use of memory and passing objects in java is actually a passing by references, meaning it is not passing the object but a pointer to the object. So this would mean less memory usage.
I'm making a button class that handles input and drawing by itself; the only thing that needs to be defined is the location and what happens when the button is pressed.
In this situation, would it be better to have a ButtonPressListener interface and have it as a parameter in Button's constructor, or should the Button be abstract with the abstract method pressed()?
The resulting initiation code for Button would be like the following:
new Button(x,y,new ButtonPressListener(){
#Override
protected void pressed(){
// code
}
});
or
new Button(x,y){
#Override
protected void pressed(){
// code
}
};
Also, in other similar situations, what should be considered when choosing between the two approaches?
Thanks.
I prefer the listener.
Resons:
The listener will give you more flexability, when using java8 lambdas.
You can write one class that listens to several buttons
You can write one class that listens to a button and inherits some other class
By the way: You should consider using a setter, rather then a parameter of the constructor. This will allow to create buttons without listeners - or define more than one listener. Also parameters are a little bit harder to read then setter, as parameters cannot have names in java.
If you are trying to learn from this project, you might as well do both at the same time and find out what works better for you. Wenn you found out, refactor and throw out the less liked option.
Make a default implementation of Button.pressed() that calls the function of your listener implementation if set. Supply two constructors, one that sets the listener and one that does not.
Of course that is not an option is others shall use this API.
So I'm having one super class, Block, that extends Composite and uses the UIBinder to make the layout
class Block extends Composite
I want to create two subclasses for that one, that each have different set of icons that have to be added. For example an InactiveBlock and an ActiveBlock.
My problem here is that I want the layout of both blocks (the icons, and some buttons,labels) to be made through the UIBinder aswell, and then to add that UIBinder (and it's events) to be added to the main Block.
Obviously I can't do something like
class ActiveBlock extends Block, Composite
add(initWidget(UIBinder.create(this)));
How could I accomplish this?
(ps if my question is not clear enough, please do tell so I can elaborate)
I would make it a single class with a constructor method having boolean as an input parameter (active/inactive).
So you can define all the common fields and methods in the class, like event handlers, images, etc.
And then use the constructor method to add the elements and handlers to the basic widget. Something will be added to all instances, something depending on whether it's active or not.
In this case you won't end up with duplicated code, still will have a benefit of using uibinder, and eventually your code will be simple enough for reading.
Everyone knows the Ctrl+Space shortcut in Eclipse but I got one question on that.
If you use this shortcut, you'll get a list of visible methods and fields. e.g.:
JTable table = new JTable(dataModel);
table.<Ctrg+Space>
.. and you'll get a list of JTable methods, JComponent ones etc. So inherited methods are listed as well. Sometimes this list is very long...
My question is: Does Eclipse offer an option to hide the inherited methods? So I just get a list of methods of this specific class? E.g. when I use..
table.<Ctrg+Space>
..I'll get a list of JTable methods and the JComponent ones won't be listed.
Hope there is a way. To search in a method-result-list won't help, I think.
Cheers!
Am not sure how to exactly achieve this, but if you place your cursor on JTable and press
Ctrl+F3
You should able to see the list of methods that are present only on Jtable.
i have a Table with JComboBoxes and want to add aPropertyChangeListener to every single JComboBox, because some selections of ComboBoxes have to change the selectables of other JComboBoxes.
I can't add all those listeners manually because there are very much of them.
I'm initializing the ComboBoxes with an array, so i already tried to add the listener when I create the JComboBox like this:
comboBox[i].addPropertyChangeListener(new PropertyChangeListener()
But it didnt work because the field variable i is not final and I need this variable.
How can I store this variable in the comboBox or is there a other possibility to solve this Problem?
If you can create all those comboboxes, then you can also add 'all those listeners' manually. There are several options:
You create a new listener each time you create a new combobox, and pass that index i to that listener (either by anonymous class, inner class, or fully fledged class) or by making a final copy as Francis Upton suggested in his answer
If you need that i only to retrieve the combobox from which the event originated, you can also call event#getSource (which is available on both the ActionEvent as well as on the PropertyChangeEvent since your question is not clear about the type of listener). In this case you can either create the listener only once, or create one listener for each combobox
You can extend JComboBox and init what you want in constructor
In your loop you can copy i to another final variable, and refer to that final variable in your ActionListener.
Instead of using an anonymous class, make a real class that implements the interface you care about. That way you can pass the combobox index (or even the combobox instance if that is all you need).