• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

setVisible(true) not working properly in web application  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a code which is executing correctly(gragh is displayed) when I run as a standalone through Command Prompt. But when I place the same code in a servlet, I am not getting the graph displayed.

The code is as follows,

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class semilog extends JPanel {
double[] data;
public semilog(){
}
public semilog(double[] data){
this.data=data;
}
final int PAD = 20;


protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int w=getWidth();
int h = getHeight();

// Draw ordinate.
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));

// Draw abcissa.
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));

//some other code

}



public void draw(double[] data) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new semilog(data));
f.setSize(400,400);
f.setLocation(200,200);
f.repaint(1);
f.setVisible(true);
System.out.println(f.isVisible());
System.out.println(f.isShowing());

}

}


When I call this code from a servlet, f.isVisible() and f.isShowing() are returning true but no graph is displayed. But when I run through Command Prompt, it is working.

Can anyone help me with this?
 
Sheriff
Posts: 21503
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't run Swing code in a servlet. Well, perhaps you can, but the actual displaying of the Swing components won't work. You'll need an applet for that, or use a different way for showing the graph.

The good news is, an image can work:
Of course you can use "image/jpeg" and "JPEG" instead of PNG if you prefer JPG images.

Moving to Servlets.
 
chandana nannapaneni
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Rob. Thanks a lot. The code is working.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!