Related
this is my Applet so far, i canĀ“t figure out how to open a new window (JFrame), when i click on "Register" JButton, i want to open a new window "Register Form Window" is to register all users (User Name, User Password, Age, etc).
LogIn.java
package intranetmailingsystem;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
public class LogIn extends javax.swing.JApplet{
#Override
public void init() {
try {
java.awt.EventQueue.invokeAndWait(this::initComponents);
} catch (InterruptedException | InvocationTargetException ex) {
}
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
jLabel1.setText("Intranet Mailing System");
jLabel2.setText("User name");
jLabel3.setText("Password");
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});
jButton1.setText("LogIn");
jButton2.setText("Clear");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Register");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jButton1)
.addGap(48, 48, 48)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3))
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(49, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addGap(65, 65, 65))
);
}// </editor-fold>
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// need to add some code here !
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
{
jTextField1.setText("");
jTextField2.setText("");
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
private void dispose() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
InternetMailingSystem.java :
package intranetmailingsystem;
public class IntranetMailingSystem
{
public static void main(String[] args)
{
// TODO code application logic here
}
}
Please check this:
Image
You have to create another window. So if you create another JFrame class call Register you would add this in your code.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Register menu = new Register();
menu.setVisible(true);
}
I want to display string in textbox of vpsfame class below my code, but I am unable to dispaly it. Can you give me suggestion this string in new8 class show in code and serial class gives me string from gps continue. It also displays it at run time via System.out.println, but does not display it through the textbox of vpsfame class
package vpspro;
import javax.comm.*;
import java.util.*;
import java.io.*;
public class Serial
{
public void com() throws UnsupportedCommOperationException, IOException, TooManyListenersException
{
int c=1;
String wantedPortName="COM6";
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
while(portIdentifiers.hasMoreElements())
{
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
pid.getName().equals(wantedPortName))
{
portId = pid;
break;
}
}
if(portId == null)
{
System.err.println("Could not find serial port " + wantedPortName);
System.exit(1);
}
else
{
System.out.println("system find gps reciever");
}
SerialPort port = null;
try {
port = (SerialPort) portId.open(
"RMC",
1);
System.out.println("all are ok");
} catch(PortInUseException e) {
System.err.println("Port already in use: " + e);
System.exit(1);
}
port.setSerialPortParams(
4800,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
BufferedReader is = null;
try {
is = new BufferedReader(new InputStreamReader(port.getInputStream()));
System.out.println("data is ok");
} catch (IOException e) {
System.err.println("Can't open input stream: write-only");
is = null;
}
String pt=null;
while(true)
{
String st = is.readLine();
System.out.print("(" + c + ")");
c++;
new8 obj1 = new new8();
obj1.decode(st);
System.out.println(st);
st = st.replace(st, "");
}
if (is != null) is.close();
/*if (os != null) os.close();*/
if (port != null) port.close();
}
}
this code for start communication with serial class
package vpspro;
import java.io.IOException;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.UnsupportedCommOperationException;
public class getcon extends javax.swing.JFrame {
public getcon() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
sc = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
br = new javax.swing.JComboBox();
help = new javax.swing.JButton();
ok = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("select comport");
sc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12" }));
sc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
scActionPerformed(evt);
}
});
sc.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
scInputMethodTextChanged(evt);
}
});
jLabel2.setText("select buad rate");
br.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "4800", "1900", "150000" }));
br.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
brActionPerformed(evt);
}
});
help.setText("help");
ok.setText("ok");
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(sc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(105, 105, 105))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(171, Short.MAX_VALUE)
.addComponent(ok)
.addGap(28, 28, 28)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(help)
.addComponent(br, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(br, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(help)
.addComponent(ok))
.addContainerGap(118, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void brActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==br)
br.addItem("4800");
br.addItem("9600");
}
private void scActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==sc)
sc.addItem("COM6");
sc.addItem("COM7");
}
private void scInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==sc)
sc.addItem("COM6");
sc.addItem("COM7");
}
private void okActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==ok)
{
try {
new Serial().com();
} catch (UnsupportedCommOperationException ex) {
Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
} catch (TooManyListenersException ex) {
Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
}
new new8();
}
}
public void getcom() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new getcon().setVisible(true);
}
});
}
private javax.swing.JComboBox br;
private javax.swing.JButton help;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
public javax.swing.JButton ok;
private javax.swing.JComboBox sc;
}
this code decoding my gps string and dispaly and hear problem created i can not dispaly below string in textbox when i rum program textbox are empty.
package vpa.io.*;
import vpspro.vpsfame;
public class new8
{
static String[] a2;
static String a1[];
static String c;
public static String[] t;
public void decode(String st)
{
if(st.isEmpty())
{
System.out.println("System has proble");
vpsfame obj=new vpsfame();
obj.ab.setText("ok");
}
else
{
a1=st.split("\\$",0);
a2=a1[1].split("\\*",0);
checksum();
if(a2[1].equalsIgnoreCase(c))
{
t=st.split(",",0);
switch (t[0].charAt(0))
{
case '$':
String s=t[0].substring(3,6);
String s3="RMC";
if (s.compareTo(s3)==0)
{
System.out.println(" NMEA 0183 Details.....");
System.out.println("System identity: "+t[0].substring(1,3));
System.out.println("NMEA Formate: "+t[0].substring(3,6));
System.out.println("(UTC)Time: "+t[1].substring(1,2));
new vpsfame().ac.setText(t[1].substring(1,2));
System.out.println("Lattitude: "+t[3]);
System.out.println("Direction of Lattitude: "+t[4]);
new vpsfame().ab.setText(t[3].concat(t[4]));
System.out.println("Longitude: "+t[5]);
System.out.println("Direction of Longitude: "+t[6]);
new vpsfame().bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground: "+t[7]);
new vpsfame().cd.setText(t[7]);
/*System.out.println("Track mode: "+t[8]);
System.out.println(st.isEmpty());
System.out.println("DATE: "+t[9]);
System.out.println("adjustment declination "+t[10]);
System.out.println("Direction of Magnetic variation: "+t[11].charAt(0));*/
}
}
else
System.out.println(" Invalid Formate...");
break;
default:
System.out.println(" Invalid DATA....");
}
}
else
System.out.println(" no match checksum");
}
}
public static void checksum()
{
int l=a2[0].length();
char[] a=a2[0].toCharArray();
int b=0;
for(int i=0;i<l;i++)
{
b=b^a[i];
}
c=Integer.toHexString(b);
}
}
and this is my main class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* vpsfame.java
*
* Created on Jan 26, 2013, 11:02:32 PM
*/
package vpspro;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.TextField;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.UnsupportedCommOperationException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
/**
*
* #author DHARMA
*/
public class vpsfame extends javax.swing.JFrame {
public vpsfame()
{
initComponents();
}
/** Creates new form vpsfame */
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
ab = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
bc = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
ac = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
cd = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
gc = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
lm = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem7 = new javax.swing.JMenuItem();
jMenuItem8 = new javax.swing.JMenuItem();
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
ab.setEditable(false);
ab.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
abActionPerformed(evt);
}
});
jLabel1.setText("latitude");
jLabel2.setText("longitude");
bc.setText(" ");
bc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcActionPerformed(evt);
}
});
jLabel4.setText("UTC");
ac.setText(" ");
ac.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
acActionPerformed(evt);
}
});
jLabel5.setText("Speed of vessel");
cd.setText(" ");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 621, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 368, Short.MAX_VALUE)
);
jMenu1.setText("File");
gc.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
gc.setIcon(new javax.swing.ImageIcon("C:\\Users\\DHARMA\\Desktop\\png\\20x20\\55.png")); // NOI18N
gc.setText("get connection");
gc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
gcActionPerformed(evt);
}
});
jMenu1.add(gc);
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setText("track");
jMenu1.add(jMenuItem2);
lm.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK));
lm.setText("load map");
lm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lmActionPerformed(evt);
}
});
jMenu1.add(lm);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem4.setText("data save");
jMenu1.add(jMenuItem4);
jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.META_MASK));
jMenuItem5.setText("set time zone");
jMenu1.add(jMenuItem5);
jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem6.setText("quit");
jMenu1.add(jMenuItem6);
jMenuBar1.add(jMenu1);
jMenu2.setText("view");
jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
jMenuItem7.setText("saved data");
jMenu2.add(jMenuItem7);
jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
jMenuItem8.setText("about us");
jMenu2.add(jMenuItem8);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(19, 19, 19))
.addGroup(layout.createSequentialGroup()
.addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))
.addComponent(ab, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(jLabel3)
.addComponent(ac, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(jLabel4)
.addComponent(bc, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ab, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(16, 16, 16)
.addComponent(bc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(ac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(45, 45, 45)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void gcActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==gc)
{
getcon obj3=new getcon();
obj3.setVisible(true);
}
}
private void lmActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void abActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void acActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bcActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) throws UnsupportedCommOperationException, IOException, TooManyListenersException {
/*public vpsfame()
{
initComponents();*/
new new8();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
/*URL url=getClass().getResource("C:\\Users\\DHARMA\\Documents\\NetBeansProjects\\vpspro\\src");*/
vpsfame obj4=new vpsfame();
new vpsfame().setBackground(Color.yellow);
obj4.setSize(1000,
500);
ImageIcon image1=new ImageIcon("C:\\Users\\DHARMA\\Documents\\NetBeansProjects\\vpspro\\src");
System.out.println("jjdfhj");
obj4.setIconImage(image1.getImage());
obj4.setTitle("VPS");
obj4.setVisible(true);
new new8();
}
});
}
/*public vpsfame()
{
fm=new JFrame("VPS");
fm.setSize(1000, 1000);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}*/
// Variables declaration - do not modify
public static javax.swing.JTextField ab;
public javax.swing.JTextField ac;
public static javax.swing.JTextField bc;
public javax.swing.JTextField cd;
private javax.swing.JMenuItem gc;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JPanel jPanel1;
private javax.swing.JMenuItem lm;
// End of variables declaration
}
I would seriously advise you to take a time out, and refactor all of your code. Igonoring that in the class new8, in method decode replace this code:
new vpsfame().ac.setText(t[1].substring(1,2));
System.out.println("Lattitude: "+t[3]);
System.out.println("Direction of Lattitude: "+t[4]);
new vpsfame().ab.setText(t[3].concat(t[4]));
System.out.println("Longitude: "+t[5]);
System.out.println("Direction of Longitude: "+t[6]);
new vpsfame().bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground: "+t[7]);
new vpsfame().cd.setText(t[7]);
With this:
//use one object, dont just create new frames
vpsfame myFrame = new vpsfame();
myFrame.ac.setText(t[1].substring(1,2));
System.out.println("Lattitude: "+t[3]);
System.out.println("Direction of Lattitude: "+t[4]);
myFrame.ab.setText(t[3].concat(t[4]));
System.out.println("Longitude: "+t[5]);
System.out.println("Direction of Longitude: "+t[6]);
myFrame.bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground: "+t[7]);
myFrame.cd.setText(t[7]);
//Also set the frame to visible, so it can be displayed once you've done all this work.
myFrame.setVisible(true);
The problem is that:
Firstly, you are creating multiple vpsframe objects where you should only have one. It makes no sense to create 5 frames where each will have 4 empty fields. Create one object and initialise all it's fields rather than using 5 diffrent objects.
You are not tracking the objects you are creating, the garbage collector will just get rid of the, so create one object, assign it to a variable and use that.
Set the vpsfame object to visible, otherwise you will never see it.
Your code also needs refactoring, you should be using the camelCase naming convention, and classes should always begin with Capital letters.
I am trying to build a java project using Netbeans IDE 7.1.
I somehow can't see or view the GUI window that I have created.
Please kindly advise.
In my class:
package rmiSimpleCalc;
public class RMISimpleCalculatorMain {
public static void main(String[] args) {
MainCalculator calc = new MainCalculator();
calc.setVisible(true);
}
}
the MainCalculator is the GUI window I would like to run. It somehow wont't display.
There is NO error message in my console tough..
Here is the MainCalculator code:
package rmiSimpleCalc;
import java.rmi.*;
public class MainCalculator extends javax.swing.JPanel {
public MainCalculator() {
initComponents();
ComboBoxOperator.addItem("+");
ComboBoxOperator.addItem("-");
ComboBoxOperator.addItem("/");
ComboBoxOperator.addItem("*");
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
txtFirstDigit = new javax.swing.JTextField();
txtSecondDigit = new javax.swing.JTextField();
btnCalculate = new javax.swing.JButton();
lblFirstDigit = new javax.swing.JLabel();
lblSecondDigit = new javax.swing.JLabel();
ComboBoxOperator = new javax.swing.JComboBox();
lblOperator = new javax.swing.JLabel();
lblResult = new javax.swing.JLabel();
lblHeader = new javax.swing.JLabel();
btnConfigureServer = new javax.swing.JButton();
txtResult = new javax.swing.JTextField();
btnCalculate.setText("Calculate");
btnCalculate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalculateActionPerformed(evt);
}
});
lblFirstDigit.setText("First Digit");
lblSecondDigit.setText("Second Digit");
ComboBoxOperator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
lblOperator.setText("Operator");
lblResult.setText("Result");
lblHeader.setText("RMI Simple Calculator");
btnConfigureServer.setText("Configure Server");
btnConfigureServer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConfigureServerActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblFirstDigit, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblSecondDigit, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblOperator, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(lblResult, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblHeader)
.addGroup(layout.createSequentialGroup()
.addComponent(ComboBoxOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnCalculate))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtSecondDigit)
.addComponent(txtFirstDigit, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnConfigureServer)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(lblHeader)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtFirstDigit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblFirstDigit))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtSecondDigit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblSecondDigit))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ComboBoxOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblOperator)
.addComponent(btnCalculate))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblResult)
.addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnConfigureServer)
.addContainerGap(24, Short.MAX_VALUE))
);
}
private void btnConfigureServerActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {
double firstdigit;
double seconddigit;
String operator;
firstdigit = Double.valueOf(txtFirstDigit.getText());
seconddigit = Double.valueOf(txtSecondDigit.getText());
operator = ComboBoxOperator.getSelectedItem().toString();
try
{
CoreInterface coreobj = (CoreInterface) Naming.lookup("localhost/Core");
double result = (coreobj.calc(firstdigit,seconddigit,operator));
txtResult.setText(Double.toString(result));
}
catch(Exception e)
{
txtResult.setText("e");
}
}
private javax.swing.JComboBox ComboBoxOperator;
private javax.swing.JButton btnCalculate;
private javax.swing.JButton btnConfigureServer;
private javax.swing.JLabel lblFirstDigit;
private javax.swing.JLabel lblHeader;
private javax.swing.JLabel lblOperator;
private javax.swing.JLabel lblResult;
private javax.swing.JLabel lblSecondDigit;
private javax.swing.JTextField txtFirstDigit;
private javax.swing.JTextField txtResult;
private javax.swing.JTextField txtSecondDigit;
// End of variables declaration
}
Any advise is appreciated.
The MainCalculator class is a JPanel. A JPanel can't just be displayed like that, it has to be part of a Window. Add it to a JFrame and call setVisible(true) on the JFrame.
Also, #npinti's advice is very good: execute GUI-related code in the EDT thread.
The issue is most likely in this section:
public static void main(String[] args) {
MainCalculator calc = new MainCalculator();
calc.setVisible(true);
}
You need to make any GUI related tasks within the Event Dispatcher Thread (EDT).
Try something like so:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
MainCalculator calc = new MainCalculator();
calc.setVisible(true);
}
});
}
JPanel doesn't show up, i put .setVisible but.. it doesn't show up neither, It builds but it doesn't show up I'm only just a beginner in this part of programming.
package calculatorv3;
public class CalculatorForm extends javax.swing.JPanel {
private static final long serialVersionUID = 1L;
public CalculatorForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
private void initComponents() {
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("1");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("3");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("4");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("5");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("6");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("7");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("8");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("9");
jButton11.setText("0");
jButton12.setText("Add");
jButton13.setText("Sub");
jButton14.setText("Mul");
jButton15.setText("Div");
jButton1.setText("Enter");
jButton16.setText("Reset");
jButton17.setText("Equals");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2)
.addComponent(jTextField3)
.addComponent(jTextField4)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton12)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton14))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton11)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton13)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton15)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jButton17, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6)
.addComponent(jButton12)
.addComponent(jButton14))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton10)
.addComponent(jButton11)
.addComponent(jButton13)
.addComponent(jButton15))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton17, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(jButton16))
.addContainerGap())
);
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new CalculatorForm().setVisible(true);
}
});
}
}
I can't find out what's wrong in my code, don't bother the typo errors "the numbering of the buttons" I'am only concern on how to show the Panel up
Ok I edited the code I merge it with the public static void main.. for one time init
import javax.swing.JButton;
import javax.swing.JTextField;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Jfetizanan
*/
public class NewMain {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
JTextField jTextField2 = new javax.swing.JTextField();
JTextField jTextField3 = new javax.swing.JTextField();
JTextField jTextField4 = new javax.swing.JTextField();
JButton jButton2 = new javax.swing.JButton();
JButton jButton3 = new javax.swing.JButton();
JButton jButton4 = new javax.swing.JButton();
JButton jButton5 = new javax.swing.JButton();
JButton jButton6 = new javax.swing.JButton();
JButton jButton7 = new javax.swing.JButton();
JButton jButton8 = new javax.swing.JButton();
JButton jButton9 = new javax.swing.JButton();
JButton jButton10 = new javax.swing.JButton();
JButton jButton11 = new javax.swing.JButton();
JButton jButton12 = new javax.swing.JButton();
JButton jButton13 = new javax.swing.JButton();
JButton jButton14 = new javax.swing.JButton();
JButton jButton15 = new javax.swing.JButton();
JButton jButton1 = new javax.swing.JButton();
JButton jButton16 = new javax.swing.JButton();
JButton jButton17 = new javax.swing.JButton();
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton3.setText("1");
jButton3.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton4.setText("3");
jButton4.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton5.setText("4");
jButton5.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton6.setText("5");
jButton6.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton7.setText("6");
jButton7.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton8.setText("7");
jButton8.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton9.setText("8");
jButton9.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
jButton10.setText("9");
jButton11.setText("0");
jButton12.setText("Add");
jButton13.setText("Sub");
jButton14.setText("Mul");
jButton15.setText("Div");
jButton1.setText("Enter");
jButton16.setText("Reset");
jButton17.setText("Equals");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2)
.addComponent(jTextField3)
.addComponent(jTextField4)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton12)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton14))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton11)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton13)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton15)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jButton17, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6)
.addComponent(jButton12)
.addComponent(jButton14))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton10)
.addComponent(jButton11)
.addComponent(jButton13)
.addComponent(jButton15))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton17, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(jButton16))
.addContainerGap())
);
}
}
But how about this, it shows errors
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
How can I add all objects to the Frame
Ok I finally finish the code.. here it is Thanks
This is incorrect
public class CalculatorForm extends javax.swing.JPanel
it should be
public class CalculatorForm extends javax.swing.JFrame
And here is a proper way How to Make Frames
UPDATE:
Do as you are doing but within initComponents()
JPanel panel = new JPanel();
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel);
panel.setLayout(layout);
add(panel);
UPDATE : Here is proper way of doing
public class CalculatorForm {
private static final long serialVersionUID = 1L;
public CalculatorForm() {
initComponents();
}
private void initComponents() {
JFrame frame = new JFrame();
// frame settings like size, close operation etc.
JPanel panel = new JPanel();
// init textfields and buttons
// add listeners or whatever
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel);
panel.setLayout(layout);
// layout settings goes here
frame.add(panel);
frame.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new CalculatorForm();
}
});
}
}
I'm creating a program to keep track of a list of DVD's. On the main page I have set up 2 JButtons. When the user clicks the one that says new, a new JFrame from another class comes up with fields to enter information about a new DVD. I'm trying to make a cancel button on this second frame work so that when one clicks the cancel button, it brings back up the main GUI and closes the new entry GUI. Code is below, I cant figure out how to do it.
Main GUI page
package dvdlibrary;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class bootPage extends javax.swing.JFrame {
/** Creates new form bootPage */
public bootPage() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setText("DVD Library");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(168, 11, -1, -1));
jLabel2.setText("What would you like to do today?");
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(113, 70, -1, -1));
jButton1.setText("Create Record");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 135, -1, -1));
jButton2.setText("View Library");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(224, 135, 111, -1));
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getSource()== jButton1)
dvdlibrary.createEntry.createE();
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getSource()== jButton2)
dvdlibrary.movieChooser.movChoo();
}
public static void bootP() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new bootPage().setVisible(true);
new bootPage().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
}
Create Entry page
package dvdlibrary;
public class createEntry extends javax.swing.JFrame {
public createEntry() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Title");
jLabel2.setText("Genre");
jLabel3.setText("Length");
jLabel4.setText("Rating");
jLabel5.setText("Description");
jLabel6.setText("Year");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setText("Input");
jButton2.setText("Load");
jButton3.setText("Save");
jButton4.setText("Cancel");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton4MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField2)
.addComponent(jTextField4)
.addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)))))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(124, 124, 124)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(jButton4)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)))
.addGap(23, 23, 23))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton4)))
.addContainerGap())
);
pack();
}
private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {
}
public static void createE() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new createEntry().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}
I think you can also use DISPOSE_ON_CLOSE:
DISPOSE_ON_CLOSE (the default for
JInternalFrame)
Hide and dispose of the window when the user closes it. This removes
the window from the screen and frees up any resources used by it.
Did you read the API to find out other values for the setDefaultCloseOperation(...) method?
You can use:
DISPOSE_ON_CLOSE - the frame will close. If this is the last open frame for the application then the JVM will terminate as well
HIDE_ON_CLOSE - the frame is just set to invisible.
when user clicks the one that says new a new jframe from another class comes up with fields to enter information about a new dvd.
The real problem with your program is that you are using a frame as the popup window. You should be using a modal JDialog. An application should only ever have a single JFrame with multiple dialogs to gather additional information. A JDialog does not allow you to use EXIT_ON_CLOSE.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
If this is the case, make sure to use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); on your JFrame when initializing, not setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DISPOSE_ON_CLOSE will terminate your application when the last JFrame
is closed. EXIT_ON_CLOSE will terminate your application as soon as
that JFrame is closed. HIDE_ON_CLOSE (default) will not terminate
your application even if all JFrames are hidden.
I had a similar problem and used jFrameInstanceVariable.setVisible(false); as the action that occured when I hit my CANCEL button. But every time I clicked the button that popped up the JFrame afterward, all the old content was still there in addition to the new content created from pressing the button.
I fixed this by making a new instance of JFrame every time the button is pressed that pops up the JFrame.
To hide the window without it actually closing the program, do:
f.setVisible(false);
If you just hide the program, using HIDE_ON_CLOSE or set.Visible(false), that is not a very good choice, because if you do something larger with more frames, the hidden frames will still be running, slowing down computer performance. The way, as other have said, is instead of using setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);, which will terminate the program when you click "X, closing all of the frames, use the following:
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
If your using a button like I think you said you were
this.dispose();
will do the trick..
Try putting this.dispose(); inside the cancel button method.