Ko Wey

Ranch Hand
+ Follow
since Sep 08, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ko Wey

May be you could take some time to make a (abstract) generic custom button ( with the method paintComponent(g) abstract) and each time you want a special button, you extend this generic button: you just have to tweek paintComponent(g) to whatever your fantasy dictates. This is not working, this is art and fun!

KEEP IT IN A SAFE PLACE!!!

Here a ovalbutton:



And here and imageButton

18 years ago
Mr Raghavendra nandavar, this is what the example tried to demostrate:
in paintComponent(Graphics g) one can just as easily put images (g.drawImage(some image,...))
or paint lines, ellipses, rectangles,...
Mouselistener's mouseClicked method to fire an Actionevent, which is what one normally expects from a button... MouseListeners mousePressed and mouseReleased were used to show the user he touched the custom button
18 years ago
Try something like this:


//file Ellipse.java

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class EllipseButton extends JPanel
implements MouseListener{

private String title = null;
private Vector listeners = null;
private boolean hit = false;

public EllipseButton (String title){
super();
this.title = title;
listeners = new Vector();
addMouseListener(this);
}

public Dimension getPreferredSize(){return new Dimension(150,75);}

public void paintComponent(Graphics g){
Graphics2D g2D = (Graphics2D)g;
super.paintComponent(g);
if (hit==true){
g2D.setColor(Color.green);
}else{
g2D.setColor(Color.yellow);
};
g2D.fillOval(0,0,getWidth()-2,getHeight()-2);
g2D.setColor(Color.black);
g2D.drawOval(0,0,getWidth()-2,getHeight()-2);
g2D.drawString(title,10,getHeight()/2);
}

public void mousePressed(MouseEvent e){
hit=true;
repaint();
}

public void mouseReleased(MouseEvent e){
hit=false;
repaint();
}

public void mouseClicked(MouseEvent e){
fireEvent(new ActionEvent(this,0,title));
}

public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

public void addActionListener(ActionListener listener){
listeners.addElement(listener);
}

public void removeActionListener(ActionListener listener){
listeners.removeElement(listener);
}

private void fireEvent(ActionEvent event){
for (int i = 0;i<listeners.size() ;i++ ){
ActionListener listener = (ActionListener)listeners.elementAt(i);
listener.actionPerformed(event);
};
}

public static void main(String[] args){//TEST
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
EllipseButton button = new EllipseButton("Special button");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("I am clicked!");
}
});
Container cont = jFrame.getContentPane();
cont.setLayout(new FlowLayout());
cont.add(new JLabel("TEST ME:"));
cont.add(button);
jFrame.pack();
jFrame.setVisible(true);
}

}//end class

18 years ago
A question for the staff/customers of this excellent saloon.
Consider a JScrollPane. When the content resizes (gets very much smaller), the JScrollPane does not resize, and that's what I want. So I think that I'm going to be clever and I make an extra JScrollPane that does just that:


It works fine...if I have only one of this custom JScrollPane-things. When I have more, I get a StackOverFlowError! The poison, I think, lies in the line getParent().validate(); or does it?

Thanks for your consideration.
18 years ago
Hi,

I always construct my JLabel (empty or not) when initializing the gui ( in the constructor)
and then later, when I want to show something: myJLabel.setText("My text")!

Seems better.

Ko
18 years ago


... nesting panes inside of panes ...




That is the best way, Paul!
18 years ago
Perhaps you get extra marks if you do something like this:
Locale locale = Locale.CANADA;//or wherever you are
String string =
NumberFormat.getCurrencyInstance(locale).format(<your float> );

123.45678 should read like this: $123.46
18 years ago
The main thing is: you must create a gui, which you can if you "master" AWT/SWING.

Your gui-classes won't start by clicking on it! Never click on classes.

If you want to start your application by clicking, you can do it
OR
by stuffing them in a jar etc

OR by making a batch-file ("name-of-class.bat in Windows")
with this entry:

java <name-of-class-with-main-in>
(or whatever you typed after the command-prompt...)

which is sometimes easier when your application is still in the testing-phase!Click on the batch-file.

Happy clicking!
18 years ago
Mr Albrechtsen's reply is of course very appropriate, for the benefit of Mr De Maio, I comment my previous given code:

InputStream (System.in)reads bytes, so less suitable for text and numbers,
we need something to read characters: a Reader (in my code a bufferedReader to read as efficiently as possible)
An InputStreamReader is a bridge from byte streams (read be InputStream)to character streams(read by Reader)

Like a plumber, we fix the pipes one after the other:
InputStream is = System.in;//read bytes
InputStreamReader isr = new InputStreamReader(is);//bytes to characters
BufferedReader br = new BufferedReader(isr);//read and store characters

the last thing "reads a line of text", and stops when it encounters <ENTER>
Then we try to extract of double from what is read...

Happy reading!
18 years ago
We cannot indeed guess what your gui should look like: it's hidden in the vaults of your phantasy. Did you try it on paper?


Do your coding more step4step: it will help you to rearrange if things don't suit you. Using comment and indents will keep your witts together.

Perhaps something like this will suit you, although I think the centerbuttons will not be your fancy....


18 years ago
A question on this topic:
jars that are in .../jre/lib/ext are supposed to be found automatically, right?

I have one pc where that's the case, another one where it isn't. Never understood why!
18 years ago
Woww!
As a father of a 16 year old, I must say this is
a dedicated prof! Poor kid!
18 years ago
Hi, try something like this:
18 years ago
I am lazy! Very lazy!


private JButton button1,button2,button3,button4,button5,button6,
button7,button8,button9,button10,button11,button12,
button13,button14,button15,button16,button17,button18,
button19,button20,button21,button22,button23,button24,
button25,button26;

private JButton[] buttons = {button1,button2,button3,button4,button5,button6,button7
,button8,button9,button10,button11,button12,button13,button14,button15,
button16,button17,button18,button19,button20,button21,button22,
button23,button24,button25,button26};


I think I would write
private JButton[] buttons = new JButton[25];

and in my code any reference to say the fifteenth button as
buttons[14] instead of button15 would be ok to me. But, as I said, I am lazy.
18 years ago
Mr. Reddy,

your main could look like this:


Every class you create is a subclass of Object. You inherit the methods of this class Object. Some of these you can "rewrite" (override), some you cannot because they are declared as final. getClass() is final, so you cannot override that method, as the compiler reminds you. So remove that method from your class, recompile and you can than read what the compiler thinks of your class' methods.... (hint: if you declare that your method will return an integer, you are obliged to do just that...)

May I suggest you pick up a good book on java, like (Headfirst java)
http://www.oreilly.com/catalog/hfjava2/
18 years ago