I need help in the running of the following Java program.
I want the main program to pause running after it displays a new JFrame form and resume after the new window is closed ( or Next button is clicked ) .
So The 10 Forms should come sequentially after I press the next button not altogether at once!
MainClass.java
public class MainClass {
void Run()
{
for(int i=0;i<10;i++)
new NewForm().setVisible(true);
}
public static void main (String[] args)
{
new MainClass().Run();
}
}
NewForm.java
public class NewForm extends javax.swing.JFrame {
public NewForm() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jButton1.setText("Next");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(155, 155, 155)
.addComponent(jButton1)
.addContainerGap(190, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(131, 131, 131)
.addComponent(jButton1)
.addContainerGap(146, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewForm().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
}
Setting jframe visible will not wait until the jframe is closed, thats why 10 jframes pops at you. You need to display just one, then add a ActionLisetener to your button. In this action, you just close your panel and open onother one. Of coursce you will need some global counter, consider making i static for start.
Related
I'm trying to make a game in Java, and I decided to use GIFs as animations in a JFrame. However, the program keeps showing past frames of the GIF, so the whole image looks like a mess.
This is what the GIF should look like in the program:
However, the GIF ends up looking like this (this is a screenshot of what appears on the JFrame):
And here is my code so far. I'm using NetBeans with Java 8.0.2.
package giftest;
public class MainWindow extends javax.swing.JFrame {
public MainWindow() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giftest/Chespin2.gif"))); // NOI18N
jLabel1.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(158, 158, 158)
.addComponent(jLabel1)
.addContainerGap(12, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(jLabel1)
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
// End of Program
The effect can be reproduced with these lines of code:
import javax.swing.*;
import java.net.*;
public class AnimatedGifTest {
AnimatedGifTest() throws MalformedURLException {
URL url = new URL(
"https://i.stack.imgur.com/y9O4G.gif");
ImageIcon ii = new ImageIcon(url);
JOptionPane.showMessageDialog(null, ii);
}
public static void main(String[] args) {
Runnable r = () -> {
try {
new AnimatedGifTest();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
};
SwingUtilities.invokeLater(r);
}
}
I'm using Java Swing, and I'm using jLabels and putting the GIF as the icon. Is there a way to fix this issue in NetBeans on Java 8.0.2? Thanks in advance!
My action button reads a JTextField value and updates that same field with only the digits included in the original value. It also copies the digits to clipboard automatically and makes a JLabel visible, letting the user know it's copied. This label goes invisible again as soon as the user types again in the text field.
Example: user inputs "abc123cde456" and clicks the action button. The output is "123456". A label is shown, telling the value was copied.
I've made this button default with getRootPane().setDefaultButton(button) so the user can trigger it using Enter key. The problem is, when using the key instead of mouse clicking, the output and copy to clipboard work, but the JLabel does not get visible.
I noticed the Enter key works properly when I set the focus off the JTextField before hitting the key, but the only way to do that in my layout is to hold the mouse click on top of the button and "drag it" outside, so the focus goes to the button instead of JTextField, like this:
passing focus to button before hitting Enter key
Code below - by the way, I'm working on NetBeans IDE:
JFrame class
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
public class FrameZ extends javax.swing.JFrame {
public FrameZ() {
initComponents();
labelCopied.setVisible(false);
inputTxt.addActionListener(actionButton.getActionListeners()[0]);
getRootPane().setDefaultButton(actionButton);
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
inputTxt = new javax.swing.JTextField();
actionButton = new javax.swing.JButton();
labelCopied = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
inputTxt.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
inputTxtKeyTyped(evt);
}
});
actionButton.setText("Only numbers");
actionButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionButtonActionPerformed(evt);
}
});
labelCopied.setText("Copied!");
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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(labelCopied)
.addGap(72, 72, 72))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(inputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
.addComponent(actionButton)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(actionButton)
.addComponent(inputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelCopied)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void actionButtonActionPerformed(java.awt.event.ActionEvent evt) {
String inputValue = inputTxt.getText();
StringBuilder digitsOnly = new StringBuilder();
for (int i = 0; i < inputValue.length(); i++) {
char c = inputValue.charAt(i);
if (Character.isDigit(c)) {
digitsOnly.append(c);
}
}
inputTxt.setText(digitsOnly.toString());
//copying to clipboard:
StringSelection strSelect = new StringSelection(inputTxt.getText());
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(strSelect, null);
labelCopied.setVisible(true);
}
private void inputTxtKeyTyped(java.awt.event.KeyEvent evt) {
labelCopied.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameZ().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton actionButton;
private javax.swing.JTextField inputTxt;
private javax.swing.JLabel labelCopied;
}
Main class
public class Main {
public static void main(String args[]){
FrameZ frm = new FrameZ();
frm.setVisible(true);
frm.setResizable(false);
frm.setLocationRelativeTo(null);
}
}
To problem solve you need to either:
use a debugger to step through the code
add System.out.println(...) statement to your code
Either of the above will help you understand the logic flow to see if it is what you expect.
I don't use an IDE so I made the following changes to add some debug code:
private void actionButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("action");
and
private void inputTxtKeyTyped(java.awt.event.KeyEvent evt) {
System.out.println("hide");
If you make these changes and type data in your text field and use the Enter key you will see output like:
hide
hide
hide
hide
action
hide
Why does this happen? Well, it would appear that the Enter key generates a KeyTyped event and this event is processed AFTER the ActionEvent
One way to fix this is to wrap the logic that makes the label visible in a SwingUtilities.invokeLater(...). This will cause the code to be added to the end of the Event Queue so it will be executed after the event generated by the Enter key:
//labelCopied.setVisible(true);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
labelCopied.setVisible(true);
}
});
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 made a simple GUI using Swing and everything is just working fine but the JLabel isn't updating when I used the .setText method. I'm really getting confused about the problem as the JLabel should work properly.
// Variables declaration
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
public NewClass() {
initComponents();
}
private void initComponents() {
jDesktopPane1 = new javax.swing.JDesktopPane();
jLabel1 = new javax.swing.JLabel();
jLabel1.setText("Hello JLabel!");
jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewClass().setVisible(true);
new NewClass().start();
}
});
}
private void start() {
this.jLabel1.setText("Hello!");
}
new NewClass().setVisible(true);
new NewClass().start();
You have created two instances of the NewClass class.
You only want one instance, then you can set the text for the label on the visible frame:
NewClass frame = new NewClass();
frame.setVisible(true);
frame.start();
If you want to change a property of any Object, then you need a reference to the Object. You can't just keep using the "new" statement.
I don't exactly understand what you are trying to do but if you are trying to update the jLabelText at the start of execution try this
public NewClass() {
initComponents();
this.setVisible(true);
jLabel1.setText("YourText");
}
I've got a problem with JTextField. I don't know how to pass a variable from a JTextField (situated in JFrame A) to another JTextField (situated in JFrame B).
I tried to do that, but it doesn't do anything, i.e. it doesn't get any runtime/compilation error nor it receives the text.
I tried to do this in ClassB:
ClassA a = new ClassA();
String text = a.jtextfield1.getText();
but it doesn't work!!
Could you help me, may with a simple example? What did I do wrong?
p.s.: i'm using netbeans
[edit --] That's the code of ClassA:
public class ClassA extends javax.swing.JFrame {
public ClassA() {
initComponents();
}
public void initComponents() {
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("Some text blah blah");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(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()
.addGap(33, 33, 33)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(124, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(86, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClassA().setVisible(true);
}
});
}
public javax.swing.JTextField jTextField1;
}
Here's the code of ClassB:
public class ClassB extends javax.swing.JFrame {
public ClassB() {
initComponents();
}
public ClassA a = new ClassA();
public void initComponents() {
jTextField1 = new javax.swing.JTextField();
getText = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getText.setText("GetText");
getText.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
getTextActionPerformed(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(31, 31, 31)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(getText)))
.addContainerGap(143, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(getText)
.addContainerGap(49, Short.MAX_VALUE))
);
pack();
}
public void getTextActionPerformed(java.awt.event.ActionEvent evt) {
a.jTextField1.getText(); //this doesn't work. How can I it makes work?
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClassB().setVisible(true);
}
});
}
public javax.swing.JButton getText;
public javax.swing.JTextField jTextField1;
}
Thanks in advance.
If you instantiate a new ClassA instance (and, BTW, the syntax is new ClassA()), then you will have... a new instance, with another jtextfield1 than the one in the existing ClassA instance. You need to pass a reference to the existing ClassA instance to the ClassB instance:
When ClassB is constructed:
ClassB theClassB = new ClassB(theClassA);
You don't seem to master the basic OO concepts of classes and objects, and not even the syntax of Java. I would advise not using Swing now, and learning the basics first. Swing is hard, much too hard for a developer which doesn't understand these concepts.
Read the basics and OO lessons of the Java tutorial
You ask for a simple example, you will get one. I leave it up to you how to match this example on your code, which shouldn't be too difficult.
public class PanelWithTextField extends JPanel{
private JTextField textField = new JTextField();
public JTextField getTextField(){ return textField; };
}
public static void main( String[] args ){
EventQueue.invokeLater(){ new Runnable(){
public void run(){
//create a first panel
PanelWithTextField panel = new PanelWithTextField();
panel.getTextField().setText( "Some text" );
//create a second panel
PanelWithTextField anotherPanel = new PanelWithTextField();
//copy the text from the first panel's textfield to the second panel's textfield
anotherPanel.getTextField().setText( panel.getTextField().getText() );
}
}
}
I hope I did not make too much typo's in it, as I did not try to run it nor did I used my IDE to write this code.
Basically you will need a reference to your first panel in your second panel (or in any part of the code where you which to access that textfield).
And as already suggested by others. You should start by making sure you understand the basic OO concepts and basic Java syntax before you start messing around with Swing and UI's