• Post Reply Bookmark Topic Watch Topic
  • New Topic

Paint in swing  RSS feed

 
raddikrishna kittur
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ,
If I have JButtons and in the program i use paint() then the JButtons are hidden from view .Please help me .
KK
 
dharini thyagaraj
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
you've to use the paincomponent method and not paint for painting swing components
warm regards,
dharini
 
raddikrishna kittur
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi dharini,
following is my code .when i call repaint() it is not painting the message.even directly calling paintComponents (Graphics g) is also not of help. Please help .
KK

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code = "JButtonDemo" width = 400 height = 400>
</applet>*/
public class JButtonDemo extends JApplet implements ActionListener{
Graphics g;
JButton b1 , b2;
public void init(){

Container cp =getContentPane();
cp.setLayout(new FlowLayout());

//g.drawString("Hello",50,25 );
b1 = new JButton();
b2= new JButton(" "+456);
b2.setBackground(Color.cyan);
b2.setForeground(Color.red);
b2.addActionListener(this);
cp.add("Center",b1);
cp.add("South",b2);
}
public void start(){
g= getGraphics();

paintComponents(g);

//repaint();

}


public void actionPerformed(ActionEvent ae){
g= getGraphics();

draw1(g);
}

public void paintComponents(Graphics g){
g.drawString("Welcome to swing", 250, 250);
}
public void draw1(Graphics g){
g.drawString("Welcome ", 250, 150);
}
}
 
sashkamal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raddikrishna kittur:
KK [/B]

hi,
call super.paint in ur paint method.that will solve the problem._
sashkamal.
------------------
sashkamal
 
Nuzhar Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u r doing a basic mistake u set flowlayout but r using consttraint of borderlayout
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!