I am new to Java GWT plugins.
In our code, we are using code like below,
In test1.java
public class RowResults extends Composite
{
#UiField VerticalPanel vpnlWidgets;
public RowResults()
{
uiBinder.createAndBindUi(this));
getRows();
}
private void getRows()
{
for(RowDetails obj: RowDetailsArray)
{
RowWidget row= new RowWidget(obj);
vpnlWidgets.add(row);
}
}
}
In test2.java
public RowWidget(RowDetails rowObj)
{
uiBinder.createAndBindUi(this);
this.Row = rowObj;
}
I have posted here some necessary code. In this code, if I have 10 elements in RowDetailsArray, then for each elements createAndBindUi is called. It seems somewhat slow also.
Is there any way to call uiBinder.createAndBindUi(this); function one time and use that for all the 10 elements.
Also, what will happen while calling createAndBindUi(this). Whether it convert ui.xml file to class file or some thing else.
Pls Correct me if I am wrong.,
Thanks in Advance.
I guess it depends on how complicated your RowWidget is. If it has no special styling, I would suggest to use a FlexTable instead and just insert rows in it. It is quite optimized.
Use This Code Format.
public class RowResults extends Composite{
private static RowResultsUiBinder uiBinder = GWT.create(RowResultsUiBinder.class);
interface RowResultsUiBinder extends UiBinder<Widget, RowResults> {}
public RowResults(){
initWidget( uiBinder.createAndBindUi( this ) );
// put all your code here if you do not have any other constructor if you have put
//the above line in that constructor in the begning.
}
Related
So basically im creating a list with a lot of information that i get from the user, and i need to display that "Estudiante" created on a list asside. So this is what i first tried, but it tells me that setListData is for arrays, so i tried other thing that i found that included the using .toArray(array) but that didnt work too.
Just to clarify what modelo is i copied this first code
public class VentanaEstudiante extends javax.swing.JFrame {
private Sistema modelo;
/**
* Creates new form VentanaEstudiante
*/
public VentanaEstudiante(Sistema unSistema) {
modelo = unSistema;
this.setSize(400, 280);
initComponents();
}
private void BotonCrearEstudianteActionPerformed(java.awt.event.ActionEvent evt) {
Estudiante unEst=new Estudiante(NombreEstudiante.getText(), Integer.parseInt(CedulaEstudiante.getText()),MailEstudiante.getText(), Integer.parseInt(NumeroEstudiante.getText()), Integer.parseInt(SemestreEstudiante.getText()));
modelo.agregarEstudiante(unEst);
ListaEstudiantesJ.setListData((modelo.getListaEstudiantes()).toArray());
Estudiante has a toString method, and the superclass, also does.
public String toString(){
return super.toString() + "Numero:" + this.getNumero() + "Semestre: " + this.getSemestre();
}
Here you have my lists and i only copied the listaEstudiantes methods because this are the ones im asking right now. This class Sistema, doesnt have any toString methods because i throught that this arraylist didnt needed one.
public class Sistema {
private ArrayList<Estudiante> listaEstudiantes;
private ArrayList<Docente> listaDocentes;
private ArrayList<Equipo> listaEquipos;
public Sistema(){
listaEstudiantes = new ArrayList<>();
listaDocentes= new ArrayList<>();
listaEquipos=new ArrayList<>();
}
public void agregarEstudiante(Estudiante unEstudiante){
listaEstudiantes.add(unEstudiante);
}
public ArrayList<Estudiante> getListaEstudiantes(){
return listaEstudiantes;
}
I need to use ArrayList in case you have something that may work better, i just need to use them
This whole project has a lot of showing Lists and sometimes i have to even let the user select things from them, something that i also dont know how to do but i dont know if i can ask more than one question here. The list is also going to need to refresh and all of that but i think i can handle that. Thanks
JList.setListData() has two variants, one expecting an array of elements, the other expecting a vector of elements.
Behind the scenes these two methods create an instance of an anonymous subclass of AbstractListModel and pass that instance to JList.setModel().
You can easily implement similar code for any List instance:
static <E> void setListData(JList<E> jList, List<? extends E> listData) {
jList.setModel(new AbstractListModel<E>() {
public int getSize() { return listData.size(); }
public E getElementAt(int i) { return listData.get(i); }
});
}
Ok, while I tried to find a title that explains the problem I probably have to expand on it.
Recently I implemented a small program that will be used to control a tape library. Knowing it had to work with multiple different types of tape library so the following design was developed.
interface Tapelibrary<T extends TapeDrive> {
List<T> getListofDrives();
void doSomethingWithDrive(T d);
}
class SpecificTapeLibrary implements Tapelibrary<HPDrive> {
private List<HPDrive> driveList;
SpecificTapeLibrary() {
driveList.add(new HPDrive());
driveList.add(new HPDrive());
driveList.add(new HPDrive());
}
#Override
public List<HPDrive> getListofDrives() {
return driveList;
}
#Override
public void doSomethingWithDrive(HPDrive d) {
d.doSomethingHPspecific();
}
}
abstract class TapeDrive {
void doSomething() {
}
}
class HPDrive extends TapeDrive {
void doSomethingHPspecific() {
}
}
The correct tape library is determined by a factory based on command line arguments.
public static void main(String[] args) {
Tapelibrary<? extends TapeDrive> t = new TapeLibraryFabric().get();
List<? extends TapeDrive> listOfDrives = t.getListofDrives();
// the user selects a drive by using a small UI or something
TapeDrive selectedDrive = listOfDrives.get(0);
t.doSomethingWithDrive(selectedDrive); // compiler error
}
This does make sense since the compiler would have to explicitly cast the supertype TapeDrive to the subtype HPDrive which is expected by the doSomethingWithDrive(HPDrive) methods in SpecificTapeLibrary
How would this be solved in a good oop way? I ended up not using generics and casting inside the doSomethingWithDrive method (as suggested here:How to Pass a Child Class into a method requiring Super Class as parameter). But that can't be the optimal solution.
While writing this post another solution popped into my head which is much cleaner. The DriveSelector class encapsulates the selection process.
class DriveSelector {
<T> T selectDrive(List<T> inputList) {
// give the user an UI or something to select a drive
return inputList.get(0);
}
}
// the tape library then uses the selector
public void doSomethingWithSelectedDrive(DriveSelector selector) {
HPDrive t = selector.selectDrive(driveList);
t.doSomethingHPspecific();
}
Any other ideas?
Do all of your work in a generic method:
static <T extends TapeDrive> void doStuff(Tapelibrary<T> t) {
List<T> listOfDrives = t.getListofDrives();
// the user selects a drive by using a small UI or something
T selectedDrive = listOfDrives.get(0);
t.doSomethingWithDrive(selectedDrive);
}
Then call this from your main method:
Tapelibrary<? extends TapeDrive> t = new TapeLibraryFabric().get();
doStuff(t);
Ideone demo
The way this works is that it removes all of the wildcards - the thing about wildcards is that the compiler treats every one as different, even if the values are derived from a single generic instance. By putting things into the generic method like this, you allow the compiler to know that all of the Ts are the same type - thus it can know that the calls are safe.
I learning how to work with jooq. I would like to know if I can add some domain-level methods in to the generated Record classes.
Suppose the record was this:
public class ConCalCompanyRecord extends org.jooq.impl.UpdatableRecordImpl<com.aesthete.csmart.connect.model.db.gen.tables.records.ConCalCompanyRecord> implements org.jooq.Record6<java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String, java.sql.Timestamp, java.sql.Timestamp> {
// properties
// getters and setters
// I would like to add a method like this:
public void isABlueCompany(){
// work with the fields
}
}
But I know if I do this, as soon as I generate this class again from the DB, all my changes will get lost. So what is the recommended way of doing this?
A wrapper class? A sub class to the record? If its any of these, how do I get jooq to recognise these classes at the time of fetching. For example:
connectionFacade.getDSLContext()
.selectFrom(CON_CAL_INSTANCE)
.where(CON_CAL_INSTANCE.DATE.between(
new Date(datesOfTheWeekForDate[0].toDate().getTime()), new Date(datesOfTheWeekForDate[1].toDate().getTime())))
.orderBy(CON_CAL_INSTANCE.DATE)
.fetch()
.into(new RecordHandler<ConCalInstanceRecord>() {
#Override
public void next(ConCalInstanceRecord record) {
calendarEntries.addToList(new com.aesthete.csmart.connect.model.domain.records.ConCalInstance(record));
}
});
In the above case I am providing a wrapper called ConCalInstance to the record class. Do I have to write a RecordHandler like this for every query I execute if I need to use a wrapper?
What is the recommended way of doing this?
You can override jOOQ's default code generator with your own extensions. This is documented here, in the manual:
http://www.jooq.org/doc/latest/manual/code-generation/codegen-custom-code/
The example shows how it works:
public class MyGenerator extends JavaGenerator {
#Override
protected void generateRecordClassFooter(
TableDefinition table,
JavaWriter out
) {
super.generateRecordClassFooter(table, out);
if ("SOME_TABLE".equals(table.getName())) {
out.println();
out.tab(1).println("public void isABlueCompany() {");
out.tab(2).println("// Your logic here");
out.tab(1).println("}");
}
else if ("SOME_OTHER_TABLE".equals(table.getName())) {
// [...]
}
}
}
Based on Lukas's suggestion I landed up adding code to my generated Record like this
public class ConCalInstanceRecord extends org.jooq.impl.UpdatableRecordImpl....{
//fields and getter and setters of the generated record..
private ConCalInstanceBehaviour behaviour;
public ConCalInstanceBehaviour getBehaviour(){
if(behaviour==null){
behaviour=new ConCalInstanceBehaviour(this);
}
return behaviour;
}
}
Sort of like the wrapper like I was talking about, but the other way around, the record wraps a behaviour class. I can now add custom behaviour into my behaviour classes without having to go back to the generator every time I needed to add a new method.
This allowed me to access additional domain behaviour like this...
record.getBehaviour().doSomething();
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....
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;
}
}
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.
This is a two part question. First, is it possible use a generic defined objects method such as:
public class MyClass<T>{
public MyClass(T t){
t.setText("Hello World"); // Assume class T is JMenuIten has the special method setText
}
}
This code doesn't work as is, but show the general idea for what I'm aiming for. I want to use the methods which are particular to that encapsulated object. If however I were to pass in another object such as which contains the encapsulated method .doSomething. I would like to do ...
public class MyClass<T>{
public MyClass(T t){
t.doSomething("Hello World"); // Assume class T is JMenuIten has the special method setText
}
}
I'm hoping that it is possible to do this, otherwise I would have to write multiple constructors to take care of all my special cases.
My second question is similar in that I would like to return a GUI component and execute a statement such as ...
myJPanel.getComponent(1).setText("Hello"); // Assuming index 1 is a JLabel and setText is a specific method defined in the JLabel class
This code does not work because the compiler cannot tell ahead of time what symbols will be needed at runtime, though I was hoping that there was a way of making things like this work. I would also like to know if there is a method that can tell me what class type .getComponent() is returning if that is possible. I'm trying to make code as dynamic as possible without having to hardcode everything.
Thanks
You have to use a bounded wildcard.
e.g.
public interface MyObject {
void myMethod();
}
public class GenericObj<T extends MyObject> {
private T t;
public void invokeMethod() {
t.myMethod(); //this way you can invoke methods (declcared in MyObject) on T
}
}