This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
chandana nannapaneni
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?
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!