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");
}
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!
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 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.
I have a jframe that ontains two JRadioButton.
public class jradioButtontest extends javax.swing.JFrame {
public jradioButtontest() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jRadioButton1.setText("jRadioButton1");
jRadioButton2.setText("jRadioButton2");
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(198, 198, 198)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jRadioButton2)
.addComponent(jRadioButton1))
.addContainerGap(203, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addComponent(jRadioButton1)
.addGap(18, 18, 18)
.addComponent(jRadioButton2)
.addContainerGap(225, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new jradioButtontest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
// End of variables declaration
}
I want when i select first JRadioButton, other JRadioButton should be unselected.
I use ready jframe, how can change netbeans generated code?
How do this?
By default, a new JRadioButton is created in its own ButtonGroup, which means they can both be selected independently.
If you want the two buttons linked so that only one can be picked at a time, you need to add:
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
After you have constructed the buttons.
Add the 2 JRadioButtons to a single javax.swing.ButtonGroup instance.
See: How to Use the ButtonGroup Component
Use ButtonGroup :
ButtonGroup buttongroup = new ButtonGroup();
buttongroup.add(...);
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