27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Graphics, AWT, Swing II part<br />

a right click and show the popup. The problem is that i can right click on the popup<br />

labels to select them, but i'd like to select them only with a left click. How can i<br />

disable the right click on the label? Can somebody help me please?<br />

Answer: You can add in an if loop that only executes if the left mouse button is down<br />

using code like the following:<br />

MouseEvent e;<br />

if ((e.getModifiers() & e.BUTTON1_MASK) != 0)<br />

{ // code for left button click }<br />

and just so you know,<br />

InputEvent.BUTTON2_MASK is the middle or scroller button<br />

InputEvent.BUTTON3_MASK is the right button in windows<br />

-by<br />

MSW<br />

Q: How do I count the width in pixels of the text before drawing them into<br />

graphics?<br />

I have the Font and the String and I am using them in an applet:<br />

graphics.setFont(font);<br />

graphics.drawString(str);<br />

How do I count the width in pixels of the text before drawing them into graphics?<br />

Answer: import java.awt.*;<br />

Toolkit tk = java.awt.Toolkit.getDefaultToolkit();<br />

FontMetrics fm = tk.getFontMetrics(new Font("Arial", Font.PLAIN, 10));<br />

int width = fm.stringWidth("Your String");<br />

--<br />

Jarkko<br />

Q: If I do (CustomFrame extends Frame) smth. like this:<br />

Frame newFrame = new CustomFrame();<br />

what are the advantages of doing this over:<br />

CustomFrame newFrame = new CustomFrame();<br />

Answer:<br />

There is one difference.<br />

If the reference is a Frame type you can always downcast it, but not if it<br />

is a CustomFrame type.<br />

Might be nice sometimes.<br />

--<br />

Soren ' Disky ' Reinke<br />

Q: I want to know what is the difference between JScrollPane and JScrollBar...is<br />

both same or not..?<br />

Answer: No. A JScrollPane is a container for components which can grow larger than<br />

file:///F|/350_t/350_tips/graphics-II.htm (2 of 6) [2002-02-27 21:18:10]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!