I've connected to Solaris 11 from my windows machine. I've set DISPLAY to my machine. And I'm using Java 8.
Note: This worked fine when using Java 6.
When I'm launching dialog then its button and other swing components are not getting rendered.
Observed that it works on
o Windows 7 Enterprise
o Windows Server 2012 Enterprise
I tried changing the L&F but that didn't work. When I used "GTKLookAndFeel" then buttons appeared but without text/label.
Any help is greatly appreciated. Kindly let me know if any elaboration needed. Thanks.
Code for my dialog is as follows
package com.ui;
import javax.swing.SwingUtilities;
public class SimpleDialog extends java.awt.Dialog {
private static final long serialVersionUID = -8298472889742780386L;
public SimpleDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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">//GEN-BEGIN:initComponents
private void initComponents() {
btnSkip = new javax.swing.JButton();
btnRetry = new javax.swing.JButton();
btnAbort = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
lblMessage = new javax.swing.JTextArea();
btnViewLog = new javax.swing.JButton();
setLocationRelativeTo(null);
setMinimumSize(new java.awt.Dimension(600, 200));
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
setName("Form"); // NOI18N
setResizable(false);
setTitle("Simple Dialog");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
btnSkip.setText("Skip this Step"); // NOI18N
btnSkip.setName("btnSkip"); // NOI18N
btnSkip.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSkip_Click(evt);
}
});
btnRetry.setText("Retry"); // NOI18N
btnRetry.setName("btnRetry"); // NOI18N
btnRetry.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRetry_Click(evt);
}
});
btnAbort.setText("Abort"); // NOI18N
btnAbort.setName("btnAbort"); // NOI18N
btnAbort.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAbort_Click(evt);
}
});
jScrollPane1.setName("jScrollPane1"); // NOI18N
lblMessage.setColumns(20);
lblMessage.setEditable(false); // NOI18N
lblMessage.setLineWrap(true);
lblMessage.setRows(5);
lblMessage.setName("lblMessage"); // NOI18N
jScrollPane1.setViewportView(lblMessage);
btnViewLog.setText("View log"); // NOI18N
btnViewLog.setName("btnViewLog"); // NOI18N
btnViewLog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// open some log file
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnSkip)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnRetry)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
.addComponent(btnViewLog)
.addGap(79, 79, 79)
.addComponent(btnAbort)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSkip)
.addComponent(btnAbort)
.addComponent(btnRetry)
.addComponent(btnViewLog))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
setVisible(false);
dispose();
}//GEN-LAST:event_closeDialog
private void btnAbort_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbort_Click
this.setVisible(false);
}//GEN-LAST:event_btnAbort_Click
private void btnRetry_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRetry_Click
this.setVisible(false);
}//GEN-LAST:event_btnRetry_Click
private void btnSkip_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSkip_Click
this.setVisible(false);
}//GEN-LAST:event_btnSkip_Click
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SimpleDialog dialog = new SimpleDialog(new java.awt.Frame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnAbort;
private javax.swing.JButton btnRetry;
private javax.swing.JButton btnSkip;
private javax.swing.JButton btnViewLog;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea lblMessage;
// End of variables declaration//GEN-END:variables
}
To apply look and feel you need to provide two commands:
UIManager.setLookAndFeel(lnf_name);
SwingUtilities.updateComponentTreeUI(root_obj_or_specific_one);
I had a similar problem and 2nd line solved the problem.
Try this
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Let me know if it works or not. :)
Related
Both the videoplayer and HTML contains external jar files in its library which are jna-3.5.2.jar,platform-3.5.2.jar and vlcj-2.4.1.jar.
Both videoplayer and HTML page are in different projects in Netbeans and I have included the external jar files mentioned above in both the projects.
This is the code of videoplayer class.
package Player;
import com.sun.jna.NativeLibrary;
import java.awt.BorderLayout;
import java.awt.Color;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class videoplayer extends javax.swing.JApplet{
public EmbeddedMediaPlayerComponent mediaPlayerComponent;
#Override
public void init() {
try{
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"./src");
NativeLibrary.addSearchPath(RuntimeUtil.getPluginsDirectoryName(),"./src");
setBackground(Color.RED);
setSize(500,500);
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
}
catch(Exception e){
System.out.println(e);
}
/* 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(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(videoplayer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
// JApplet jp=new JApplet();
//jp.setLocation(100,100);
//jp.setSize(500,500);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* This method is called from within the init() method 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() {
jFrame1 = new javax.swing.JFrame();
jFrame2 = new javax.swing.JFrame();
jToolBar1 = new javax.swing.JToolBar();
videopanel = new javax.swing.JPanel();
lowerpanel = new javax.swing.JPanel();
Play = new javax.swing.JButton();
Pause = new javax.swing.JButton();
Forward = new javax.swing.JButton();
Backward = new javax.swing.JButton();
Snapshot = new javax.swing.JButton();
Volume = new javax.swing.JSlider();
upperpanel = new javax.swing.JPanel();
exitfullscreen = new javax.swing.JButton();
Open = new javax.swing.JButton();
Stop = new javax.swing.JButton();
fullscreen = new javax.swing.JButton();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
jFrame2.getContentPane().setLayout(jFrame2Layout);
jFrame2Layout.setHorizontalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame2Layout.setVerticalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
jToolBar1.setRollover(true);
setBackground(new java.awt.Color(51, 51, 51));
addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
formPropertyChange(evt);
}
});
videopanel.setBackground(new java.awt.Color(51, 51, 51));
lowerpanel.setBackground(new java.awt.Color(0, 0, 0));
Play.setBackground(new java.awt.Color(0, 0, 0));
Play.setForeground(new java.awt.Color(255, 0, 0));
Play.setText("Play");
Play.setToolTipText("");
Play.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PlayActionPerformed(evt);
}
});
Pause.setBackground(new java.awt.Color(0, 0, 0));
Pause.setForeground(new java.awt.Color(255, 0, 0));
Pause.setText("Pause");
Pause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PauseActionPerformed(evt);
}
});
Forward.setBackground(new java.awt.Color(0, 0, 0));
Forward.setForeground(new java.awt.Color(255, 0, 0));
Forward.setText("Fwd");
Forward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ForwardActionPerformed(evt);
}
});
Backward.setBackground(new java.awt.Color(0, 0, 0));
Backward.setForeground(new java.awt.Color(255, 0, 0));
Backward.setText("Bwd");
Backward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BackwardActionPerformed(evt);
}
});
Snapshot.setBackground(new java.awt.Color(0, 0, 0));
Snapshot.setForeground(new java.awt.Color(255, 0, 0));
Snapshot.setText("Snapshot");
Snapshot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SnapshotActionPerformed(evt);
}
});
Volume.setBackground(new java.awt.Color(0, 0, 0));
Volume.setForeground(new java.awt.Color(255, 51, 0));
Volume.setMajorTickSpacing(2);
Volume.setMaximum(200);
Volume.setMinorTickSpacing(1);
Volume.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
VolumeStateChanged(evt);
}
});
javax.swing.GroupLayout lowerpanelLayout = new javax.swing.GroupLayout(lowerpanel);
lowerpanel.setLayout(lowerpanelLayout);
lowerpanelLayout.setHorizontalGroup(
lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(lowerpanelLayout.createSequentialGroup()
.addComponent(Play, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Pause)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Forward)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Backward)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Snapshot)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Volume, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(308, Short.MAX_VALUE))
);
lowerpanelLayout.setVerticalGroup(
lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(lowerpanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Volume, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(lowerpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Play)
.addComponent(Pause)
.addComponent(Forward)
.addComponent(Backward)
.addComponent(Snapshot)))
.addContainerGap(14, Short.MAX_VALUE))
);
upperpanel.setBackground(new java.awt.Color(0, 0, 0));
upperpanel.setForeground(new java.awt.Color(255, 255, 255));
exitfullscreen.setBackground(new java.awt.Color(0, 0, 0));
exitfullscreen.setForeground(new java.awt.Color(255, 0, 0));
exitfullscreen.setText("Exit Full Screen");
exitfullscreen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitfullscreenActionPerformed(evt);
}
});
javax.swing.GroupLayout upperpanelLayout = new javax.swing.GroupLayout(upperpanel);
upperpanel.setLayout(upperpanelLayout);
upperpanelLayout.setHorizontalGroup(
upperpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, upperpanelLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(exitfullscreen)
.addContainerGap())
);
upperpanelLayout.setVerticalGroup(
upperpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, upperpanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(exitfullscreen))
);
Open.setBackground(new java.awt.Color(0, 0, 0));
Open.setForeground(new java.awt.Color(255, 0, 0));
Open.setText("Open");
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
Stop.setBackground(new java.awt.Color(0, 0, 0));
Stop.setForeground(new java.awt.Color(255, 0, 0));
Stop.setText("Stop");
Stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StopActionPerformed(evt);
}
});
fullscreen.setBackground(new java.awt.Color(0, 0, 0));
fullscreen.setForeground(new java.awt.Color(255, 0, 0));
fullscreen.setText("Full Screen");
fullscreen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fullscreenActionPerformed(evt);
}
});
javax.swing.GroupLayout videopanelLayout = new javax.swing.GroupLayout(videopanel);
videopanel.setLayout(videopanelLayout);
videopanelLayout.setHorizontalGroup(
videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lowerpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(videopanelLayout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(Open)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Stop, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addComponent(fullscreen)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(upperpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
videopanelLayout.setVerticalGroup(
videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, videopanelLayout.createSequentialGroup()
.addGroup(videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(upperpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(videopanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Open)
.addComponent(Stop)
.addComponent(fullscreen)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 439, Short.MAX_VALUE)
.addComponent(lowerpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(videopanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(videopanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
}// </editor-fold>
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
BorderLayout blmain=new BorderLayout();
BorderLayout blvideo=new BorderLayout();
setLayout(blmain);
videopanel.setLayout(blvideo);
videopanel.add(mediaPlayerComponent,blmain.CENTER);
videopanel.add(upperpanel,blmain.NORTH);
videopanel.add(lowerpanel,blmain.SOUTH);
videopanel.validate();
mediaPlayerComponent.getMediaPlayer().prepareMedia(".\\src\\abc.mp4");
mediaPlayerComponent.getMediaPlayer().setVolume(50);
}
catch(Exception e){
System.out.println(e);
}
}
private void PlayActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().play();
}
private void PauseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().pause();
}
private void ForwardActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().skip(10000);
}
private void BackwardActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().skip(-10000);
}
private void SnapshotActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().setSnapshotDirectory("D:\\");
mediaPlayerComponent.getMediaPlayer().saveSnapshot();
}
private void VolumeStateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().setVolume(Volume.getValue());
}
private void fullscreenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
videopanel.setSize(1366,650);
setSize(videopanel.getSize());
videopanel.revalidate();
upperpanel.revalidate();
lowerpanel.revalidate();
}
private void StopActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaPlayerComponent.getMediaPlayer().stop();
videopanel.remove(mediaPlayerComponent);
}
private void formPropertyChange(java.beans.PropertyChangeEvent evt) {
// TODO add your handling code here:
}
private void exitfullscreenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setSize(500,500);
videopanel.setSize(500,500);
}
/* */
// Variables declaration - do not modify
private javax.swing.JButton Backward;
private javax.swing.JButton Forward;
private javax.swing.JButton Open;
private javax.swing.JButton Pause;
private javax.swing.JButton Play;
private javax.swing.JButton Snapshot;
private javax.swing.JButton Stop;
private javax.swing.JSlider Volume;
private javax.swing.JButton exitfullscreen;
private javax.swing.JButton fullscreen;
private javax.swing.JFrame jFrame1;
private javax.swing.JFrame jFrame2;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JPanel lowerpanel;
private javax.swing.JPanel upperpanel;
private javax.swing.JPanel videopanel;
// End of variables declaration
}
The code of HTML page
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<applet code ="Player.videoplayer" archive="VLCMediaPlayer.jar" height="500" width ="500"></applet>
</body>
</html>
When I run the applet separately it works fine but when I run it on the HTML page and click on any of its button I get the following error :
Exception in thread "AWT-EventQueue-2" java.lang.ExceptionInInitializerError
Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "jna.tmpdir" "read")
As you can read from the title, i need to set the text of the JTextField by pressing the buttons.. and also the buttons should disappear when clicked(which i did with the method 'setVisible'), but also when you delete a letter the button must appear again..
But mostly focus on "How to add text to a single text field from many buttons".. each button when pressed must add a Letter,
the code i did so far//
Note : I'm working on NetBeans IDE
public class Test extends javax.swing.JPanel {
/**
* Creates new form Test
*/
public Test() {
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() {
jPanel1 = new javax.swing.JPanel();
k = new javax.swing.JButton();
o = new javax.swing.JButton();
a1 = new javax.swing.JButton();
a = new javax.swing.JButton();
n = new javax.swing.JButton();
d = new javax.swing.JButton();
r = new javax.swing.JButton();
b = new javax.swing.JButton();
u = new javax.swing.JButton();
s = new javax.swing.JButton();
l = new javax.swing.JButton();
l1 = new javax.swing.JButton();
txtField = new javax.swing.JTextField();
c = new javax.swing.JButton();
k.setText("K");
k.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kActionPerformed(evt);
}
});
o.setText("O");
o.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oActionPerformed(evt);
}
});
a1.setText("A");
a1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
a1ActionPerformed(evt);
}
});
a.setText("A");
a.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aActionPerformed(evt);
}
});
n.setText("N");
n.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nActionPerformed(evt);
}
});
d.setText("D");
d.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dActionPerformed(evt);
}
});
r.setText("R");
r.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rActionPerformed(evt);
}
});
b.setText("B");
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bActionPerformed(evt);
}
});
u.setText("U");
u.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
uActionPerformed(evt);
}
});
s.setText("S");
s.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sActionPerformed(evt);
}
});
l.setText("L");
l.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lActionPerformed(evt);
}
});
l1.setText("L");
l1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
l1ActionPerformed(evt);
}
});
txtField.setFont(new java.awt.Font("Serif", 1, 12)); // NOI18N
txtField.setFocusable(false);
txtField.setMaximumSize(new java.awt.Dimension(202, 31));
txtField.setMinimumSize(new java.awt.Dimension(202, 31));
txtField.setOpaque(false);
txtField.setPreferredSize(new java.awt.Dimension(202, 31));
txtField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtFieldActionPerformed(evt);
}
});
c.setText("C");
c.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cActionPerformed(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()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(b)
.addComponent(s))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(u)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(l)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(l1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(o)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(c)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(k))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(a)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(n)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(d)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(r)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(a1)))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txtField, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(addd)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(txtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(addd)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(n)
.addComponent(d)
.addComponent(r)
.addComponent(s)
.addComponent(a)
.addComponent(a1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b)
.addComponent(u)
.addComponent(l)
.addComponent(l1)
.addComponent(o)
.addComponent(k)
.addComponent(c))
.addGap(19, 19, 19))
);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/test3/fotot/sanda.jpg"))); // NOI18N
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
private void sActionPerformed(java.awt.event.ActionEvent evt) {
s.setVisible(false);
}
private void aActionPerformed(java.awt.event.ActionEvent evt) {
a.setVisible(false);
}
private void nActionPerformed(java.awt.event.ActionEvent evt) {
n.setVisible(false);
}
private void dActionPerformed(java.awt.event.ActionEvent evt) {
d.setVisible(false);
}
private void rActionPerformed(java.awt.event.ActionEvent evt) {
r.setVisible(false);
}
private void a1ActionPerformed(java.awt.event.ActionEvent evt) {
a1.setVisible(false);
}
private void bActionPerformed(java.awt.event.ActionEvent evt) {
b.setVisible(false);
}
private void uActionPerformed(java.awt.event.ActionEvent evt) {
u.setVisible(false);
}
private void lActionPerformed(java.awt.event.ActionEvent evt) {
l.setVisible(false);
}
private void l1ActionPerformed(java.awt.event.ActionEvent evt) {
l1.setVisible(false);
}
private void oActionPerformed(java.awt.event.ActionEvent evt) {
o.setVisible(false);
}
private void kActionPerformed(java.awt.event.ActionEvent evt) {
k.setVisible(false);
}
private void cActionPerformed(java.awt.event.ActionEvent evt) {
c.setVisible(false);
}
// Variables declaration - do not modify
private javax.swing.JButton a;
private javax.swing.JButton a1;
private javax.swing.JButton b;
private javax.swing.JButton c;
private javax.swing.JButton d;
private javax.swing.JButton k;
private javax.swing.JButton l;
private javax.swing.JButton l1;
private javax.swing.JButton n;
private javax.swing.JButton o;
private javax.swing.JButton r;
private javax.swing.JButton s;
public javax.swing.JTextField txtField;
private javax.swing.JButton u;
// End of variables declaration
}
Any help or suggestion is more than welcome!!
Thank you.
Do you mean something as follows?:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TestTextField {
private JTextField textField;
private Map<Character, JButton> buttons = new HashMap<>();
private void initUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField(30);
frame.add(textField, BorderLayout.NORTH);
JPanel panel = new JPanel(new GridLayout(0, 8, 15, 15));
for (int i = 0; i < 26; i++) {
final char c = (char) ('A' + i);
final JButton button = new JButton(String.valueOf(c));
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + c);
}
});
buttons.put(c, button);
panel.add(button);
}
textField.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e) {
updateButtonVisibility();
}
#Override
public void insertUpdate(DocumentEvent e) {
updateButtonVisibility();
}
#Override
public void changedUpdate(DocumentEvent e) {
updateButtonVisibility();
}
});
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
protected void updateButtonVisibility() {
String text = textField.getText();
for (Entry<Character, JButton> e : buttons.entrySet()) {
e.getValue().setVisible(!text.toUpperCase().contains(String.valueOf(e.getKey())));
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
TestTextField test = new TestTextField();
test.initUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Yes, I am aware that this question has been asked before; however, our methods of conducting the functions are different and for this reason I believe that this should not be marked as a duplicate.
I am trying to make a Calculator GUI Function, but currently all I can do is have the user click on a number, and then have that number printed out to the screen.
When I click the divide function (this is the only one I have set up currently), "1" is printed out to the screen. Obviously this is not an error, since I am getting the user's input (the number clicked) twice without change. That number divided by itself (which is essentially what I am doing right now) is 1.
This is my current code:
package my.calculator;
public class CalculatorUI extends javax.swing.JFrame
{
public CalculatorUI()
{
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Output = new javax.swing.JTextField();
One = new javax.swing.JButton();
Two = new javax.swing.JButton();
Three = new javax.swing.JButton();
Four = new javax.swing.JButton();
Five = new javax.swing.JButton();
Six = new javax.swing.JButton();
Seven = new javax.swing.JButton();
Eight = new javax.swing.JButton();
Nine = new javax.swing.JButton();
Zero = new javax.swing.JButton();
Divide = new javax.swing.JButton();
Multiply = new javax.swing.JButton();
Subtract = new javax.swing.JButton();
Add = new javax.swing.JButton();
Clear = new javax.swing.JButton();
Calc = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Output.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
OutputPropertyChange(evt);
}
});
One.setText("1");
One.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OneActionPerformed(evt);
}
});
Two.setText("2");
Two.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TwoActionPerformed(evt);
}
});
Three.setText("3");
Three.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ThreeActionPerformed(evt);
}
});
Four.setText("4");
Four.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FourActionPerformed(evt);
}
});
Five.setText("5");
Five.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FiveActionPerformed(evt);
}
});
Six.setText("6");
Six.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SixActionPerformed(evt);
}
});
Seven.setText("7");
Seven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SevenActionPerformed(evt);
}
});
Eight.setText("8");
Eight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EightActionPerformed(evt);
}
});
Nine.setText("9");
Nine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NineActionPerformed(evt);
}
});
Zero.setText("0");
Zero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ZeroActionPerformed(evt);
}
});
Divide.setText("/");
Divide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DivideActionPerformed(evt);
}
});
Multiply.setText("X");
Subtract.setText("-");
Add.setText("+");
Clear.setText("C");
Clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearActionPerformed(evt);
}
});
Calc.setText("=");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Output)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(One)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Two)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Three, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(Four)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Five)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Six, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Seven)
.addComponent(Clear))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(Eight)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Nine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(Zero)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Calc)))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Add)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(Subtract, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Multiply, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Divide, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Output, 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.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Three, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(One)
.addComponent(Two))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Four)
.addComponent(Five)
.addComponent(Six)
.addComponent(Multiply))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Seven)
.addComponent(Eight)
.addComponent(Nine)
.addComponent(Subtract))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Zero)
.addComponent(Add)
.addComponent(Calc)
.addComponent(Clear)))
.addComponent(Divide, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
);
pack();
}// </editor-fold>
private void OneActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(1));
}
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(""));
}
private void TwoActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(2));
}
private void ThreeActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(3));
}
private void FourActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(4));
}
private void FiveActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(5));
}
private void SixActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(6));
}
private void SevenActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(7));
}
private void EightActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(8));
}
private void NineActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(9));
}
private void ZeroActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(0));
}
private void DivideActionPerformed(java.awt.event.ActionEvent evt) {
int num1 = Integer.parseInt(Output.getText());
int number = Integer.parseInt(Output.getText());
int calculate = num1 / number;
Output.setText(Integer.toString(calculate));
}
private void OutputPropertyChange(java.beans.PropertyChangeEvent evt) {
Output.setEditable(false); // TODO add your handling code here:
}
public static void main(String args[])
{
//<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(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CalculatorUI.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 CalculatorUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Add;
private javax.swing.JButton Calc;
private javax.swing.JButton Clear;
private javax.swing.JButton Divide;
private javax.swing.JButton Eight;
private javax.swing.JButton Five;
private javax.swing.JButton Four;
private javax.swing.JButton Multiply;
private javax.swing.JButton Nine;
private javax.swing.JButton One;
private javax.swing.JTextField Output;
private javax.swing.JButton Seven;
private javax.swing.JButton Six;
private javax.swing.JButton Subtract;
private javax.swing.JButton Three;
private javax.swing.JButton Two;
private javax.swing.JButton Zero;
// End of variables declaration
}
I would also like to include a decimal function (so that you can have doubles, etc), but am lost as to how to do so. Any and all help regarding both questions is greatly appreciated.
Thank you.
I know it's tempting but I consider a calculater (even if it's just for basic arithmetic) not quite as trivial as it might look for a beginner. How would you e.g. solve an expression like 3+3*2? What you need for starters is a Parser/Tokenizer an intermediate format like the polish notation and an algorithm to get the order right e.g. the shanting yard algorithm.
I've got a couple JInternalFrames inside a DesktopPane, and I've noticed something with the first time they open.
When I first open one, and then open a second one, the first JInternalFrame will be on top of the second. I want them to open on top of each other, but this only happens the first time. I realize now that it is because of the order that they are added. But my program will be retrieving information right at the desktoppane load so I need the JIF variables to be added before the windows are actually opened. So how can I make sure the window that is opened comes out on top regardless of the order they are added?
I would prefer not to use a dialogbox.
Try this code...
I've create a sample application for you to understand...
public class NewJFrame extends javax.swing.JFrame {
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane1;
//This is a Internal Frame I have created
private PaymentInternalFrame payIF;
public NewJFrame() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
jDesktopPane1 = new javax.swing.JDesktopPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Click");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
jDesktopPane1.setLayout(jDesktopPane1Layout);
jDesktopPane1Layout.setHorizontalGroup(
jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jDesktopPane1Layout.setVerticalGroup(
jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 412, 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(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
.addComponent(jDesktopPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jDesktopPane1))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
if(payIF==null||payIF.isClosed()){
payIF = new PaymentInternalFrame();
mainDesktop.add(payIF);
payIF.setVisible(true);
payIF.setMaximum(true);
payIF.isSelected();
}else{
payIF.setSelected(true);
}
} catch (PropertyVetoException e) {
NotificationMessage.errorMessage(this, e);
}
}
//Main Method...
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
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);