PDA

View Full Version : Java Help


Rubu
March 1, 2005, 11:38 PM
Just switching to java, and still dont know many staff. here is small problem, if someone can give me a solution. their must be an easier way to do it, but dont know how.

i've two frame, frame1 is the main frame, frame2 will come up when a button in frame1 is pressed. while frame2 is on, the button on frame1 will not generate frame2 again. and closing frame2 will go back to frame1. this actions can be repeated. the problem i'm having is, as many times i keep pressing the generating button, a new frame2 keeps coming up.

thanks in advance.

here is the code i've:

public void JButton1_actionPerformed(ActionEvent e){
Frame2 f = new Frame2();
f.validate();
f.show();
}

the "new" is causing the trouble. and Frame2.show() gives syntax error.

many not be needed, still, i'm using Borland JBuilder 2005 Enterprise Edition.



Edited on, March 2, 2005, 4:44 AM GMT, by AgentSmith.

AsifTheManRahman
March 1, 2005, 11:40 PM
ektu matha ghuracche. can u re-explain? or post the code? let me see what i can do.

AsifTheManRahman
March 1, 2005, 11:40 PM
ok i think i got what you said. sorry about that. posting the code will be helpful nevertheless. and yes, it probably doesnt matter which IDE you're using here. :)

Edited on, March 2, 2005, 4:41 AM GMT, by AsifTheManRahman.

Rubu
March 1, 2005, 11:48 PM
code posted, atmh. thanks.

AsifTheManRahman
March 2, 2005, 02:00 PM
hmm. looks like you're opening frame2, but not preventing it from opening when one version of frame2 is already open. f.show() should display frame2, but where does it say in the code that it should not be displayed once there's already a frame2 open?

btw - can you post the exact errors you're getting, and the code for the method call?

AsifTheManRahman
March 2, 2005, 02:07 PM
btw - i'm not very familiar with frames and windows (or anything visual for that matter, unless you're talking about web development tools) so i'm not sure how much i may be able to help you.

Nasif
March 3, 2005, 12:03 AM
Before you create new Frame2(), have a global flag variable verification. If flag is unset create new Frame2() then set the flag. After frame2() is closed/destroyed unset that variable again.

Something like this:
<font size=3><pre>
if (flag == 0) {
Frame2 f = new Frame2();
flag=1;
f.validate();
f.show();
}
</pre></font>

and then in the close/destroy method the last operation should be: flag=0;

That will ensure that your frame2 isn't recreated if its already active. I don't know if its the best way (not a java expert) but it is one way.

By the way, I think NetBeans (http://www.netbeans.org/downloads/index.html) is much better IDE for Java (and its free :) ).

Edited on, March 3, 2005, 5:06 AM GMT, by nasif.

Rubu
March 3, 2005, 12:33 AM
nasif vai, thanks for the help, i tried that, but the problem here is, i can't unset the flag. i mean, when frame two is closed it should make the flag =0 again. what is the way to do that?

one thing i found is:

setDefaultCloseOperation(flag=0);


the problem is, if i do that, the frame does not close anymore. and i can't use it twice so:
setDefaultCloseOperation(flag=0);
setDefaultCloseOperation(frame2.HIDE_ON_CLOSE);
only executes the first line, doesnot matter which one i put first. so, it will either make the flag zero or close, don't know how to do both.

btw, jbuilder is free too ;) and its more fun to use a professional compiler free than to use a free one.

Rubu
March 3, 2005, 12:36 AM
i found it!!!!

just declear the frame outside the stub of the jButton, right at the beginning of

public class Frame1 extends JFrame{......

that way, when i press the jbutton, it will not create a new frame, but will reshow the same frame already created.

AsifTheManRahman
March 3, 2005, 09:48 AM
hahaha....basic mistake that all java beginners make! nothing to worry about! :)

cricman
March 13, 2005, 03:43 PM
use Jcreator its the best

Rubu
March 21, 2005, 08:48 PM
once again, i'm stuck: this time a bit more complicated. see if anyone can give some tips.

overveiw: i'm writting a software to operate a device (an RFID). it operates in bitwise command. i need to send a packet of binary (=hex) number through rs232 serial port.

problem: the packet has to be in the grup of 8 bits (1 byte). i did it in C, they have this "unsigned char" that has 8 bit. problem with java is, its char is 16 bits(2 bytes). i tryed to use "byte" type variable in java, but i can't. because i can't assign something like

byte var1=0xFF; i'm not able to change the Most sig bit to 1.
and if i'm using a bigger variable say char or int, my device will not take it since they are padded with a huge line of zeros. what can i do? any idea?

in short, i need 8 bit of space where i can use all eight.

Zunaid
March 21, 2005, 08:56 PM
read this:

http://darksleep.com/player/JavaAndUnsignedTypes.html

Rubu
March 21, 2005, 09:21 PM
thanks doc.

how did u find it so quick? i spent some time looking for something like this but did not find any. have u had it read before? in this case, one more question for u (this is just out of curisity, i can use that method anyway). why bother doing all this, is not it be same if we do this?

int a=0x000000FF;
b=(byte)a;

if not, why?

Edited on, March 22, 2005, 2:43 AM GMT, by AgentSmith.

Nasif
March 22, 2005, 01:10 AM
Won't work because the moment you change it into byte type most sig bit can't be modified. You will always need higher type to store lower type ( word to store byte, long to store word etc). Thats why all operations need that byte mask (0xFF).

Not a java guru, but looking at the problem that seems to be the reason. I think C/C++ is the best programming for hardware interface :)

Rubu
March 22, 2005, 02:13 AM
yes, the thing i'm trying to do has done in C several times. but when we choose the language for our project, we wanted GUI+database+hardware interface. fast two are harder in C. VB was not an option because we needed it platform free. i'll give it a try, if does not work, i'll have to find a way to run an internal program written in C and get the data from text files or something like that.