Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

swing components  RSS feed

 
belle springfield
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code, which has layout managers accepting inputs,
can you please give some insight on what would be the other approach if i want the inputs to be accepted thru a jsp instead?
Thanks

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JDialog;
import javax.swing.JFrame;

import sun.awt.HorizBagLayout;
import sun.awt.VerticalBagLayout;

public class TimeInTimeOutApp extends JFrame
{
static TextField timeInHour = null;
static TextField timeInMin = null;
static TextField timeOutHour = null;
static TextField timeOutMin = null;
final static int HOUR = 0;
final static int MIN = 1;
static Point p = null;
static Dimension myDim = null;
public TimeInTimeOutApp()
{
super();
init();
}

public void init()
{
setCustomSize();
setLabels();
addComponents();
pack();
setVisible(true);
}

public void setLabels()
{
setTitle("Time In Time Out");
}

public void setCustomSize()
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
myDim = new Dimension(dim);
myDim.setSize(dim.getWidth() / 5, dim.getHeight() / 5);
double centerW = dim.getWidth() / 2;
double centerH = dim.getHeight() / 2;
double centerX = centerW - dim.getWidth() / 10;
double centerY = centerH - dim.getHeight() / 10;
p = new Point((int) centerX, (int) centerY);
setSize(myDim);
setLocation(p);
}

public void addComponents()
{
this.addWindowListener(createExiter());
Container cpmain = this.getContentPane();
cpmain.setLayout(new VerticalBagLayout(10));
Panel cp = new Panel();
cp.setLayout(new HorizBagLayout(2));
cp.add(createLabel("Time In "));
timeInHour = createTimeTextField(null, HOUR);
timeInMin = createTimeTextField(null, MIN);
cp.add(timeInHour);
cp.add(createLabel(":"));
cp.add(timeInMin);
Panel cpout = new Panel();
cpout.setLayout(new HorizBagLayout(2));
cpout.add(createLabel("Time Out "));
timeOutHour = createTimeTextField(null, HOUR);
timeOutMin = createTimeTextField(null, MIN);
cpout.add(timeOutHour);
cpout.add(createLabel(":"));
cpout.add(timeOutMin);
cpmain.add(cp);
cpmain.add(cpout);
Panel cpbut = new Panel();
cpbut.setLayout(new FlowLayout(FlowLayout.CENTER));
cpbut.add(createButton(" OK ", createActionListener()));
cpmain.add(cpbut);
}
public WindowListener createExiter()
{
return new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
};
}
/**
* @return
*/
private JDialog createDialog(String message)
{
JDialog dial = new JDialog(this, true);
dial.setTitle(" MESSAGE ");
Container cp = dial.getContentPane();
cp.setLayout(new VerticalBagLayout(10));
cp.add(new Label(" "));
cp.add(new Label(" " + message + " "));
cp.add(new Label(" "));
dial.setLocation(p);
dial.setSize(myDim);
dial.pack();
dial.setVisible(true);
return dial;
}

public Panel createPanel()
{

Panel panel = new Panel();
return panel;
}

public Button createButton(String label, ActionListener al)
{
Button b = new Button(label);
b.addActionListener(al);
return b;
}

public ActionListener createActionListener()
{
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (submit())
{
System.exit(0);
}
}
};

return al;
}

public boolean submit()
{
return save();
}

public Label createLabel(String label)
{
return new Label(label);
}

public TextField createTextField(String label)
{
return new TextField(label);
}
public TextField createTimeTextField(String label, int type)
{
final int finalType = type;
if (label == null)
{
label = "";
}
final TextField tf = createTextField(label);
tf.setColumns(2);
TextListener tl = new TextListener()
{

public void textValueChanged(TextEvent e)
{
String timeInput = tf.getText();
if (timeInput != null && timeInput.trim().length() > 0)
{
int len = timeInput.length();
if (len > 2)
{
timeInput = timeInput.substring(0, 2);
tf.setText(timeInput);
}
try
{
int time = Integer.parseInt(timeInput.trim());
if (finalType == HOUR)
{
if (time > 23)
{
adjust(String.valueOf(time));
}
}
else
{
if (time > 59)
{
adjust(String.valueOf(time));
}
}
}
catch (Exception except)
{
adjust(timeInput);
}
}
}
void adjust(String timeInput)
{
char[] x = timeInput.toCharArray();
if(!Character.isDigit(x[0])||x[0]!=' ')
{
if(!Character.isDigit(x[1])||x[1]!=' ')
{
x[0] = x[1];
x[1] = ' ';
}
}
if(!Character.isDigit(x[1])||x[1]!=' ')
{
x[1] = ' ';
}

tf.setText(new String(x));
tf.setCaretPosition(timeInput.length());
}
};
tf.addTextListener(tl);
return tf;
}

public static void main(String[] args)
{
TimeInTimeOutApp timeInTimeOutApp = new TimeInTimeOutApp();
}

private boolean save()
{
String inHour = timeInHour.getText();
String inMin = timeInMin.getText();
String outHour = timeOutHour.getText();
String outMin = timeOutMin.getText();
if (!(checkValid(inHour, inMin, outHour, outMin)))
{
createDialog(" Not enough hours. Should be 8 hours at least. ");
return false;
}

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
Connection conn = null;
try
{
conn = DriverManager.getConnection(
"jdbc racle:thin:@10.24.208.228:1521:ICP42", "ibpipcdev",
"ibpipcdev");
}
catch (SQLException e2)
{
e2.printStackTrace();
}

Statement stmt = null;
try
{
if (conn != null)
{
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO TEMP_IO VALUES(TO_DATE('"
+ inHour + "-" + inMin + "', 'HH24-MI'),TO_DATE('"
+ outHour + "-" + outMin + "','HH24-MI'))");
conn.commit();
stmt.close();
conn.close();
}
else
{
createDialog("Cannot make a connection. ");
return false;
}
}
catch (SQLException e)
{
createDialog("Cannot save date. The format may be incorrect!");
return false;
}
catch (Exception e)
{
e.printStackTrace();
createDialog("Cannot save date for some reason. ");
return false;
}
return true;
}

/**
* @param inHour
* @param inMin
* @param outHour
* @param outMin
* @return
*/
private boolean checkValid(String inHour, String inMin, String outHour,
String outMin)
{
int inh = Integer.parseInt(inHour);
int inm = Integer.parseInt(inMin);
int outh = Integer.parseInt(outHour);
int outm = Integer.parseInt(outMin);
if(outh-inh > 8)
{
return true;
}
else if(outh-inh==8)
{
if(outm >= inm)
{
return true;
}
}
return false;
}

}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of suggestions:

1 - use the "code" tags (see the FAQ) to preserve code formatting when posting.
2 - repost this in the right forum - this a forum for pasting JDBC questions. Just because your code briefly uses JDBC doesn't mean its a JDBC question.

I can't promise anyone will help, but you stand a better chance if you do these two things.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"bullets doublezero",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JSP forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing and JSP are vastly different technologies and the amount of code that could be retained is minimal.

JSP is a technology to dynamically create HTML pages to send to a browser. So your input mechanisms are limited to what is allowed by HTML form elements.

You might want to investigate the use of Applets if using Swing on the web is your goal.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you familiar with the Model View Controller (MVC) pattern? The main idea is to keep presentation and data separate. If you use the MVC pattern in your Swing app you may find that you can reuse the non-display elements in a JSP unchanged. You will have to completely reimplement the presentation logic. As Bear says, there are quite a few limitations on what you can do in a web page compared to in a Swing app. You have to relinquish even more control over how your UI will appear.

I'm planning to do try and implement the UI for my SCJD project in JSP once I've completed it in Swing.

Jules
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!