i just wanna count in jlabel. i think, I have tried all the solutions posted in the site but i couldn't find out any solution. I am a beginner, have one month to learn Java. I am sorry if my question is too stupid.
package asdf;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JLabel;
public class asd extends JFrame implements ActionListener {
int a=0; // variable
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asd window = new asd();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public asd() {
super();
Timer time=new Timer(1000, this);
time.start();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(20, 11, 137, 111);
frame.getContentPane().add(panel);
panel.setLayout(null);
***JLabel Jtable = new JLabel();
Jtable.setBounds(0, 25, 127, 58);
Jtable.setText("" + a);
panel.add(Jtable);***
System.out.println(a); //it is counting on console but in Jlabel variable is not.
}
#Override
public void actionPerformed(ActionEvent arg0) {
a++;
initialize();
}
}
i just wanna count in jlabel. i think, I have tried all the solutions posted in the site but i couldn't find out any solution. I am a beginner, have one month to learn Java. I am sorry if my question is too stupid.
You shouldn't be initializing a new frame and all the components on every action performed, you should only be updating the text of the label. You can do this via JLabel.setText https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setText(java.lang.String)
int a = 0; // variable
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asd window = new asd();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public asd() {
super();
Timer time=new Timer(1000, this);
time.start();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private JLabel label;
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(20, 11, 137, 111);
frame.getContentPane().add(panel);
panel.setLayout(null);
label = new JLabel();
label.setBounds(0, 25, 127, 58);
label.setText("" + a);
panel.add(label);
System.out.println(a); //it is counting on console but in Jlabel variable is not.
}
#Override
public void actionPerformed(ActionEvent arg0) {
a++;
label.setText("" + a);
}
Related
I have made a JFrame that shows a start button, and changes to stop when clicked. How to make it so that it changes its text to start when stop is clicked. Here is the source code:
public class FRMCountdown extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FRMCountdown frame = new FRMCountdown();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FRMCountdown() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton Start_Stop_btn = new JButton("Start");
Start_Stop_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Start_Stop_btn.setText("Stop");
}
});
Start_Stop_btn.setBounds(10, 188, 89, 23);
contentPane.add(Start_Stop_btn);
}
}
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
Swing was designed to be used with layout managers. I used a FlowLayout to place one JButton. Null layouts and absolute positioning lead to problems.
Java field names start with a lower case letter, Java method names start with a lower case letter. Java class names start with an upper case letter.
Here's the modified code.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ToggleJButton {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new ToggleJButton();
}
});
}
public ToggleJButton() {
JFrame frame = new JFrame("Toggle JButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 100, 5, 100));
JButton startStopButton = new JButton("Start");
startStopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
String text = button.getText();
if (text.contentEquals("Start")) {
text = "Stop";
} else {
text = "Start";
}
button.setText(text);
}
});
panel.add(startStopButton);
return panel;
}
}
I have a Java app and a JDateChooser. The problem comes when I start my app.
The JDateChooser remains invisible and appears only after I iconify my app.
What I should do?
dateChooser = new JDateChooser();
dateChooser.setDateFormatString("d/MM/yyyy");
dateChooser.setBounds(48, 68, 102, 20);
JTextFieldDateEditor editor = (JTextFieldDateEditor) dateChooser.getDateEditor();
editor.setEditable(false);
frmCodfiscextractor.getContentPane().add(dateChooser);
Also, I put this at the end of the class :
frmCodfiscextractor.repaint();
import java.awt.EventQueue;
import javax.swing.JFrame;
import com.alee.laf.WebLookAndFeel;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class demo {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WebLookAndFeel.install();
demo window = new demo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public demo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setDateFormatString("d/MM/yyyy");
dateChooser.setBounds(74, 193, 181, 30);
JTextFieldDateEditor editor=(JTextFieldDateEditor)dateChooser.getDateEditor();
editor.setEditable(false);
frame.getContentPane().add(dateChooser);
frame.repaint();
}
}
Use latest version of JCalender. It is correct working in my case.
I built a Java-Swing GUI using the WindowBuilder in Eclipse. But when I try to add new components using the .add() and .revalidate() nothing happens.
If someone could help to fix this issue I realy would apreciate it.
package Frame;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
public class TestFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame window = new TestFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnSampleButton = new JButton("Sample Button");
btnSampleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.add(new JButton("BTN"));
frame.revalidate();
frame.repaint();
}
});
frame.getContentPane().setLayout(null);
btnSampleButton.setBounds(110, 126, 185, 112);
frame.getContentPane().add(btnSampleButton);
}
}
Try following:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
TestFrame window = new TestFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnSampleButton = new JButton("Sample Button");
btnSampleButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JButton btn = new JButton("BTN");
btn.setSize(btn.getPreferredSize());
btn.setLocation(new Point(1, 1));
frame.add(btn);
frame.revalidate();
frame.repaint();
}
});
frame.getContentPane().setLayout(null);
btnSampleButton.setBounds(110, 126, 185, 112);
frame.getContentPane().add(btnSampleButton);
}
}
Try to learn Layout Managers. When you use an appropriate layout manager you shouldn't set the component's size/location.
I am new to Swing programming. And I m trying to develop a desktop application.
First all I need to create a login window, which should not be draggable and its position must be in center of the screen.
So by learning , I have created a window by the following code:
import com.sun.awt.AWTUtilities;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Newframe {
private JLabel label;
JFrame frame;
JButton btn;
Newframe(){
prepareGUI();
}
public static void main(String arg[]) {
Newframe n=new Newframe();
}
public void prepareGUI(){
frame=new JFrame();
frame.setUndecorated(true);
frame.setSize(300, 300);
frame.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
}
}
Now, I want to add components (e.g textfields, labels, buttons, etc...) to this created frame.
I m trying to add the components to the frame by initialize the components and add them to the frame ( by this frame.add(jbutton)) , but components are not going to add to the created frame...
Can any one help me for this?
frame.getContentPane().add(component)
Note this may vary depending on the layout you use.
Also, it'd be better to put the UI in the Event Dispatch Thread, with this:
public static void main(String arg[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Newframe n=new Newframe();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
You can use below code to add component to the frame and to center the frame you can use frame.setLocationRelativeTo(null);.
public class Newframe {
private JLabel label;
private JTextField txt;
JFrame frame;
JButton btn;
Newframe() {
prepareGUI();
}
public static void main(String arg[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Newframe n = new Newframe();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void prepareGUI() {
frame = new JFrame();
frame.setLayout(null);
frame.setSize(300, 300);
frame.setUndecorated(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
label = new JLabel("Name");
label.setBounds(10, 10, 100, 20);
frame.add(label);
txt = new JTextField();
txt.setBounds(50, 10, 100, 20);
frame.add(txt);
btn = new JButton("OK");
btn.setBounds(40, 40, 80, 20);
frame.add(btn);
}
}
I want to add a vertical scroll-bar on my JFrame with null layout.
Is it possible or not? please help!
Just set the JScrollPane as ContentPane for JFrame as it is described here:
public class TabbedPaneTest {
public static void main(String [] a) {
final JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.setContentPane(pane);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.setVisible(true);
}
});
}
}
In eclipse IDE you can use following code
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane jsp = new JScrollPane(container);
container.setPreferredSize(new Dimension(500, 250));
container.setLayout(null);
JLabel lblHelloWorld = new JLabel("Hello World");
lblHelloWorld.setBounds(10, 11, 101, 14);
container.add(lblHelloWorld);
frame.getContentPane().add(jsp);
}
}