I am trying to implement Observer pattern with multiple JFrame instances. However, when notifyObservers() is called, only the last instantiated JFrame instance is updated.
This is my code:
Mall.java
import java.util.ArrayList;
import java.util.Observable;
public class Mall extends Observable{
private ArrayList<String> stores;
public Mall(){
stores = new ArrayList<String>();
}
public void addNewStore(String store){
stores.add(store);
System.out.println(store);
setChanged();
notifyObservers(store);
}
public ArrayList<String> getStores(){
return stores;
}
}
CustomerFrame.java
public class CustomerFrame extends javax.swing.JFrame {
public CustomerFrame() {
initComponents();
}
public CustomerFrame(Mall theMall) {
initComponents();
MallObserver mallObs = new MallObserver();
theMall.addObserver(mallObs);
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(jList1);
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(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(141, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(124, Short.MAX_VALUE))
);
pack();
}
public static javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
}
MallObserver.java
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
public class MallObserver implements Observer{
private Mall mallUpdate;
#Override
public void update(Observable o, Object arg) {
mallUpdate = (Mall) o;
DefaultListModel listModel = new DefaultListModel();
for(int i = 0; i < mallUpdate.getStores().size(); i++)
listModel.addElement(mallUpdate.getStores().get(i));
CustomerFrame.jList1.setModel(listModel);
}
}
AdminFrame.java
public class AdminFrame extends javax.swing.JFrame {
Mall theMall;
public AdminFrame() {
initComponents();
theMall = new Mall();
}
#SuppressWarnings("unchecked")
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
jLabel1.setText("jLabel1");
jButton1.setText("Add Store");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("New Customer");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(143, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2)
.addComponent(jButton1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(26, 26, 26)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(138, 138, 138))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(129, 129, 129)
.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(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addContainerGap(88, Short.MAX_VALUE))
);
pack();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
CustomerFrame newCust = new CustomerFrame();
newCust.setVisible(true);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
theMall.addNewStore(jTextField1.getText());
}
public static void main(String args[]) {
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(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AdminFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
}
The idea is, when a customer enters the store, the store will open up a new JFrame for the customer. When admin adds a store, all the JFrame CustomerFrame.java will have their list item updated. However, in my case, only the CustomerFrame.java that is last instantiated will get the update while the others remains the same.
The problem above can be reproduced by Opening 2 New Customer and try to Add Store at AdminFrame.java
What is the problem here?
You don't call any methods on thisFrame from within the MallCustomer's update(...) method. Rather than setting the model on storeList (where the heck does that come from?), give CustomerFrame a public method, say called setStoreListModel(ListModel listModel) that you call in update, passing in the storeModel.
i.e.,
public class CustomerFrame extends javax.swing.JFrame {
privarte Mall theMall;
private JList storesList = new JList();
public CustomerFrame(Customer customer, Mall theMall){
MallCustomer mallObserver = new MallCustomer(this);
theMall.addObserver(mallObserver);
}
public setStoresListModel(ListModel listModel) {
storesList.setModel(listModel);
}
}
and
#Override
public void update(Observable o, Object arg) {
mallUpdate = (Mall) o;
DefaultListModel storeModel = new DefaultListModel();
//Stores update
for(int i = 0; i < mallUpdate.getStores().size();i++) {
storeModel.addElement(mallUpdate.getStores().get(i));
}
// storesList.setModel(storeModel);//a JList variable
thisFrame.setStoresListModel(storeModel);
}
Note: code not compiled nor tested
Edit
You've got several issues that I can see:
Your JList should not be public nor static. Make it a private instance field.
Again (as I suggested all along), give the Customer window a public setListModel type of method.
An application should only have one main JFrame, and so the CustomerFrame JFrame should not be a JFrame but rather should be a non-modal JDialog, or maybe better a JPanel that can be placed anywhere -- in its own JDialog, in the main JFrame.
Pass the main JFrame into your JDialogs so that the dialog can register the parent JFrame in its super's constructor.
Pass the CustomerDialog instance into your MallObserver instance, and then use it to set a field inside of MallObserver.
In the update method, create or update the model and call `setListModel on the customer dialog instance that MallObserver holds.
Create Mall instance before calling initComponents(). This way you can use the same Mall instance inside of your action listener methods.
A nit-pick: when creating and posting MCVE's, get rid of the messy and distracting NetBeans generated code. Instead only post simple code and simple GUI's that you've created yourself, similar to the changes that I've made below.
And you MCVE should all fit in a single file. The file can have several classes, but it should be easy for us to cut and paste into our IDE's and then run.
For example:
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.awt.event.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class AdminFrame extends javax.swing.JFrame {
private Mall theMall = new Mall(); //!!
public AdminFrame() {
initComponents();
//!! theMall = new Mall();
}
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
addStoreBtn = new javax.swing.JButton();
newCustBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
jLabel1.setText("jLabel1");
addStoreBtn.setText("Add Store");
addStoreBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
theMall.addNewStore(jTextField1.getText());
}
});
addStoreBtn.setMnemonic(KeyEvent.VK_S);
newCustBtn.setText("New Customer");
newCustBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// !! CustomerFrame newCust = new CustomerFrame();
CustomerDialog newCust = new CustomerDialog(AdminFrame.this, theMall);
newCust.pack();
newCust.setLocationByPlatform(true);
newCust.setVisible(true);
}
});
newCustBtn.setMnemonic(KeyEvent.VK_C);
JPanel mainPanel = new JPanel();
mainPanel.add(jLabel1);
mainPanel.add(jTextField1);
mainPanel.add(addStoreBtn);
mainPanel.add(newCustBtn);
add(mainPanel);
pack();
setLocationRelativeTo(null);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AdminFrame().setVisible(true);
}
});
}
private javax.swing.JButton addStoreBtn;
private javax.swing.JButton newCustBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
}
class MallObserver implements Observer {
private Mall mallUpdate;
private CustomerDialog customerDialog; // !!
// !!
public MallObserver(CustomerDialog customerFrame) {
this.customerDialog = customerFrame; // !!
}
#Override
public void update(Observable o, Object arg) {
mallUpdate = (Mall) o;
DefaultListModel<String> listModel = new DefaultListModel<>();
for (int i = 0; i < mallUpdate.getStores().size(); i++) {
listModel.addElement(mallUpdate.getStores().get(i));
}
customerDialog.setListModel(listModel);
}
}
#SuppressWarnings("serial")
class CustomerDialog extends JDialog { //!!
// !!!!!!! public CustomerFrame() {
// initComponents();
// }
public void setListModel(ListModel<String> listModel) {
jList1.setModel(listModel);
}
public CustomerDialog(AdminFrame adminFrame, Mall theMall) {
super(adminFrame, "Customer Dialog", ModalityType.MODELESS);
initComponents();
// !! MallObserver mallObs = new MallObserver();
MallObserver mallObs = new MallObserver(this); // !!
theMall.addObserver(mallObs);
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new JList<>();
jList1.setPrototypeCellValue(" ");
jList1.setVisibleRowCount(15);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(jList1);
add(jScrollPane1);
pack();
}
// public static javax.swing.JList jList1;
private JList<String> jList1;
private JScrollPane jScrollPane1;
}
class Mall extends Observable {
private ArrayList<String> stores;
public Mall() {
stores = new ArrayList<String>();
}
public void addNewStore(String store) {
stores.add(store);
setChanged();
notifyObservers(store);
}
public ArrayList<String> getStores() {
return stores;
}
}
Related
I want to display an image from the web to a panel in another JFrame at the click of a button. Whenever I click the button, first the image loads; and during this time, the current form potentially freezes. Once the image has loaded, the form is displayed with the image. How can I avoid the situation where my form freezes since it is very irritating. Among my codes:
My current class:
private void btn_TrackbusActionPerformed(java.awt.event.ActionEvent evt) {
try {
sendMessage("Query,map,$,start,211,Arsenal,!");
System.out.println(receiveMessage());
} catch (UnknownHostException ex) {
Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception ex) {
Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
}
client_trackedbus nextform=new client_trackedbus(planform,connection,packet_receive,packet_send);
this.setVisible(false);
this.dispose();
nextform.setVisible(true);
// TODO add your handling code here:
}
My next class that displays the image:
public class client_trackedbus extends javax.swing.JFrame {
client_planform planform=null;
DatagramSocket connection=null;
DatagramPacket packet_receive=null;
DatagramPacket packet_send=null;
JLabel label=null;
/** Creates new form client_trackedbus */
public client_trackedbus(client_planform planform,DatagramSocket connection,DatagramPacket packet_receive,DatagramPacket packet_send) {
initComponents();
this.planform=planform;
this.connection=connection;
this.packet_receive=packet_receive;
this.packet_send=packet_send;
try {
displayMap("http://www.huddletogether.com/projects/lightbox2/images/image-2.jpg", jPanel1, new JLabel());
} catch (MalformedURLException ex) {
Logger.getLogger(client_trackedbus.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void displayMap(String url,JPanel panel,JLabel label) throws MalformedURLException{
URL imageurl=new URL(url);
Image image=(Toolkit.getDefaultToolkit().createImage(imageurl));
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
panel.add(label);
// System.out.println(panel.getSize().width);
this.getContentPane().add(panel);
}
/** 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();
jLabel1 = new javax.swing.JLabel();
btn_Exit = new javax.swing.JButton();
btn_Plan = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Public Transport Journey Planner");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 368, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 172, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Arial", 1, 18));
jLabel1.setText("Your tracked bus");
btn_Exit.setText("Exit");
btn_Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_ExitActionPerformed(evt);
}
});
btn_Plan.setText("Plan journey");
btn_Plan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_PlanActionPerformed(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(104, 104, 104)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addComponent(btn_Plan)
.addGap(65, 65, 65)
.addComponent(btn_Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(20, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jPanel1, 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(btn_Exit)
.addComponent(btn_Plan))
.addContainerGap(12, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btn_ExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Exitform();
}
private void btn_PlanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
this.dispose();
this.planform.setVisible(true);
}
private void Exitform(){
this.setVisible(false);
this.dispose();
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new client_trackedbus().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_Exit;
private javax.swing.JButton btn_Plan;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
As suggested in the article Concurrency in Swing, your button handler's query may be blocking the event dispatch thread. Using javax.swing.SwingWorker is one approach to loading images in the background, while displaying progress and keeping the GUI thread alive.
Addendum: Here's a sscce that loads the SO logo; it's been updated to handle exceptions and resize the enclosing container to fit the loaded image:
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
* #see http://stackoverflow.com/questions/4530659
*/
public final class WorkerTest extends JFrame {
private final JLabel label = new JLabel("Loading...");
public WorkerTest() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
this.add(label);
this.pack();
this.setLocationRelativeTo(null);
}
private void start() {
new ImageWorker().execute();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
WorkerTest wt = new WorkerTest();
wt.setVisible(true);
wt.start();
}
});
}
class ImageWorker extends SwingWorker<Image, Void> {
private static final String TEST
= "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
#Override
protected Image doInBackground() throws IOException {
Image image = ImageIO.read(new URL(TEST));
return image.getScaledInstance(640, -1, Image.SCALE_SMOOTH);
}
#Override
protected void done() {
try {
ImageIcon icon = new ImageIcon(get());
label.setIcon(icon);
label.setText("Done");
WorkerTest.this.pack();
WorkerTest.this.setLocationRelativeTo(null);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
public class SSBTest extends javax.swing.JFrame {
/** Creates new form worker1 */
public SSBTest() {
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">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 348, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 210, Short.MAX_VALUE));
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(196, Short.MAX_VALUE).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(178, 178, 178)).addGroup(layout.createSequentialGroup().addGap(86, 86, 86).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(122, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE).addComponent(jLabel1).addGap(36, 36, 36)));
pack();
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
final SSBTest ssbTest = new SSBTest();
ssbTest.setVisible(true);
ssbTest.execute();
}
});
}
private void execute() {
(new MeaningOfLifeFinder(jLabel1, jPanel1)).execute();
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
class MeaningOfLifeFinder extends SwingWorker<Icon, Object> {
JLabel label = null;
JPanel panel;
MeaningOfLifeFinder(JLabel label, JPanel jpanel) {
this.label = label;
this.panel = jpanel;
}
protected Icon doInBackground() throws IOException {
URL imageurl;
Image image = null;
System.out.println("image loading");
imageurl = new URL("http://maps.google.com/maps/api/staticmap"
+ "?zoom=14&size=512x512&maptype=roadmap"
+ "&markers=color:green|label:21|-15.0,-150.0&sensor=false");
//image = (Toolkit.getDefaultToolkit().createImage(imageurl));
image = ImageIO.read(imageurl);
ImageIcon icon = new ImageIcon(image);
System.out.println("image loaded...");
return icon;
}
#Override
protected void done() {
try {
System.out.println("image adding to label...");
label.setIcon(get());
//panel.add(label);
System.out.println("image loaded to label...");
} catch (Exception ignore) {
}
}
// System.out.println(panel.getSize().width);
}
I'm trying to make a java program, with GUI, that will let the user input the time he wants to wait until the machine will shutdown, restart or sleep. I managed to make the GUI and implemented the commands to shutdown, restart or sleep, but i don't know how to make the timer.
Here is what i did so far :
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ContactEditorUI extends javax.swing.JFrame {
public ContactEditorUI() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
buttonGroup2 = new javax.swing.ButtonGroup();
jInternalFrame1 = new javax.swing.JInternalFrame();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jInternalFrame1.setVisible(true);
jRadioButton1.setText("Shut down");
jRadioButton1.addContainerListener(new java.awt.event.ContainerAdapter() {
public void componentAdded(java.awt.event.ContainerEvent evt) {
jRadioButton1ComponentAdded(evt);
}
});
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
jRadioButton2.setText("Restart");
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton2ActionPerformed(evt);
}
});
jRadioButton3.setText("Sleep");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
jLabel1.setText("Please insert the time :");
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setText("00:00:00");
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50)
.addComponent(jButton1))
.addComponent(jRadioButton1)
.addComponent(jRadioButton3)
.addComponent(jRadioButton2))
.addContainerGap(140, Short.MAX_VALUE))
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jTextField1)
.addGap(2, 2, 2)))
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addContainerGap(32, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);
pack();
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
buttonGroup.add(jRadioButton1);
Runtime runtime = Runtime.getRuntime();
try {
//wait();
Process proc = runtime.exec("shutdown -s");
} catch (IOException ex) {
Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
buttonGroup.add(jRadioButton2);
Runtime runtime = Runtime.getRuntime();
try {
//wait();
Process proc = runtime.exec("shutdown -r");
} catch (IOException ex) {
Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
buttonGroup.add(jRadioButton3);
Runtime runtime = Runtime.getRuntime();
try {
//wait();
Process proc = runtime.exec("shutdown -h");
} catch (IOException ex) {
Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jRadioButton1ComponentAdded(java.awt.event.ContainerEvent evt) {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ContactEditorUI().setVisible(true);
}
});
}
private javax.swing.ButtonGroup buttonGroup;
private javax.swing.JButton jButton1;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JTextField jTextField1;
}
All the help will be very appreciated.
You would need to use Java's Timer class. Within this there's a method called schedule() with different signatures allowing for a variety of uses.
Timer timer = new Timer();
int start=10000; //millisecond 10 seconds=10000
int delay=1000; // millisecond 1 second
timer.schedule(new TimerTask()
{
public void run() {
// what happens when the timer repeats
}
},start,delay);
This piece of code was found originally here. This is a new answer as to relate to a bit of context to your question.
This will start a new Timer when it is called. To be able to let the user set the specified time, the schedule method takes that in as a parameter (the date that is input would need to be converted into the start variable).
Use the Java documentation found here to be able to modify this answer to suit you, as schedule can be used in a few different ways.
I think these two questions will help you or at least give you a clue:
JDialog timeout when idle
Timer on a java swing jdialog
How do I wire output to paneWithList?
PaneWithList has a listener on its JList so that the selected row is output to the console. How can I direct that output to the JTextPane on output?
Could PaneWithList fire an event which Main picks up? Would PropertyChangeSupport suffice?
Main.java:
package dur.bounceme.net;
import javax.swing.JTabbedPane;
public class Main {
private static JTabbedPane tabs;
private static PaneWithList paneWithList;
private static PaneWithTable paneWithTable;
private static Output output;
public static void main(String[] args) {
tabs = new javax.swing.JTabbedPane();
paneWithList = new PaneWithList();
paneWithTable = new PaneWithTable();
tabs.addTab("list", paneWithList);
tabs.addTab("table", paneWithTable);
tabs.addTab("output", output);
}
}
Here's an example using the observer pattern, also seen here, here and here. Note that it would also be possible to listen to the combo's model.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
/**
* #see http://en.wikipedia.org/wiki/Observer_pattern
* #see https://stackoverflow.com/a/10523401/230513
*/
public class PropertyChangeDemo {
public PropertyChangeDemo() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.add(new ObserverPanel());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
PropertyChangeDemo example = new PropertyChangeDemo();
}
});
}
}
class ObserverPanel extends JPanel {
private JLabel title = new JLabel("Value received: ");
private JLabel label = new JLabel("null", JLabel.CENTER);
public ObserverPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObserverPanel"));
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(title);
panel.add(label);
this.add(panel);
ObservedPanel observed = new ObservedPanel();
observed.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName().equals(ObservedPanel.PHYSICIST)) {
String value = e.getNewValue().toString();
label.setText(value);
}
}
});
this.add(observed);
}
}
class ObservedPanel extends JPanel {
public static final String PHYSICIST = "Physicist";
private static final String[] items = new String[]{
"Alpher", "Bethe", "Gamow", "Dirac", "Einstein"
};
private JComboBox combo = new JComboBox(items);
private String oldValue;
public ObservedPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObservedPanel"));
combo.addActionListener(new ComboBoxListener());
this.add(combo);
}
private class ComboBoxListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent ae) {
String newValue = (String) combo.getSelectedItem();
firePropertyChange(PHYSICIST, oldValue, newValue);
oldValue = newValue;
}
}
}
I'd be use JMenu with JMenuItems with contents layed by using CardLayout rather than very complicated JTabbedPane(s)
For my own reference, and for anyone using the Netbeans GUI builder, this works so far as it goes:
The PanelWithList class:
package dur.bounceme.net;
public class PanelWithList extends javax.swing.JPanel {
public PanelWithList() {
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">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jList1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jList1MouseClicked(evt);
}
});
jList1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jList1KeyReleased(evt);
}
});
jScrollPane1.setViewportView(jList1);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(167, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1))
.addContainerGap(166, Short.MAX_VALUE))
);
}// </editor-fold>
private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
row();
}
private void jList1KeyReleased(java.awt.event.KeyEvent evt) {
row();
}
// Variables declaration - do not modify
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
private void row() {
Object o = jList1.getSelectedValue();
String s = (String)o;
jTextArea1.setText(s);
this.firePropertyChange("list", -1, 1);
}
}
and Frame.java as a driver:
package dur.bounceme.net;
public class Frame extends javax.swing.JFrame {
public Frame() {
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">
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
panelWithList1 = new dur.bounceme.net.PanelWithList();
panelWithTable1 = new dur.bounceme.net.PanelWithTable();
newJPanel1 = new dur.bounceme.net.PanelWithCombo();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
panelWithList1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
panelWithList1PropertyChange(evt);
}
});
jTabbedPane1.addTab("tab1", panelWithList1);
jTabbedPane1.addTab("tab2", panelWithTable1);
jTabbedPane1.addTab("tab3", newJPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 937, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 913, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 555, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(22, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
private void panelWithList1PropertyChange(java.beans.PropertyChangeEvent evt) {
System.out.println("panelWithList1PropertyChange ");
}
/**
* #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(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame.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 Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTabbedPane jTabbedPane1;
private dur.bounceme.net.PanelWithCombo newJPanel1;
private dur.bounceme.net.PanelWithList panelWithList1;
private dur.bounceme.net.PanelWithTable panelWithTable1;
// End of variables declaration
}
The key being that panelWithList1PropertyChange is the listener on 'panelWithList' itself.
Because PanelWithList.firePropertyChange("list", -1, 1); cannot send Object nor String that I see, I'm not exactly sure how to get the value selected all the way from the JList up to the JFrame.
Hmm, well the API says yes it can send Objects. Have to check that out.
I think it's necessary to get some info about the model which the JList is using up to the JFrame. However, doesn't that break MVC? Or maybe that's the wrong approach.
How do I wire output to paneWithList?
PaneWithList has a listener on its JList so that the selected row is output to the console. How can I direct that output to the JTextPane on output?
Could PaneWithList fire an event which Main picks up? Would PropertyChangeSupport suffice?
Main.java:
package dur.bounceme.net;
import javax.swing.JTabbedPane;
public class Main {
private static JTabbedPane tabs;
private static PaneWithList paneWithList;
private static PaneWithTable paneWithTable;
private static Output output;
public static void main(String[] args) {
tabs = new javax.swing.JTabbedPane();
paneWithList = new PaneWithList();
paneWithTable = new PaneWithTable();
tabs.addTab("list", paneWithList);
tabs.addTab("table", paneWithTable);
tabs.addTab("output", output);
}
}
Here's an example using the observer pattern, also seen here, here and here. Note that it would also be possible to listen to the combo's model.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
/**
* #see http://en.wikipedia.org/wiki/Observer_pattern
* #see https://stackoverflow.com/a/10523401/230513
*/
public class PropertyChangeDemo {
public PropertyChangeDemo() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.add(new ObserverPanel());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
PropertyChangeDemo example = new PropertyChangeDemo();
}
});
}
}
class ObserverPanel extends JPanel {
private JLabel title = new JLabel("Value received: ");
private JLabel label = new JLabel("null", JLabel.CENTER);
public ObserverPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObserverPanel"));
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(title);
panel.add(label);
this.add(panel);
ObservedPanel observed = new ObservedPanel();
observed.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName().equals(ObservedPanel.PHYSICIST)) {
String value = e.getNewValue().toString();
label.setText(value);
}
}
});
this.add(observed);
}
}
class ObservedPanel extends JPanel {
public static final String PHYSICIST = "Physicist";
private static final String[] items = new String[]{
"Alpher", "Bethe", "Gamow", "Dirac", "Einstein"
};
private JComboBox combo = new JComboBox(items);
private String oldValue;
public ObservedPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObservedPanel"));
combo.addActionListener(new ComboBoxListener());
this.add(combo);
}
private class ComboBoxListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent ae) {
String newValue = (String) combo.getSelectedItem();
firePropertyChange(PHYSICIST, oldValue, newValue);
oldValue = newValue;
}
}
}
I'd be use JMenu with JMenuItems with contents layed by using CardLayout rather than very complicated JTabbedPane(s)
For my own reference, and for anyone using the Netbeans GUI builder, this works so far as it goes:
The PanelWithList class:
package dur.bounceme.net;
public class PanelWithList extends javax.swing.JPanel {
public PanelWithList() {
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">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jList1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jList1MouseClicked(evt);
}
});
jList1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jList1KeyReleased(evt);
}
});
jScrollPane1.setViewportView(jList1);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(167, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1))
.addContainerGap(166, Short.MAX_VALUE))
);
}// </editor-fold>
private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
row();
}
private void jList1KeyReleased(java.awt.event.KeyEvent evt) {
row();
}
// Variables declaration - do not modify
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
private void row() {
Object o = jList1.getSelectedValue();
String s = (String)o;
jTextArea1.setText(s);
this.firePropertyChange("list", -1, 1);
}
}
and Frame.java as a driver:
package dur.bounceme.net;
public class Frame extends javax.swing.JFrame {
public Frame() {
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">
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
panelWithList1 = new dur.bounceme.net.PanelWithList();
panelWithTable1 = new dur.bounceme.net.PanelWithTable();
newJPanel1 = new dur.bounceme.net.PanelWithCombo();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
panelWithList1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
panelWithList1PropertyChange(evt);
}
});
jTabbedPane1.addTab("tab1", panelWithList1);
jTabbedPane1.addTab("tab2", panelWithTable1);
jTabbedPane1.addTab("tab3", newJPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 937, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 913, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 555, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(22, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
private void panelWithList1PropertyChange(java.beans.PropertyChangeEvent evt) {
System.out.println("panelWithList1PropertyChange ");
}
/**
* #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(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame.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 Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTabbedPane jTabbedPane1;
private dur.bounceme.net.PanelWithCombo newJPanel1;
private dur.bounceme.net.PanelWithList panelWithList1;
private dur.bounceme.net.PanelWithTable panelWithTable1;
// End of variables declaration
}
The key being that panelWithList1PropertyChange is the listener on 'panelWithList' itself.
Because PanelWithList.firePropertyChange("list", -1, 1); cannot send Object nor String that I see, I'm not exactly sure how to get the value selected all the way from the JList up to the JFrame.
Hmm, well the API says yes it can send Objects. Have to check that out.
I think it's necessary to get some info about the model which the JList is using up to the JFrame. However, doesn't that break MVC? Or maybe that's the wrong approach.
I want to display an image from the web to a panel in another JFrame at the click of a button. Whenever I click the button, first the image loads; and during this time, the current form potentially freezes. Once the image has loaded, the form is displayed with the image. How can I avoid the situation where my form freezes since it is very irritating. Among my codes:
My current class:
private void btn_TrackbusActionPerformed(java.awt.event.ActionEvent evt) {
try {
sendMessage("Query,map,$,start,211,Arsenal,!");
System.out.println(receiveMessage());
} catch (UnknownHostException ex) {
Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception ex) {
Logger.getLogger(client_Trackbus.class.getName()).log(Level.SEVERE, null, ex);
}
client_trackedbus nextform=new client_trackedbus(planform,connection,packet_receive,packet_send);
this.setVisible(false);
this.dispose();
nextform.setVisible(true);
// TODO add your handling code here:
}
My next class that displays the image:
public class client_trackedbus extends javax.swing.JFrame {
client_planform planform=null;
DatagramSocket connection=null;
DatagramPacket packet_receive=null;
DatagramPacket packet_send=null;
JLabel label=null;
/** Creates new form client_trackedbus */
public client_trackedbus(client_planform planform,DatagramSocket connection,DatagramPacket packet_receive,DatagramPacket packet_send) {
initComponents();
this.planform=planform;
this.connection=connection;
this.packet_receive=packet_receive;
this.packet_send=packet_send;
try {
displayMap("http://www.huddletogether.com/projects/lightbox2/images/image-2.jpg", jPanel1, new JLabel());
} catch (MalformedURLException ex) {
Logger.getLogger(client_trackedbus.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void displayMap(String url,JPanel panel,JLabel label) throws MalformedURLException{
URL imageurl=new URL(url);
Image image=(Toolkit.getDefaultToolkit().createImage(imageurl));
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
panel.add(label);
// System.out.println(panel.getSize().width);
this.getContentPane().add(panel);
}
/** 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();
jLabel1 = new javax.swing.JLabel();
btn_Exit = new javax.swing.JButton();
btn_Plan = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Public Transport Journey Planner");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 368, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 172, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Arial", 1, 18));
jLabel1.setText("Your tracked bus");
btn_Exit.setText("Exit");
btn_Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_ExitActionPerformed(evt);
}
});
btn_Plan.setText("Plan journey");
btn_Plan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_PlanActionPerformed(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(104, 104, 104)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addComponent(btn_Plan)
.addGap(65, 65, 65)
.addComponent(btn_Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(20, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jPanel1, 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(btn_Exit)
.addComponent(btn_Plan))
.addContainerGap(12, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btn_ExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Exitform();
}
private void btn_PlanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
this.dispose();
this.planform.setVisible(true);
}
private void Exitform(){
this.setVisible(false);
this.dispose();
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new client_trackedbus().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_Exit;
private javax.swing.JButton btn_Plan;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
As suggested in the article Concurrency in Swing, your button handler's query may be blocking the event dispatch thread. Using javax.swing.SwingWorker is one approach to loading images in the background, while displaying progress and keeping the GUI thread alive.
Addendum: Here's a sscce that loads the SO logo; it's been updated to handle exceptions and resize the enclosing container to fit the loaded image:
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
* #see http://stackoverflow.com/questions/4530659
*/
public final class WorkerTest extends JFrame {
private final JLabel label = new JLabel("Loading...");
public WorkerTest() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
this.add(label);
this.pack();
this.setLocationRelativeTo(null);
}
private void start() {
new ImageWorker().execute();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
WorkerTest wt = new WorkerTest();
wt.setVisible(true);
wt.start();
}
});
}
class ImageWorker extends SwingWorker<Image, Void> {
private static final String TEST
= "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
#Override
protected Image doInBackground() throws IOException {
Image image = ImageIO.read(new URL(TEST));
return image.getScaledInstance(640, -1, Image.SCALE_SMOOTH);
}
#Override
protected void done() {
try {
ImageIcon icon = new ImageIcon(get());
label.setIcon(icon);
label.setText("Done");
WorkerTest.this.pack();
WorkerTest.this.setLocationRelativeTo(null);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
public class SSBTest extends javax.swing.JFrame {
/** Creates new form worker1 */
public SSBTest() {
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">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 348, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 210, Short.MAX_VALUE));
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(196, Short.MAX_VALUE).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(178, 178, 178)).addGroup(layout.createSequentialGroup().addGap(86, 86, 86).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(122, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE).addComponent(jLabel1).addGap(36, 36, 36)));
pack();
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
final SSBTest ssbTest = new SSBTest();
ssbTest.setVisible(true);
ssbTest.execute();
}
});
}
private void execute() {
(new MeaningOfLifeFinder(jLabel1, jPanel1)).execute();
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
class MeaningOfLifeFinder extends SwingWorker<Icon, Object> {
JLabel label = null;
JPanel panel;
MeaningOfLifeFinder(JLabel label, JPanel jpanel) {
this.label = label;
this.panel = jpanel;
}
protected Icon doInBackground() throws IOException {
URL imageurl;
Image image = null;
System.out.println("image loading");
imageurl = new URL("http://maps.google.com/maps/api/staticmap"
+ "?zoom=14&size=512x512&maptype=roadmap"
+ "&markers=color:green|label:21|-15.0,-150.0&sensor=false");
//image = (Toolkit.getDefaultToolkit().createImage(imageurl));
image = ImageIO.read(imageurl);
ImageIcon icon = new ImageIcon(image);
System.out.println("image loaded...");
return icon;
}
#Override
protected void done() {
try {
System.out.println("image adding to label...");
label.setIcon(get());
//panel.add(label);
System.out.println("image loaded to label...");
} catch (Exception ignore) {
}
}
// System.out.println(panel.getSize().width);
}