made circular buttons, need help with ToolTipText
hi folks,
I have 2 questions:
1.i want to make a program which will go on adding buttons to my JPanel as and when a button is clicked. i can't figure out how to do it.
2.i have made circular buttons which i have then added to my JPanel.
the circular buttons have been made using just an extension of JComponent
now when i call setToolTipText(), it works fine with my RoundButton. however, when i move away from my RoundButton, the area of the RoundButton gets repainted, but the tool tip still remains visible on the JPanel. now i understand it is a case of not repainting the JPanel after a RoundButton loses focus. but my question is, when do i repaint my JPanel?
[704 byte] By [
m_kadiaa] at [2007-11-26 17:16:27]

# 1
1. In action of button add code for creating button and adding it to needed componet, for example myPanel, and then call myPanel.validate(); and myPanel.repaint();
2. Try to extend not JComponet, extend JButton it less difficult and may be it remove all your problems, if not, than type your RoundButton code here for checking.
# 2
1. I m not sure you have refreshing problem or adding components problem. If you don't know how to add new buttons on the panel, what you should do is like:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AddButton extends JPanel implements ActionListener {
JButton button = new JButton ("original");
public AddButton () {
this.setLayout(new FlowLayout());
button.addActionListener(this);
this.add(button);
}
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog(this, "Adding new button");
JButton newButton = new JButton ("new button");
newButton.addActionListener(this);
this.add(newButton);
this.validate();
}
public static void main (String args []) {
JFrame frame = new JFrame ();
AddButton ab = new AddButton();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(ab, BorderLayout.CENTER);
frame.setBounds(100, 100, 300, 300);
frame.setVisible(true);
}
}
remember to call this.validate()
when you are adding new component to a container.
It's a bit complex if you want buttons to do different things, you'd have to add different action listeners to them respectively.
2. I've not done the same thing, but i did try to extend JComponent to create customized components. There is not problem with my object to display/hide ToolTipText. Try to repaint the parent object using super.paint(g);
or you just make sure that you correctly override public void paintComponet(Graphics g)
, and the code in the method is not wrong.
or if you are sure the problem is caused by the container refreshing, you can call circularButton.getParent().repaint()
, but i don't think this is the case.
hope it help
cheers
Message was edited by:
siab
siaba at 2007-7-8 23:44:30 >

# 3
thanks siab, your code is very interesting. and somehow the repainting problem solved itself. i just had to setBackground(Color.WHITE), and it all fell into place
about my second question: what i am trying to make is a graph, whose nodes will all be buttons.
so i need those buttons to be part of an array of buttons.yet, it is an array whose size i do not know before runtime; its size increases as and when an event is triggered for creating a new button. so any suggestions on how i go abt doing that?
# 4
to solve this problem, i would like to use a Map or a List, i prefer Map because of its retrieving ability. On the other hand, the size of an Array can be defined after the program starts, so i can't see any problem in your program.
If the number of buttons is infinite(unknown at any time), then you get to use Map or List.
am i making myself clear about your question?
cheers
siaba at 2007-7-8 23:44:30 >
