Netbeans: string not displaying - java

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.

Related

Rdf and java gui

I have a job to do and I want to display results from this program in my GUI but I just can not do it, then I want a method for allowing me to see the results of this program in my graph inetreface, so I created an interface and I reserved a button displays and an area for display (JTextArea), it means when I click on button shows me it shows the results (the contents of my Rdf file). I want your help if possible and thank you in advance
Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in = (InputStream) FileManager.get().open("C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf");
if (in == null) {
throw new IllegalArgumentException(
"File: " + "C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf"+ " not found");
}
// read the RDF/XML file
model.read(in, "");
// write it to standard out
//model.write(System.out);
model.write(System.out);
and here's my program interface
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;
/**
*
* #author SAMSUNG
*/
public class frameFinal extends javax.swing.JFrame {
/**
* Creates new form FrameFinal
*/
public frameFinal() {
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jTextField3 = new javax.swing.JTextField();
jPanel3 = new javax.swing.JPanel();
jTextArea2 = new javax.swing.JTextArea();
jButton3 = new javax.swing.JButton();
jPanel4 = new javax.swing.JPanel();
jButton4 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Rdf & taxonomie");
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jButton1.setText("PARCOURIR");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField3.setBackground(new java.awt.Color(204, 204, 204));
jTextField3.setEnabled(false);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(53, Short.MAX_VALUE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(79, 79, 79))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(120, 120, 120)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(157, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Chargement File", jPanel1);
jButton3.setText("Visualise");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jTabbedPane1.addTab("Visualiser", jPanel3);
jButton4.setText("Generer");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap(477, Short.MAX_VALUE)
.addComponent(jButton3)
.addGap(20, 20, 20))
.addComponent(jScrollPane2)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton3))
);
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(0, 489, Short.MAX_VALUE)
.addComponent(jButton4)
.addContainerGap())
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton4)
.addGap(0, 25, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Taxonomie", jPanel4);
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
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)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("C:\\Users\\SAMSUNG\\Desktop\\WICM2\\projet"));
fileChooser.setFileFilter(new FileNameExtensionFilter("RDF Definitions", "rdf", "RDF"));
int returnVal = fileChooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
this.jTextField3.setText(fileChooser.getSelectedFile().getPath());
}//GEN-LAST:event_jButton1ActionPerformed
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
jTextArea1.setText("");
Model model = ModelFactory.createDefaultModel();
InputStream in = FileManager.get().open("C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf");
Model m = model.read(in,null);
NodeIterator nit = m.listObjects();
List<String> lclass = new ArrayList<>();
Map<String,List<String>> map = new HashMap<>();
StmtIterator si = m.listStatements();
while(si.hasNext()){
Statement statement = si.next();
if(statement.getPredicate().getLocalName().equalsIgnoreCase("subClassOf")){
String mere = statement.getObject().toString();
String fils = statement.getSubject().getLocalName();
if(map.containsKey(mere)){
List<String> l = map.get(mere);
l.add(fils);
map.replace(mere, l);
}else{
List<String> l = new ArrayList<>();
l.add(fils);
map.put(mere, l);
}
}
if(statement.getObject().isResource() && statement.getPredicate().getLocalName().equalsIgnoreCase("type")){
if(!lclass.contains(statement.getSubject().getLocalName())){
lclass.add(statement.getSubject().getLocalName()) ;
}
}
}
jTextArea1.append("Les classes:\n");
for(String classe:lclass){
jTextArea1.append(classe+"\n");
}
jTextArea1.append("Les classe et les sous classes:\n");
for(String l : map.keySet()){
jTextArea1.append("Classe mere : "+l+"\n");
for(String s:map.get(l)){
jTextArea1.append(" Classe fils:"+s+"\n");
}
}
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
/*jTextArea2.setText("");
Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in = FileManager.get().open("C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf");
// DataInputStrean st = new DataInputStream() ;
if (in == null) {
throw new IllegalArgumentException(
"File: " + "C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf"+ " not found");
}
// read the RDF/XML file
model.read(in, "");
// write it to standard out
//model.write(System.out);
model.write(System.out);*/
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frameFinal().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField3;
// End of variables declaration//GEN-END:variables
}
If I understand you want to display entire RDF file in a text area. So if it's that what you want, you just need to read it in using a reader something like...
try{
FileReader f = new FileReader("c:\\opus_august2007.rdf");
BufferedReader b = new BufferedReader(f);
boolean eof = false;
while(! eof)
{
String lineIn = b.readLine();
if(lineIn == null)
{
eof = true;
}
else
{
mytxtarea.append(lineIn + System.getProperty("line.separator"));
}
}
}catch(Exception e){
e.printStackTrace();
}

vlcj application optimization

Hi friends I'm actually developing my own video player, using VLCJ libraries , I use a canvas over a JFrame and a options bar with buttons to play,stop etc..
in this moment the video player and all the buttons works, but
it is so slow and the fps are so low too, I think that using a canvas is not the best method to play videos , or maybe my code is not optimized .
Main class:
package vlcvideo;
import java.io.File;
import javax.swing.JFileChooser;
public class VlcVideo {
static JFileChooser filex = new JFileChooser();
public static void main(String[] args)
{
String rutaVlc = "C:/vlc64/vlc";
String rutaVideo="/src/vlcVideo/c.mp4";
File ourFile;
//seleccionar la ruta de instalacion del vlc
/*filex.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//selecciona solo directorios
filex.showSaveDialog(null);
ourFile=filex.getSelectedFile();
rutaVlc=ourFile.getAbsolutePath();*/
/*
filex.setFileSelectionMode(JFileChooser.FILES_ONLY);
filex.showSaveDialog(null);
ourFile=filex.getSelectedFile();
rutaVideo=ourFile.getAbsolutePath();*/
System.out.println(rutaVlc + "ruta DLL");
System.out.println(rutaVideo + " ruta video");
//new MediaPlayer(rutaVlc,rutaVideo).run();
//MediaPlayer mediaPlayer = new MediaPlayer(rutaVlc,rutaVideo);
prueba pb = new prueba(rutaVlc,rutaVideo);
// reproductor rp = new reproductor(rutaVlc,rutaVideo);
// rp.setVisible(true);
}
}
Frame class:
package vlcvideo;
import com.sun.jna.NativeLibrary;
/*import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;*/
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class prueba extends javax.swing.JFrame
{
private static final int SKIP_TIME_MS = 10 * 1000;
private boolean mousePressedPlaying = false;
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension tm = tk.getScreenSize();
private final String vlc;
Canvas c;
private final String video;
EmbeddedMediaPlayer mediaPlayer;
public prueba(String vlc,String video)
{
this.video=video;
this.vlc=vlc;
initComponents();
initMyComponentes();
createCanvas();
createMedia();
printTime();
}
public void printTime()
{
/*while(true)
{
final long time = mediaPlayer.getTime();
final int position = (int)(mediaPlayer.getPosition() * 1000.0f);
final int chapter = mediaPlayer.getChapter();
final int chapterCount = mediaPlayer.getChapterCount();
int posicionSlider=positionSlider.getValue();
if(mediaPlayer.isPlaying())
{
updateTime(time);
updatePosition(position);
updateChapter(chapter, chapterCount);
}
} */
timeThread tt = new timeThread(this,mediaPlayer);
tt.start();
}
public void createCanvas()
{
c = new Canvas();
c.setBackground(Color.black);
int panelHeight = panelVideo.getHeight();
int panelWidth = panelVideo.getWidth();
System.out.println(panelHeight + " " +panelWidth);
c.setBounds(0, 0,panelWidth, panelHeight);
panelVideo.add(c);
}
public void initMyComponentes()
{
positionSlider.setMinimum(0);
positionSlider.setMaximum(1000);
positionSlider.setValue(0);
positionSlider.setToolTipText("Position");
volumeSlider.setOrientation(JSlider.HORIZONTAL);
volumeSlider.setToolTipText("Change volume");
System.out.println(tm.height + " * "+tm.width);
}
/* public void duracion()
{
IContainer container = IContainer.make();
int result = container.open(video, IContainer.Type.READ, null);
if(result<0)
throw new RuntimeException("Failed to open media file");
long duration = container.getDuration();
System.out.println("Duration (ms): " + duration);
}*/
public void createMedia()
{
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),vlc);
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
mediaPlayer.toggleFullScreen();
mediaPlayer.setEnableMouseInputHandling(false);
mediaPlayer.setEnableKeyInputHandling(true);
mediaPlayer.prepareMedia(video);
mediaPlayer.play();
this.setVisible(true);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
panelVideo = new javax.swing.JPanel();
btnMute = new javax.swing.JButton();
btnPlay = new javax.swing.JButton();
btnPause = new javax.swing.JButton();
btnStop = new javax.swing.JButton();
positionSlider = new javax.swing.JSlider();
lblTime = new javax.swing.JLabel();
chapterLabel = new javax.swing.JLabel();
volumeSlider = new javax.swing.JSlider();
btnRewind = new javax.swing.JButton();
btnRewind2 = new javax.swing.JButton();
lblTotal = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBackground(new java.awt.Color(255, 51, 51));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout panelVideoLayout = new javax.swing.GroupLayout(panelVideo);
panelVideo.setLayout(panelVideoLayout);
panelVideoLayout.setHorizontalGroup(
panelVideoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
panelVideoLayout.setVerticalGroup(
panelVideoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 256, Short.MAX_VALUE)
);
btnMute.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fotos/mute.jpg"))); // NOI18N
btnMute.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMuteActionPerformed(evt);
}
});
btnPlay.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fotos/Play.png"))); // NOI18N
btnPlay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPlayActionPerformed(evt);
}
});
btnPause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fotos/Pause.jpg"))); // NOI18N
btnPause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPauseActionPerformed(evt);
}
});
btnStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fotos/stop.jpg"))); // NOI18N
btnStop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnStopActionPerformed(evt);
}
});
positionSlider.setMinorTickSpacing(10);
positionSlider.setPaintTicks(true);
positionSlider.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
positionSliderMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
positionSliderMouseReleased(evt);
}
});
lblTime.setText("0");
chapterLabel.setText("00/00");
volumeSlider.setMinorTickSpacing(10);
volumeSlider.setPaintLabels(true);
volumeSlider.setPaintTicks(true);
volumeSlider.setToolTipText("");
volumeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
volumeSliderStateChanged(evt);
}
});
btnRewind.setText("<<");
btnRewind.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRewindActionPerformed(evt);
}
});
btnRewind2.setText(">>");
btnRewind2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRewind2ActionPerformed(evt);
}
});
lblTotal.setText("/0t");
jLabel1.setText("Volumen");
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(178, 178, 178)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(panelVideo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnPause, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnStop, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(positionSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(91, 91, 91))
.addGroup(layout.createSequentialGroup()
.addGap(171, 171, 171)
.addComponent(lblTime)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblTotal)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 152, Short.MAX_VALUE)
.addComponent(btnRewind)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(chapterLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(37, 37, 37)
.addComponent(btnMute, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19))
.addGroup(layout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(btnRewind2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 109, Short.MAX_VALUE)
.addComponent(volumeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panelVideo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel2, 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(33, 33, 33)
.addComponent(positionSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblTime)
.addComponent(lblTotal)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(chapterLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnRewind)
.addComponent(btnRewind2)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(btnMute, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(volumeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnPause, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPlay)
.addComponent(btnStop, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(19, 19, 19))))
);
pack();
}// </editor-fold>
private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {
mediaPlayer.play();
}
private void btnPauseActionPerformed(java.awt.event.ActionEvent evt) {
mediaPlayer.pause();
}
private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {
mediaPlayer.stop();
}
private void btnMuteActionPerformed(java.awt.event.ActionEvent evt) {
mediaPlayer.mute();
}
private void positionSliderMousePressed(java.awt.event.MouseEvent evt) {
if(mediaPlayer.isPlaying()) {
mousePressedPlaying = true;
mediaPlayer.pause();
}
else
{
mousePressedPlaying = false;
}
setSliderBasedPosition();
}
private void positionSliderMouseReleased(java.awt.event.MouseEvent evt) {
setSliderBasedPosition();
updateUIState();
}
private void volumeSliderStateChanged(javax.swing.event.ChangeEvent evt) {
JSlider source = (JSlider)evt.getSource();
// if(!source.getValueIsAdjusting()) {
mediaPlayer.setVolume(source.getValue());
// }
}
private void btnRewindActionPerformed(java.awt.event.ActionEvent evt) {
skip(-SKIP_TIME_MS);
}
private void btnRewind2ActionPerformed(java.awt.event.ActionEvent evt) {
skip(SKIP_TIME_MS);
}
public void setSliderBasedPosition()
{
if(!mediaPlayer.isSeekable()) {
return;
}
float positionValue = positionSlider.getValue() / 1000.0f;
// Avoid end of file freeze-up
if(positionValue > 0.99f) {
positionValue = 0.99f;
}
mediaPlayer.setPosition(positionValue);
}
private void updateUIState()
{
if(!mediaPlayer.isPlaying()) {
// Resume play or play a few frames then pause to show current position in video
mediaPlayer.play();
if(!mousePressedPlaying) {
try {
// Half a second probably gets an iframe
Thread.sleep(500);
}
catch(InterruptedException e) {
// Don't care if unblocked early
}
mediaPlayer.pause();
}
}
long time = mediaPlayer.getTime();
int position = (int)(mediaPlayer.getPosition() * 1000.0f);
int chapter = mediaPlayer.getChapter();
int chapterCount = mediaPlayer.getChapterCount();
updateTime(time);
updatePosition(position);
updateChapter(chapter, chapterCount);
}
public void skip(int skipTime)
{
// Only skip time if can handle time setting
if(mediaPlayer.getLength() > 0) {
mediaPlayer.skip(skipTime);
updateUIState();
}
}
public void updateTime(long millis)
{
String s = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
lblTime.setText(s);
}
public void updatePosition(int value)
{
// positionProgressBar.setValue(value);
positionSlider.setValue(value);
}
public void updateChapter(int chapter, int chapterCount) {
String s = chapterCount != -1 ? (chapter + 1) + "/" + chapterCount : "-";
chapterLabel.setText(s);
chapterLabel.invalidate();
validate();
}
public void updateVolume(int value) {
volumeSlider.setValue(value);
}
// Variables declaration - do not modify
private javax.swing.JButton btnMute;
private javax.swing.JButton btnPause;
private javax.swing.JButton btnPlay;
private javax.swing.JButton btnRewind;
private javax.swing.JButton btnRewind2;
private javax.swing.JButton btnStop;
private javax.swing.JLabel chapterLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JLabel lblTime;
private javax.swing.JLabel lblTotal;
private javax.swing.JPanel panelVideo;
public javax.swing.JSlider positionSlider;
private javax.swing.JSlider volumeSlider;
// End of variables declaration
}
The Thread used for print the time:
package vlcvideo;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
public class timeThread extends Thread
{
prueba pb;
EmbeddedMediaPlayer mediaPlayer;
public timeThread(prueba pb,EmbeddedMediaPlayer mediaPlayer)
{
this.pb=pb;
this.mediaPlayer=mediaPlayer;
}
public void run()
{
while(true)
{
final long time = mediaPlayer.getTime();
final int position = (int)(mediaPlayer.getPosition() * 1000.0f);
final int chapter = mediaPlayer.getChapter();
final int chapterCount = mediaPlayer.getChapterCount();
int posicionSlider=pb.positionSlider.getValue();
if(mediaPlayer.isPlaying())
{
pb.updateTime(time);
pb.updatePosition(position);
pb.updateChapter(chapter, chapterCount);
}
}
}
}
libraries used:
-jna 3.4.0.jar
-vlcj 2.1.0.jar
jdk:1.8
You have a tight loop in your thread that will consume just about all of your CPU.
Rather than polling inside a tight loop you should use media player events to get the information you want.
Or at worst you should sleep in that loop.

Accessing elements from a jFrame that is within a running instance of a thread

I am trying to make an app for communicating between different running instances of a thread. I have a jFrame that has a jTextField and a jButton. In the jTextField I type the number of threads that I want to run and after I press the jButton the threads run. Each thread contains a jFrame with a jButton. So if I type 3 in the jTextField and then press OK, three different jFrames pop out that have an own jButton. If I press the jButton in one of the jFrames of the threads, the jButton is set to disabled (setEnabled(false)). This should happen to each jButton of the jFrames from within the threads when pressed but the one from the last jFrame that is still not pressed.
This is the window class for the thread:
public class Window extends JFrame implements Runnable {
JFrame jr;
JButton bt;
public void run() {
jr=new JFrame();
bt=new jButton();
bt.setTitle("Press Me");
jr.setLayout(new FlowLayout());
jr.add(bt);
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
bt.setEnabled(false);
}
});
jr.setVisible(true);
}
}
Now this is how I run multiple instances of this thread. i is the number of the thread instances that is taken from the jTextField:
( int i=Integer.parseInt(jTextField1.gettext()) )
for (int a=0;a<i;a++) {
Runnable thr=new Window(a);
executor.execute(thr);
}
This is what I want to do: After I press the jButton on every jFrame that is within the thread instances and it is set to setEnabled(false) I get to the last jFrame that is popped up whose jButton is still unpressed. When I press this last JButton I want that all the JButtons on every jFrame to be set back to setEnabled(true). How can I do that?
This is the main class it works 100 % now!
import java.awt.Color;
import java.awt.Image;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
*
* #author oliver
*/
public class Hauptklasse extends javax.swing.JFrame {
static int i = 0;
static Random r;
static boolean OkApasat=false;
static int c;
public static Fereastra[] thread;
public Hauptklasse() throws IOException {
initComponents();
Image logo;
logo = ImageIO.read(getClass().getResource("resurse/logo.png"));
jLabel4.setIcon(new ImageIcon(logo));
jLabel4.setVisible(true);
setLocation(200,100);
jPanel1.setBackground(Color.cyan);
jTextField1.setEditable(false);
jButton2.setEnabled(false);
r=new Random();
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setPreferredSize(new java.awt.Dimension(549, 448));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton1.setText("Pornire");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setBackground(new java.awt.Color(0, 0, 0));
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Andale Mono", 0, 11)); // NOI18N
jTextArea1.setForeground(new java.awt.Color(255, 255, 255));
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setText("Numar maxim de ferestre");
jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton2.setText("OK");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2)))
.addContainerGap(31, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addGroup(jPanel1Layout.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(jButton2))
.addGap(22, 22, 22)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setEditable(true);
jButton1.setEnabled(false);
jButton2.setEnabled(true);
jTextArea1.append("\n Welcome! :)");
jTextArea1.append("\n The following app tests the communication between threads");
jTextArea1.append("\n Please enter maximum number of windows to be opened");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
OkApasat=true;
}
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String args[]) throws IOException {
final Hauptklasse main1 = new Hauptklasse();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
main1.setVisible(true);
}
});
while (true){
if (OkApasat==true){
if (Integer.parseInt(jTextField1.getText())>=3){
i= (Integer.parseInt(jTextField1.getText()));
main1.jTextArea1.append("\n"+i+" windows opened");
Fereastra[] thr=new Fereastra[i];
for (c = 0; c < i; c++) {
thr[c]=new Fereastra(thr);
thr[c].run();
}
OkApasat=false;
main1.jButton2.setEnabled(false);
main1.jTextField1.setEditable(false);
}
else jTextArea1.append("\n Wrong maximum number of windows-must be at least 3");
OkApasat=false;
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(Hauptklasse.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
public static javax.swing.JTextArea jTextArea1;
public static javax.swing.JTextField jTextField1;
// End of variables declaration
}
THIS IS THE WINDOW CLASS, WORKS 100 %:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* #author oliver
*/
public class Fereastra extends JFrame implements Runnable {
public JFrame jr;
public JButton bt;
public JLabel l;
public JLabel l2;
public Fereastra[] thread;
public boolean deblocat = true;
public Fereastra(Fereastra[] thread) {
this.thread = thread;
jr = new JFrame();
jr.setSize(250, 250);
jr.setLayout(new FlowLayout());
bt = new JButton();
jr.add(bt);
bt.setText("OK");
bt.setBackground(Color.cyan);
l2 = new JLabel();
l2.setText("Buton activ");
jr.add(l2);
jr.setVisible(true);
}
public void run() {
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bt.setEnabled(false);
deblocat = true;
for (int c = 0; c < Hauptklasse.i; c++) {
if (thread[c] != null && thread[c].bt.isEnabled()) {
deblocat = false;
break;
}
}
if (deblocat == true) {
for (int c = 0; c < Hauptklasse.i; c++) {
if (thread[c] != null) {
thread[c].bt.setEnabled(true);
}
}
}
}
});
}
}
My classes:
1) The Window class (Fereastra means window, deblocat means unlocked, buton activ means Button is enabled)
package concurenta;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* #author oliver
*/
public class Fereastra extends JFrame implements Runnable {
public static JFrame jr;
public static JButton bt;
public static JLabel l;
public static JLabel l2;
public static Fereastra[] thread;
public boolean deblocat = true;
public Fereastra(Fereastra[] thread) {
this.thread = thread;
jr = new JFrame();
jr.setSize(250, 250);
jr.setLayout(new FlowLayout());
bt = new JButton();
jr.add(bt);
bt.setText("OK");
bt.setBackground(Color.cyan);
l2 = new JLabel();
l2.setText("Buton activ");
jr.add(l2);
jr.setVisible(true);
}
public void run() {
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bt.setEnabled(false);
deblocat = true;
for (int c = 0; c < Hauptklasse.i; c++) {
if (thread[c] != null && thread[c].bt.isEnabled()) {
deblocat = false;
break;
}
}
if (deblocat == true) {
for (int c = 0; c < Hauptklasse.i; c++) {
if (thread[c] != null) {
thread[c].bt.setEnabled(true);
}
}
}
}
});
}
}
2) The Main Class(a.k.a Hauptklasse, okApasat means ok is pressed):
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package concurenta;
import java.awt.Color;
import java.awt.Image;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
*
* #author oliver
*/
public class Hauptklasse extends javax.swing.JFrame {
static int i = 0;
static Random r;
static boolean OkApasat=false;
static int c;
public static Fereastra[] thread;
public Hauptklasse() throws IOException {
initComponents();
Image logo;
logo = ImageIO.read(getClass().getResource("resurse/logo.png"));
jLabel4.setIcon(new ImageIcon(logo));
jLabel4.setVisible(true);
setLocation(200,100);
jPanel1.setBackground(Color.cyan);
jTextField1.setEditable(false);
jButton2.setEnabled(false);
r=new Random();
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setPreferredSize(new java.awt.Dimension(549, 448));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton1.setText("Pornire");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setBackground(new java.awt.Color(0, 0, 0));
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Andale Mono", 0, 11)); // NOI18N
jTextArea1.setForeground(new java.awt.Color(255, 255, 255));
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setText("Numar maxim de ferestre");
jButton2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton2.setText("OK");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2)))
.addContainerGap(31, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addGroup(jPanel1Layout.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(jButton2))
.addGap(22, 22, 22)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setEditable(true);
jButton1.setEnabled(false);
jButton2.setEnabled(true);
jTextArea1.append("\n Welcome! :)");
jTextArea1.append("\n The following app tests the communication between threads");
jTextArea1.append("\n Please enter maximum number of windows to be opened");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
OkApasat=true;
}
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String args[]) throws IOException {
final Hauptklasse main1 = new Hauptklasse();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
main1.setVisible(true);
}
});
while (true){
if (OkApasat==true){
if (Integer.parseInt(jTextField1.getText())>=3){
i= (int) (Math.random() * (Integer.parseInt(jTextField1.getText()) - 3)) + 3;
main1.jTextArea1.append("\n"+i+" windows opened");
Fereastra[] thr=new Fereastra[i];
for (c = 0; c < i; c++) {
thr[c]=new Fereastra(thr);
thr[c].run();
}
OkApasat=false;
main1.jButton2.setEnabled(false);
main1.jTextField1.setEditable(false);
}
else jTextArea1.append("\n Wrong maximum number of windows-must be at least 3");
OkApasat=false;
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(Hauptklasse.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
public static javax.swing.JTextArea jTextArea1;
public static javax.swing.JTextField jTextField1;
// End of variables declaration
}

How can I pass a variable from one JFrame To Another JFrame

I'm new to Java. I tried to find solution by myself but am not sure how to proceed.
I have 2 Jframes (JF1,JF2) , and I need to pass a variable from the JF1 to JF2
This is the code for JF1 : Note : i need the value x , value x get it from JTextfeild.
Can any one help me please
/*
* JF1.java
*
* Created on 04 مايو, 2012, 09:01 م
*/
package finalpro;
import java.io.BufferedInputStream;
import javax.swing.JOptionPane;
/**
*
* #author OmaR
*/
public class JF1 extends javax.swing.JFrame {
/** Creates new form JF1 */
public JF1() {
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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
JB1 = new javax.swing.JButton();
JB3 = new javax.swing.JButton();
JB2 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Online Test Creater");
JB1.setText("Make");
JB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JB1ActionPerformed(evt);
}
});
JB3.setText("Quit");
JB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JB3ActionPerformed(evt);
}
});
JB2.setText("Reset");
JB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JB2ActionPerformed(evt);
}
});
jLabel2.setText("Number Of Question");
jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTextField1MouseClicked(evt);
}
});
jButton1.setText("Main");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(JB2)
.addComponent(JB3)
.addComponent(JB1)
.addComponent(jButton1))
.addGap(30, 30, 30))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {JB1, JB2, JB3, jButton1});
layout.setVerticalGroup(
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)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(JB1)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(JB2)
.addGap(18, 18, 18)
.addComponent(JB3)))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {JB1, JB2, JB3, jButton1});
pack();
}// </editor-fold>
private void JB3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void JB2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField1.setText("");
}
private void JB1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JF1 JFBU = new JF1() ;
JF2 JCreate = new JF2();
this.setVisible(false);
int z = Integer.parseInt(jTextField1.getText());
if (z == 0)
{
JOptionPane.showMessageDialog(null, " You Enter 0 Please write anumber ");
}
else
{
JCreate.setVisible(true);
}
}
private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null, "The Max Number of Questions Is 10");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFmain JFm = new JFmain();
JFm.setVisible(true);
this.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JF1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton JB1;
private javax.swing.JButton JB2;
private javax.swing.JButton JB3;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
I have JF2, and I need to pass the Var z on it.
Create constructor with parameter int z:
public class JF2 extends JFrame {
private int z;
public void setZ(int z) {
this.z = z;
}
public int getZ() {
return z;
}
public JF2(int z) {
this.z = z;
}
}
And pass the variable z when you create the JF2 -> JF2 jCreate = new JF2(z); or using setter jCreate.setZ(z);

populating a jcombobox with database information

I know I am missing something simple, I think in getting the combobox to access the array.
package my.freelancebillingapp;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import javax.swing.*;
public class billingInfoUI extends javax.swing.JFrame {
public billingInfoUI() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
hoursWorked = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
flatRate = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
workType = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Billing Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_BOTTOM, new java.awt.Font("Bleeding Cowboys", 0, 48))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 457, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
hoursWorked.setHorizontalAlignment(javax.swing.JTextField.CENTER);
hoursWorked.setText("Hours Worked");
hoursWorked.setBorder(null);
jLabel1.setText("Or");
flatRate.setHorizontalAlignment(javax.swing.JTextField.CENTER);
flatRate.setText("Flat Rate");
flatRate.setBorder(null);
workType.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Work Type", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
workType.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Web Design", "Graphic Design", "Consulting" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
workType.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(workType);
jList2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Any Extras?", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
jList2.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Blog", "Forum", "Templating", "Rush Delivery" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane2.setViewportView(jList2);
jButton1.setText("Save");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton2.setText("Invoice");
jButton3.setText("Close");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jComboBox1.setModel(customers);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(89, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(hoursWorked, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48)
.addComponent(jLabel1)
.addGap(1, 1, 1))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(flatRate, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(79, 79, 79))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(79, 79, 79)
.addComponent(jButton3)
.addGap(52, 52, 52))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(134, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(107, 107, 107))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(hoursWorked, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(flatRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton1))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
new FreelanceBillingUI().setVisible(true);
setVisible(false);
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String hoursWorkedtxt = hoursWorked.getText();
String flatRatetxt = flatRate.getText();
workType.getSelectedValue();
workType.toString();
Object workTypetxt = workType.getSelectedValue();
if(hoursWorkedtxt.contains("H")){
hoursWorkedtxt = "0";
}else{
flatRatetxt = "0";
}
}
public class CustomerData {
private static final String JDBC_CONNECTION_URL = "jdbc:mysql://localhost/test";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
private static final String SQL_FETCH_CUSTOMERS = "SELECT custName FROM customers";
private Connection connection = null;
public CustomerData(){
initConnection();
}
private void initConnection() {
try {
//load the mysql driver
Class.forName("com.mysql.jdbc.Driver");
//create the database connection
connection = DriverManager.getConnection(JDBC_CONNECTION_URL, DATABASE_USER, DATABASE_PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeConnection(){
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
connection = null;
}
}
}
public ArrayList fetchCustomerData(){
if (connection != null){
Statement statement = null;
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_FETCH_CUSTOMERS);
return convertResultSetToCustomersArray(resultSet);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("NO VALID DATABASE CONNECTION, CAN'T FETCH CUSTOMER DATA!");
}
return new ArrayList();
}
private ArrayList convertResultSetToCustomersArray(ResultSet results) throws SQLException{
ArrayList customers = new ArrayList();
while (results.next()){
customers.add(results.getString("custName"));
}
return customers;
}
}
private void initData(){
CustomerData customerData = new CustomerData();
ArrayList custArrayList = customerData.fetchCustomerData();
customers = (String[]) custArrayList.toArray(new String[0]);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new billingInfoUI().setVisible(true);
}
});
}
private String[] customers = null;
private javax.swing.JTextField flatRate;
private javax.swing.JTextField hoursWorked;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JList workType;
}
There is too much code there to look at 90% of it is NOT related to adding data in a combo box. You have a dozen other components which clutter the code so we can't see what actually happening with the comobo box.
It you really need help then post a SSCCE.
That is, create a simple frame and load hard coded data into the combo box.
Then create a simple program to read data from the database and use System.out.println(...) to display each value.
Once you've mastered the two basic steps, then put the two programs together to load a combobox dynamically from a database.
Finally add the rest of you components to the frame.
Break you problem down into small, manageable steps and you will be able to debug it easier.
A few points:
Wading through your code, I don't see a call to initData being made anywhere. Without that call, you wont call your database code. Without the database call, you wont get the data.
JComboBox does not have a setModel method that takes an array. You can wrap your array in a DefaultComboBoxModel and set that on the model.
It is good to keep in mind that blocking calls like database access should not be made in the AWT painting thread, and neither should calls that update the GUI be made outside of the AWT thread. You should look into creating your GUI and grabbing your data in separate threads, and making use of SwingUtlities.invokeLater or similar tools to update your GUI.

Categories

Resources