Javax.Swing; How can I create an object of a different class and assign variables received from the jframe ? Conceptual issue - swing

I am working for the first time with javax.swing and jframes, so please excuse me if you find this question primitive.
Problem: In my main function I have created an object of a class lets say ClassTest. So the code goes like:
import TestPackage.ClassTest.*;
public class Qinterface extends JFrame and implements ActionListener
{
public string Login;
public static void main(String[] args){
ClassTest test = new ClassTest();
try{ eventqueue invoker ...}catch{}
}
Qinterface(){
setResizable(false);
setTitle("Carrefour : Qualys Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 100, 850, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txtEnterText = new JTextField();
txtEnterText.setText("Enter Qualys Login");
txtEnterText.setBounds(10, 193, 166, 23);
contentPane.add(txtEnterText);
txtEnterText.setColumns(10);
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent eSubmit)
{
//button is pressed
System.out.println("You clicked the button Submit");
Login = txtEnterText.getText();}});
}
}
So as seen in the last line of the code I am able to get the value from the txtEnterText field and assign to a local variable "Login". But how do i go about if I want to assign this value to an instance of a class created in the main function, for example;
test.x=txtEnterText.getText();
I know its not possible in this approach as we are in the constructor the Qinterface class and the variable of the ClassTest instantiated in the main are not visible.
So the question is general and conceptual; how do you go about such kinds of problems to resolve them, when coding with javax.swing ?

Using a login process as an example:
Your interface class could hold a "LoginData" object that is populated by the action listener. By providing a getter for the data object, the login data can then be accessed from outside of the interface.
This is just one of the many ways you could tackle this problem.
This would be a good candidate for MVC architecture - you can read about it a little here.

Related

Creating a new component with Castle Windsor providing the DI

I'm new to Windsor and am trying to get my head around how to do this in a standalone client (WPF in my case).
I have a class called a PictureWrapper that uses a PictureClient, like this:
public class PictureWrapper
{
private PictureClient myClient;
public PictureWrapper(int clientID)
{
myClient = new PictureClient();
//etc
}
}
In my PictureWrapper project, I can create a WindsorInstaller class that looks like this:
public class WindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<IPictureClient>().ImplementedBy<PictureClient>().LifestyleTransient());
}
}
Assuming I call the Install() method above beforehand, here's where I get a little lost-- how exactly do I now create the PictureWrapper class in my WPF application? Do I still new() it up?
And given I'm not passing an instance of the PictureClient class in the PictureWrapper's constructor, how do I initialize it when creating the PictureWrapper?
I could, I suppose, make it a constructor parameter but when I create the PictureWrapper in the client, what do I pass to its constructor in my client? I'm trying to figure out how to get the DI ball rolling from my client, essentially.

Flash AS3 Dyanmic Text keeps giving an error 1119

So I have a method that takes in a String and then is suppose to set the dynamic textbox on a button to said String.
public function setText(caption:String) {
this.btext.text = caption;
}
I really don't understand why this method is producing a 1119 error.
Access of a possibly undefined property btext through a reference with static type Button.as
The instance name of the Dynamic Textbox is btext and I have tried deleting the textbox and making a new one however this still produces a 1119 error. I also read on another stack question that trying this['btext'].text = caption; which gave me plenty of runtime errors.
Basically what am I doing wrong?
Thank you for any help.
EDIT
Here is the code I am using, and I create an instance of button add it to the stage and store it in an array with this code.
Code to create button
this.buttonArray.push(this.addChild(weaponButton));
Button.as
package {
import flash.display.MovieClip;
import flash.filters.*;
public class Button extends MovieClip {
public function Button() {
}
public function setPosition(xpos:int, ypos:int) {
this.x = xpos;
this.y = ypos;
}
public function setScale(xScale:Number, yScale:Number) {
this.scaleX = xScale;
this.scaleY = yScale;
}
public function addDropShadow():Array {
var dropShadow:DropShadowFilter = new DropShadowFilter(2,45,0, 1,4,4,1,1,true);
return [dropShadow];
}
public function removeDropShadow():Array {
return null;
}
public function setText(caption:String) {
this.btext.text = caption;
}
}
}
As you have stated btext is an instance name of an object. Here is where I assume btext is an object you created in your library.
In your class you are doing 2 things wrong. So lets examine your method.
public function setText(caption:String) {
this.btext.text = caption;
}
The first thing wrong is you are using "this". "this" is a reference to the current instance of the class you are in. And you are saying btext is a property on said instance. Which as I am assuming it is not because you defined btext as an object in your library. This will give you the property is undefined error you are gettting.
Now the second issue at hand is you are about to ask "OK how do I reference btext in my class then". What you need to know is that only objects added to the display list IE:stage can access objects via the stage.
You can do this 3 ways.
The first way is to pass a reference to the button into the class and store it as a property of the class.
The second way is to add your class to stage and in the class listen to the addedToStage event. At that time you can then access the object.
MovieClip(root)["btext"].text
The first 2 methods are not good practice since btext is not apart of the class and a general rule of thumb would be to encapsulate your class.
To make this work what you could do is have your class assign the value to a property in your class then fire an event and make the parent of this class listen to that event then just grab the value and assign.
Here is some suggested reading
I think the variable btext doesn't exist at all, or is it inherited from Movieclip?

AS3 access class instance from everywhere

for my current project I am starting to work with AS3 and I have written a ClipManager class where I can define an MC like "mainView" during initialization like this:
clipManager:ClipManager = new ClipManager(mainView);
With my clipManager I can now easily load stuff into the mainView etc. The problem is that I want every button throughout the whole thing to access Class Methods of this instance to alter the mainView. Can I have something like a global Class instance in Flash or is there any smarter way to achieve what I am trying to do?
You can either add your ClipManager class as a static somewhere - i.e. a god object - (perhaps your main class) and access it through that, or you can use the Singleton pattern.
A common way to implement it in as3:
public class Singleton
{
private static m_instance:Singleton = null; // the only instance of this class
private static m_creating:Boolean = false;// are we creating the singleton?
/**
* Returns the only Singleton instance
*/
public static function get instance():Singleton
{
if( Singleton.m_instance == null )
{
Singleton.m_creating = true;
Singleton.m_instance = new Singleton;
Singleton.m_creating = false;
}
return Singleton.m_instance;
}
/**
* Creates a new Singleton. Don't call this directly - use the 'instance' property
*/
public function Singleton()
{
if( !Singleton.m_creating )
throw new Error( "The Singleton class can't be created directly - use the static 'instance' property instead" );
}
}
Now, to access your class, you call Singleton.instance. There'll only ever be one instance of this class.
As for anti-patterns etc, well that's another post :)

How to bind a component to more beansbinding classes

Any one know how to bind one swing JComponent to two BeansBinding classes(Specially with Netbeans IDE)? Also how to bind a variable in JFrame to a beanbinding class's property?
A) Hmm ... still not sure what exactly you want to achieve: build a binding chain, maybe? Something like
bean."name" <--> textField."text" --> otherBean.logger
BindingGroup context = new BindingGroup();
context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
bean, BeanProperty.create("name"),
field, BeanProperty.create("text")));
context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
field, BeanProperty.create("text"),
otherBean, BeanProperty.create("logger")));
B) Beansbinding is all about binding properties not fields (aka: variables). So whatever you want to bind needs a getter and (maybe a setter, depends on your requirements) and must fire notification on change. Then do the binding just as always ..
public MyFrame extends JFrame {
private int attempts;
public int getAttempts() {
return attempts;
}
private void incrementAttempts() {
int old = getAttempts();
attempts++;
firePropertyChange("attempts", old, getAttempts());
}
private void bind() {
BindingGroup context = new BindingGroup();
context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
this, BeanProperty.create("attempts"),
label, BeanProperty.create("text")));
}
}

How to reference objects in AS3?

I've learned one way to do that, but I want to improve my knowledge. For simplicity I'm not going to use import neither extends in the code below.
1
public class Main
{
public function Main()
{
new MyCustomObject(stage);
}
}
2
public class MyCustomObject
{
public var referenceStage:Stage = new Stage();
public function MyCustomObject(xxx:Stage)
{
this.referenceStage = xxx;
referenceStage.addChild(this);
}
}
I've learned it reading a tutorial over internet, but I want to know where I can find more samples on how to reference objects in AS3. For future codes, I want to add hitTest and the like.
Thanks !
The best place is the ActionScript 3 Reference from Adobe: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html
Here is the specific section on objects: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html
if you absolutely want to pass a stage reference through an argument to a constructor, you can do so about how you have it laid out (although get rid of the new Stage() call, which won't do anything).
that said, .stage is a property available to all display objects that are in the display list (meaning: the have been added via addChild or addChildAt).
you're probably getting that error trying to reference a .stage property of an object before it's been added to the display list. this is a common error, and can be handled by waiting to reference the .stage property until it has been added, usually using addEventListener(Event.ADDED_TO_STAGE...
so instead of
public class MyObject extends Sprite {
public function MyObject():void{
this.x = this.stage.stageWidth/2;
}
}
you'd use something like this
public class MyObject extends Sprite {
public function MyObject():void{
this.addEventListener(Event.ADDED_TO_STAGE, this.addedHandler, false, 0, true);
}
private function addedHandler(e:Event):void{
this.x = this.stage.stageWidth/2;
}
}
HTH
In your example, you don't need do call new Stage() in your CustomObject
public var referenceStage:Stage;
is enough
A hitting function may be found here http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/
Possible solutions are:
Instead of passing the stage object, you can also pass the main object and calling functions in the main object for the custom object
Maintain an array in the MainObject with which you want do collisions test.
Implementing an Interface (extend an object) with a function which do the hit test agains the array in the MainObject (for example went the EntreFrame Event is fired)
Custom Events are the solution for communicating with the main object loosely
Passing a reference to an object in the constructor is a classic OOP pattern