Results 1 to 4 of 4
- 08-22-2012, 03:07 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
Adding a button overwriting a paintComponent
Hello, I am a first newcomer here.
I want to ask you a question.
I have a button that has an icon and an ActionListener. I want to add it to a panel GridLayout(1,1), using paintComponent cause I want to add other lines, simple move animation and such, inside also.
I use paintComponent, I add lines, rectangles, oval but when I add the button it takes up all the space and overwrites all the panel. I want to be able to animate the button itself, use its listener and in general co-exists in the panel.
I pass up my code.
The main is irrelavant as it just creates the frame and adds the panel inside.Java Code:public class MainPanel extends JPanel { private static final long serialVersionUID = 1L; protected final ImageIcon redDoorIcon = new ImageIcon(getClass().getResource("/images/RedDoor.png")); private JButton button; public MainPanel(Controller controller) { setLayout(new GridLayout(1, 1)); button = new JButton("press me"); // button.setVisible(false); add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("*"); } }); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(10, 10, 30, 10); redDoorIcon.paintIcon(this, g, 50, 50); // add(button); } }
If I don't add the button, the panel is the way I want. But I can't add it just like the other, giving it a x,j (width,height).
How can this be done?
Thank you.
- 08-22-2012, 03:51 PM #2
Re: Adding a button overwriting a paintComponent
Moved from New to Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-22-2012, 03:51 PM #3
Re: Adding a button overwriting a paintComponent
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-22-2012, 05:39 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
Re: Adding a button overwriting a paintComponent
Yes db, I made it through the links.
Although I was looking at the layout managers and overwrite paint/paintComponent, how could I figure that if I don't put a LayoutManager at all I could do exactly what I want? I'd never imagine or find it out, thanks a lot for showing that to me.
For anyone else interested, check db's link and especially
Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
Similar Threads
-
How would you go about adding text and images to a button or label?
By goatjugsoup in forum New To JavaReplies: 5Last Post: 05-21-2012, 10:33 AM -
Hibernate adding/overwriting records incorrectly
By jhasell in forum JDBCReplies: 8Last Post: 07-05-2011, 06:32 PM -
Adding a sqrt button and percentage button to a calculator
By Josie_Taylor in forum New To JavaReplies: 4Last Post: 03-14-2011, 01:16 AM -
adding flash file to a button!
By Y. Progammer in forum New To JavaReplies: 1Last Post: 03-03-2010, 03:18 PM -
Adding EXIT button on MIDlet form
By Java Tip in forum Java TipReplies: 0Last Post: 11-22-2007, 10:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks