Help! -How Launch Jframe from Jsp so modal

[nobr]Hello,

I'm trying to launch a JFrame from a Jsp page but the code either continues without showing the JFrame though a validation check indicates the frame isVisible OR the page never loads on call. For the code shown below, the jsp page first displays a button to launch the frame. On clicking the button the same jsp is called to launch the frame. On clicking the button however the page appears to keep loading without ever finishing. All help is appreciated!!

my Jsp code is as follows:

<%@ page language="java" contentType="text/html;charset=UTF-8" import="com.plumtree.remote.portlet.*,edu.columbia.iri.app.projects.*" %>

<%@page import="javax.swing.*" %>

<%

// Prefs page for ProjectsPortlet. Let the user select default projects to list.

%>

<%

//Check if the form was submitted

if(request.getParameter("Set")!=null && request.getParameter("Set").equals("1111"))

{

request.setAttribute("action","prefDisplay");

request.setAttribute("orderby","title");

ServletContext jc = getServletContext();

DualListBox dual =new DualListBox(request,(String)jc.getAttribute("db.driver"), (String)jc.getAttribute("db.connectionstring"),"title");

if(f.isShowing()){

System.out.println("done breathing in WPg" + request.getAttribute("ProjectIDs").toString());//}

}

elseif (request.getParameter("action")!=null && request.getParameter("action").equalsIgnoreCase("urlDisplay"))

{

request.setAttribute("action","urlDisplay");

request.setAttribute("orderby","title");

//Send to ProjectsController

request.getRequestDispatcher("pc").forward(request, response);

}

else{

%>

<html>

<head>

<title>Set Project Preferences Button Page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="set projects">

<meta http-equiv="description" content="Displays Button to Set Project Preferences">

<!--<link rel="stylesheet" type="text/css" href="styles.css">-->

</head>

<body>

<br>

Click on the button below to select projectsfor display.

<br>

<form name="setform" id="setform" action="SetProjects.jsp">

<input type="hidden" value="1111" name="Set">

<input type="submit" value="Set Projects">

</form>

</body>

</html>

<%

}

%>

The class being called to launch the frame, called DualListBox.java is as follows:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Frame;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.lang.reflect.InvocationTargetException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collection;

import java.util.Iterator;

import java.util.SortedSet;

import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import javax.swing.AbstractListModel;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.ListCellRenderer;

import javax.swing.ListModel;

publicclass DualListBoxextends JPanel{

/**

*

*/

privatestaticfinallong serialVersionUID = 1;

privatestaticfinal Insets EMPTY_INSETS =new Insets(0, 0, 0, 0);

privatestaticfinal String ADD_BUTTON_LABEL ="Add >>";

privatestaticfinal String REMOVE_BUTTON_LABEL ="<< Remove";

privatestaticfinal String DONE_BUTTON_LABEL ="Submit!";

privatestaticfinal String DEFAULT_SOURCE_CHOICE_LABEL ="Available Projects";

privatestaticfinal String DEFAULT_DEST_CHOICE_LABEL ="Your Selections";

private String orderby, mydriver, connectionString;

private JLabel sourceLabel;

private JList sourceList;

private SortedListModel sourceListModel;

private JList destList;

private String chosenprojects;

private SortedListModel destListModel;

private JLabel destLabel;

private JButton addButton;

private JButton removeButton;

private JButton doneButton;

private DatabaseHelper dh;

protected HttpServletRequest request;

protected JFrame f;

protected JDialog jd;

public DualListBox(HttpServletRequest req, String driver, String connection, String ordering){

System.out.println("In DualList Setup");

request =req;

orderby =ordering;

connectionString = connection;

mydriver = driver;

f =new JFrame("Projects List Selector");

jd =new JDialog(f,true);

jd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

System.out.println("B4 initscreen");

initScreen();

System.out.println("After initscreen");

String[] DBprojects = this.dbCall();

System.out.println("After DB Call");

this.addSourceElements( DBprojects );

System.out.println("Filled screen");

jd.getContentPane().add(this, BorderLayout.CENTER);

//f.getContentPane().add(jd, BorderLayout.CENTER);

jd.setSize(800, 600);

System.out.println("OK1");

//jd.pack();

System.out.println("OK2");

jd.setVisible(true);

System.out.println("OK3");

//Runnable runner = new FrameShower(jd);hangs cout;nullpter with

//EventQueue.invokeLater(runner);

}

public String getSourceChoicesTitle(){

return sourceLabel.getText();

}

public String chosenprojects(){

return chosenprojects;

}

public JFrame getFrame(){

return f;

}

publicvoid setSourceChoicesTitle(String newValue){

sourceLabel.setText(newValue);

}

public String getDestinationChoicesTitle(){

return destLabel.getText();

}

publicvoid setDestinationChoicesTitle(String newValue){

destLabel.setText(newValue);

}

publicvoid clearSourceListModel(){

sourceListModel.clear();

}

publicvoid clearDestinationListModel(){

destListModel.clear();

}

publicvoid addSourceElements(ListModel newValue){

fillListModel(sourceListModel, newValue);

}

publicvoid setSourceElements(ListModel newValue){

clearSourceListModel();

addSourceElements(newValue);

}

publicvoid addDestinationElements(ListModel newValue){

fillListModel(destListModel, newValue);

}

private String[] dbCall(){

if(dh==null)

dh =new DatabaseHelper(mydriver, connectionString);

PreparedStatement ps =null;

ResultSet rs =null;

ArrayList<String>children =new ArrayList<String>();

ArrayList<String[]>tree =new ArrayList<String[]>();

if(orderby==null || orderby.equals("")){

orderby ="region";

}

String query ="select title,id from projects";// order by " + orderby;

System.out.println(query);

//System.out.println(orderby);

try{

Connection conn =dh.getConnection();

ps = conn.prepareStatement(query);

rs = ps.executeQuery();

while (rs.next()){

children.add(new String(rs.getString(1)));

System.out.println(rs.getString(1));

tree.add(new String[]{rs.getString(1),rs.getString(2)});

}

request.setAttribute("ResultTree",tree);

return (String[])children.toArray(new String[children.size()]);

}catch (SQLException e){

thrownew RuntimeException(e);

}finally{

try{

if (null != rs) rs.close();

}catch (SQLException e){

;

}

try{

if (null != ps) ps.close();

}catch (SQLException e){

;

}

}

}

privatevoid fillListModel(SortedListModel model, ListModel newValues){

int size = newValues.getSize();

for (int i = 0; i < size; i++){

model.add(newValues.getElementAt(i));

}

}

publicvoid addSourceElements(Object newValue[]){

fillListModel(sourceListModel, newValue);

}

publicvoid setSourceElements(Object newValue[]){

clearSourceListModel();

addSourceElements(newValue);

}

publicvoid addDestinationElements(Object newValue[]){

fillListModel(destListModel, newValue);

}

privatevoid fillListModel(SortedListModel model, Object newValues[]){

model.addAll(newValues);

}

public Iterator sourceIterator(){

return sourceListModel.iterator();

}

public Iterator destinationIterator(){

return destListModel.iterator();

}

publicvoid setSourceCellRenderer(ListCellRenderer newValue){

sourceList.setCellRenderer(newValue);

}

public ListCellRenderer getSourceCellRenderer(){

return sourceList.getCellRenderer();

}

publicvoid setDestinationCellRenderer(ListCellRenderer newValue){

destList.setCellRenderer(newValue);

}

public ListCellRenderer getDestinationCellRenderer(){

return destList.getCellRenderer();

}

publicvoid setVisibleRowCount(int newValue){

sourceList.setVisibleRowCount(newValue);

destList.setVisibleRowCount(newValue);

}

publicint getVisibleRowCount(){

return sourceList.getVisibleRowCount();

}

publicvoid setSelectionBackground(Color newValue){

sourceList.setSelectionBackground(newValue);

destList.setSelectionBackground(newValue);

}

public Color getSelectionBackground(){

return sourceList.getSelectionBackground();

}

publicvoid setSelectionForeground(Color newValue){

sourceList.setSelectionForeground(newValue);

destList.setSelectionForeground(newValue);

}

public Color getSelectionForeground(){

return sourceList.getSelectionForeground();

}

public String getProjects(){

return chosenprojects;

}

privatevoid clearSourceSelected(){

Object selected[] = sourceList.getSelectedValues();

for (int i = selected.length - 1; i >= 0; --i){

sourceListModel.removeElement(selected[i]);

}

sourceList.getSelectionModel().clearSelection();

}

privatevoid clearDestinationSelected(){

Object selected[] = destList.getSelectedValues();

for (int i = selected.length - 1; i >= 0; --i){

destListModel.removeElement(selected[i]);

}

destList.getSelectionModel().clearSelection();

}

privatevoid initScreen(){

setBorder(BorderFactory.createEtchedBorder());

setLayout(new GridBagLayout());

sourceLabel =new JLabel(DEFAULT_SOURCE_CHOICE_LABEL);

sourceListModel =new SortedListModel();

sourceList =new JList(sourceListModel);

add(sourceLabel,new GridBagConstraints(0, 0, 1, 1, 0, 0,

GridBagConstraints.CENTER, GridBagConstraints.NONE,

EMPTY_INSETS, 0, 0));

add(new JScrollPane(sourceList),new GridBagConstraints(0, 1, 1, 5, .5,

1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,

EMPTY_INSETS, 0, 0));

addButton =new JButton(ADD_BUTTON_LABEL);

add(addButton,new GridBagConstraints(1, 2, 1, 2, 0, .25,

GridBagConstraints.CENTER, GridBagConstraints.NONE,

EMPTY_INSETS, 0, 0));

addButton.addActionListener(new AddListener());

removeButton =new JButton(REMOVE_BUTTON_LABEL);

add(removeButton,new GridBagConstraints(1, 4, 1, 2, 0, .25,

GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(

0, 5, 0, 5), 0, 0));

removeButton.addActionListener(new RemoveListener());

doneButton =new JButton(DONE_BUTTON_LABEL);

add(doneButton,new GridBagConstraints(1, 6, 1, 2, 0, .25,

GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(

0, 10, 0, 10), 0, 0));

doneButton.addActionListener(new DoneListener());

f.addWindowListener(new java.awt.event.WindowAdapter(){

publicvoid windowClosing(WindowEvent winEvt){

//could set to null here to force use of Done button only

chosenprojects = destList.getSelectedValues().toString();

request.setAttribute("ProjectIDs", destList.getSelectedValues().toString());

System.exit(0);

}

});

destLabel =new JLabel(DEFAULT_DEST_CHOICE_LABEL);

destListModel =new SortedListModel();

destList =new JList(destListModel);

add(destLabel,new GridBagConstraints(2, 0, 1, 1, 0, 0,

GridBagConstraints.CENTER, GridBagConstraints.NONE,

EMPTY_INSETS, 0, 0));

add(new JScrollPane(destList),new GridBagConstraints(2, 1, 1, 5, .5,

1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,

EMPTY_INSETS, 0, 0));

}

privateclass AddListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

Object selected[] = sourceList.getSelectedValues();

addDestinationElements(selected);

clearSourceSelected();

}

}

privateclass RemoveListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

Object selected[] = destList.getSelectedValues();

addSourceElements(selected);

clearDestinationSelected();

}

}

privateclass DoneListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

chosenprojects = destList.getSelectedValues().toString();

request.setAttribute("ProjectIDs", destList.getSelectedValues().toString());

System.exit(0);

}

}

}

class FrameShowerimplements Runnable{

final JDialog frame;

public FrameShower(JDialog frame){

this.frame = frame;

}

publicvoid run(){

System.out.println("B4 make visible");

frame.setVisible(true);

System.out.println("Made screen visible");

}

}

class SortedListModelextends AbstractListModel{

/**

*

*/

privatestaticfinallong serialVersionUID = 8777627817685130496;

SortedSet model;

public SortedListModel(){

model =new TreeSet();

}

publicint getSize(){

return model.size();

}

public Object getElementAt(int index){

return model.toArray()[index];

}

publicvoid add(Object element){

if (model.add(element)){

fireContentsChanged(this, 0, getSize());

}

}

publicvoid addAll(Object elements[]){

Collection c = Arrays.asList(elements);

model.addAll(c);

fireContentsChanged(this, 0, getSize());

}

publicvoid clear(){

model.clear();

fireContentsChanged(this, 0, getSize());

}

publicboolean contains(Object element){

return model.contains(element);

}

public Object firstElement(){

return model.first();

}

public Iterator iterator(){

return model.iterator();

}

public Object lastElement(){

return model.last();

}

publicboolean removeElement(Object element){

boolean removed = model.remove(element);

if (removed){

fireContentsChanged(this, 0, getSize());

}

return removed;

}

}

[/nobr]

[31256 byte] By [bbrza] at [2007-11-27 4:42:44]
# 1
JFrame from JSP? Are you sure you want to do that?
Hippolytea at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 2

You can't. JSP's java code is processed on the server, there's no way to make it load a frame or other UI on the client side. You can only display HTML on the client (typically). Your HTML can include a Java applet, but that would run totally independently from the JSP code.

The JFrame probably would show, just on the server.

bsampieria at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 3
The java code on your jsp page runs on the server, so there's no way for it to open a frame on the client. All the client gets is the html output of the servlet, not java code.
hunter9000a at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 4
Well, that code sample was lovely.The mixing of client-server & desktop applications was not.What exactly are you trying accomplish? What are your requirements?
filestreama at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 5
Like any HTML, your JSP page can have an applet on it, but there are no specialhooks to make an applet play nice with JSP/servlets. <cue the big, salty tears/>
Hippolytea at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 6

Thanks for the feedback guys. I'm pretty new at this so I'm glad to get some help.

I'm trying to get input from users (in the form of selections from a given database list of choices) and then pass those selected choices in the form of strings to other java classes on the backend. The selected strings are names of projects so the backend classes extract those project names from a postgres database and display the selected projects in a tabular format with additional attrinutes (like a google search snippet)such as:

P1 name

P1 image

P1 description

P2 name

P2 image

P2 description.

The idea was to use a Jframe to get the user slections and then get my controller class to call the model class passing the selections through the request object.

Is there a better way to do this? Please advise.

Thanks!!

bbrza at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 7
Use a form on the JSP page. How much experience do you have coding JSPs?
Hippolytea at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 8
- This is mean to be a web application so a web approach is what I'm really seeking.
bbrza at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 9
Not a whole lot..
bbrza at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 10
It's time for you to do some reading! There are lots of books and tutorials out there.You may also want to consider using a web application framework,which complicates life in the short turn but simplifies it long term.
Hippolytea at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 11
Can you point me to a good tutorial on the form use?
bbrza at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...
# 12
http://java.sun.com/javaee/5/docs/tutorial/doc/
Hippolytea at 2007-7-12 9:54:23 > top of Java-index,Java Essentials,Java Programming...