I am trying to create a seven segment display that automatically moves using the timer with the use of the ActionListener and actionPerformed.
I thought that if I use the for, if statement it would automatically loop from 0 to 2 and set the Background color for each segment of the numbers. However, when I display it, it is stuck on just displaying zero and will not count up from there.
Can anyone help me on what I am doing wrong, that makes this stuck on zero?
Here is the programming I have now using JFrame.
import javax.swing.Timer;
public class SevenSegment extends JFrame {
Timer timer = new Timer(100, null);
public SevenSegment() {
timer.start();
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 3; i++) {
if(i == 0) {
lblNumberOne.setBackground(Color.red);
lblNumberTwo.setBackground(Color.red);
lblNumberThree.setBackground(Color.red);
lblNumberFour.setBackground(Color.red);
lblNumberFive.setBackground(Color.red);
lblNumberSix.setBackground(Color.red);
}
else if(i == 1) {
lblNumberTwo.setBackground(Color.red);
lblNumberThree.setBackground(Color.red);
}
else if(i == 2) {
lblNumberOne.setBackground(Color.red);
lblNumberTwo.setBackground(Color.red);
}
}
}
});
}
}
Here's about as simple a Swing GUI clock as you can make.
I did this so I could show you a general way to start all of your Swing GUIs. Oracle has a helpful tutorial, Creating a GUI With JFC/Swing, that will show you how to create all kinds of Swing GUIs.
We start all Swing GUIs with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
We always create a JPanel to put our Swing components on. The only Swing component that we add to a JFrame is a JPanel or a JScrollPane. This allows us to separate the creation of a JFrame from the rest of the view. The JFrame code is nearly identical for all Swing applications. The only difference is the JPanels you add to the JFrame.
The JFrame code must be called in a specific order. This is the order I use for my Swing applications.
The JPanel code is in a separate paragraph for general tidiness. I like to keep things separate so I can focus on one small part of the GUI at a time.
The updateClockLabel method exists because I need to execute the code one time when I'm creating the JPanel, and five times a second thereafter, to actually update the JLabel.
By the way, I didn't write all this code (all 64 lines) in one shot. I wrote a little, tested a lot. My code was not correct the first time I wrote it, and I don't just mean making typos.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SimpleClock implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleClock());
}
private JLabel clockLabel;
#Override
public void run() {
JFrame frame = new JFrame("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
Timer timer = new Timer(200, new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
updateClockLabel();
}
});
timer.start();
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 45, 5, 45));
clockLabel = new JLabel(" ");
clockLabel.setFont(panel.getFont().deriveFont(Font.BOLD, 72f));
updateClockLabel();
panel.add(clockLabel);
return panel;
}
public void updateClockLabel() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm:ss a");
String timeDisplay = LocalTime.now().format(formatter);
clockLabel.setText(timeDisplay);
}
}
Related
As for testing-reasons I tried to open a JDialog window with the panel and its contents of the main application frame. As I already had anything in there I wanted to see if I could simply set the JDialogs contentPane to the one I passed over. So in simplyfied form this came together:
testsforSO.java :
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class testsforSO extends JFrame {
private static final long serialVersionUID = -3890178393751567629L;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testsforSO frame = new testsforSO();
frame.setSize(300, 300);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public testsforSO() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("I am the Frame");
getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
panel.setLayout(null);
JButton btnRestart = new JButton("Restart");
btnRestart.setBounds(10, 10, 50, 50);
btnRestart.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
testsforSO.main(null);
dispose();
}
});
panel.add(btnRestart);
getContentPane().add(panel);
repaint();
// -----------DELETE These comments-------------
// JDialog myDialg = new JDialog(this);
// myDialg.setContentPane(panel);
// myDialg.setVisible(true);
// myDialg.setSize(300,300);
// myDialg.setLocation(new Point(250, 250));
// myDialg.setTitle("I am Dialog from within the script");
myDialog.main(panel);
}
}
and myDialog.java :
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JDialog;
public class myDialog extends JDialog {
private static final long serialVersionUID = 7079322237622743228L;
public static void main(Container myContainer) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
myDialog frame = new myDialog(myContainer);
frame.setVisible(true);
frame.setContentPane(myContainer);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public myDialog(Container myContainer) {
setContentPane(myContainer);
getContentPane().setLayout(null);
setBounds(200,200,200,200);
}
}
When starting the main frame I assumed it would contain the restarting button as well as the dialog does. But interestingly it was only the dialog with the button.
However when hitting it, the main frame properly restarted, a second dialog was set up and it contained the button again. This time however the main frame had the button as well, just without any function. Clicking on it does absolutely nothing.
Then I tried further and checked if that behaviour would change if I added the dialog directly into the main applications code (see the commented lines) and, starting the application once again only the dialog in its own class showed the button. Even hitting this one now restarted properly but the button won't show up on any other window except the lonely declared dialog.
What am I missing here and how could I refactor my code to work properly if even at all in this manner?
Understand that Swing components can only be present in one container, and while you may see the visual residue of a component in a container, the actual component is only present in the last container added to.
Myself, if I wanted dialog and jframe to have the same content pane components, I'd create a factory method to create the contentPane, and then use it to create two unique but identical contentPanes.
Also, I'd be remiss if I didn't mention something about your use of null layouts. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
For instance, when I ran your code, this is the dialog that I saw:
You hard coded the button's size to be too small to show its text on my platform. If you had used layout managers, and called pack() on the top-level window, the button would show appropriately.
First of all, this is a more specific question than it seems to be. To start off: I am currently doing a small application with a rather small GUI, so I decided to make a GUI class, and initialize my whole GUI in this constructor.
This would look like this:
public class GUI extends JFrame{
public GUI{
//Initialize GUI here, including its Frames, Panels, Buttons etc.
}
}
How can I now access the GUIs frame etc. from an external class? If I would create an object of the GUI class, I would simply duplicate my GUI window. I did not come across any other ideas than making the frame, panel and so on static.
I'm somewhat lost right now. Also I'm pretty sure that I am not thinking the right way into this case, but I need someone to point me to the right direction. If someone could help me out, I would be very thankful.
First of all, using static is the worst solution possible, even if your GUI class is a singleton (buf if it is, at least it will work fine).
Why don't you simply create getters and/or setters ? And finally, it is usually not normal that external classes need to access the components of another graphic class. You should wonder if your design is the most fitted for your needs.
Here's a simple GUI to change the background color of a JPanel with a JButton. Generally, this is how you construct a Swing GUI.
package com.ggl.testing;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ChangeDemo implements Runnable {
private boolean isYellow;
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ChangeDemo());
}
#Override
public void run() {
frame = new JFrame("Change Background Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel(
"Click the button to change the background color");
nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
namePanel.add(nameLabel);
mainPanel.add(namePanel);
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.YELLOW);
isYellow = true;
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
isYellow = !isYellow;
if (isYellow) buttonPanel.setBackground(Color.YELLOW);
else buttonPanel.setBackground(Color.RED);
}
});
buttonPanel.add(changeButton);
mainPanel.add(buttonPanel);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
You don't access the Swing components of the GUI from other classes. You create other classes to hold the values of the GUI.
Generally, you use the model / view / controller pattern to construct a Swing GUI. That way, you can focus on one part of the GUI at a time.
Take a look at my article, Java Swing File Browser, to see how the MVC pattern works with a typical Swing GUI.
You don't need to make it static or to create a new JFrame object every time.
Have a look at this simple code :
class UseJFrame {
public static void main(String...args) {
Scanner sc = new Scanner(System.in);
JFrame frame = new GUI();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
System.out.println("Press E to exit");
String ip;
while(true) {
System.out.println("Show GUI (Y/N/E)? : ");
ip = sc.nextLine();
if(ip.equalsIgnoreCase("y") {
frame.setVisible(true);
} else if(ip.equalsIgnoreCase("n") {
frame.setVisible(false);
} else { // E or any other input
frame.dispose();
}
}
}
}
Note : Don't make GUI visible through constructor or it will show window at the very starting of creation of JFrame object.
If you want to use the same JFrame object at other places too then pool architecture would be better approach.
I am trying to make a basic program where whenever you press a button, a JCheckbox is generated and added below the other JCheckbox on a panel. I figured out how to generate the JCheckbox with a ActionListener but I can't figure out how to get each new check box to appear below the previous one. Everything else seems to be working but I can't get this location thing to work.
box.setVisible(true);
_p.add(box);
int i = 0;
int u = i++;
box.setAlignmentX(0);
box.setAlignmentY(u);
Here is a sample of my code. I've been stuck on this problem for a very long time and would greatly appreciate any and all help.
Check out the Swing tutorial on Using Layout Managers. You could use a vertical BoxLayout or a GridBagLayout or maybe a GridLayout.
Whatever layout you choose to use the basic code for adding components to a visible GUI is:
panel.add(...);
panel.revalidate();
panel.repaint();
The other statements in your code are not necessary:
//box.setVisible(true); // components are visible by default
The following methods do not set a grid position.
//box.setAlignmentX(0);
//box.setAlignmentY(u);
JCheckbox lives in a container like a JPanel (that means that you add checkbox to a panel) . A JPanel have a layoutManager. Take a look about Using Layout Managers
You could use BoxLayout with Y_AXIS orientation or a GridLayout with 1 column and n rows.
Example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CheckBoxTest {
private JPanel panel;
private int counter=0;
public CheckBoxTest(){
panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
JButton button = new JButton(" Add checkbox ");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
panel.add(new JCheckBox("CheckBox"+Integer.toString(counter++)));
//now tell the view to show the new components added
panel.revalidate();
panel.repaint();
//optional sizes the window again to show all the checkbox
SwingUtilities.windowForComponent(panel).pack();
}
});
panel.add(button);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Checkbox example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(Boolean.TRUE);
CheckBoxTest test = new CheckBoxTest();
frame.add(test.panel);
//sizes components
frame.pack();
frame.setVisible(Boolean.TRUE);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I thought that CardLayout uses a HashMap to store its pairs (panels and associated string identifiers) but looking through the CardLayout class I noticed that it actually uses a Vector. This is how I understand it: when the show method is called it loops through the contents of the vector checking with equals to find out if this is the name of the appropriate card, and if it is, it then loops through all the panels of the container to find out which one is currently visible, it hides it, and then it displays the appropriate card.
If I was making a gui app that has a lot of different panels wouldn't that be kind of slow technique to flip to the desired panel? Should I better use my own way of showing my panels like storing them to an array and manually using add/remove or setVisible instead of using CardLayout? This is actually the way I was using at the beginning before I ended up to CardLayout.
This will never be an issue: you don't normally flip between components very frequently, and when you do then scanning a list with a smallish number of compoenents (usually 3-100?) is going to take a negligible amount of time compared with other operations that will have to happen (e.g. drawing the new component). Choice of data structure is basically irrelevant from a performance perspective - you could use a linked list and nobody would notice.
Also note that a HashMap wouldn't be appropriate for a CardLayout as it needs to preserve the order of the cards so that you can use first/next/previous etc.
So basically, don't worry and don't waste your time rolling your own CardLayout clone - CardLayout works just fine.
I don't find any performance issue in CardLayout. Even if you have a 1000 child components, it still feels very fast. Either by using previous/next or using show, it goes really fast.
Try to post an SSCCE that reproduces your problem and then we may help you. Here is something to start from:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestCardLayout {
protected void initUI() {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final CardLayout layout = new CardLayout();
final JPanel panel = new JPanel(layout);
for (int i = 0; i < 1000; i++) {
panel.add(new JLabel("Label " + i), getLabelConstraint(i));
}
JButton next = new JButton("Next");
next.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
layout.next(panel);
}
});
JButton previous = new JButton("Previous");
previous.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
layout.previous(panel);
}
});
final JButton choose = new JButton("Choose");
choose.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String value = JOptionPane.showInputDialog(choose, "Enter a number between 0 and 999");
try {
int i = Integer.valueOf(value);
if (i > -1 && i < 1000) {
layout.show(panel, getLabelConstraint(i));
}
} catch (NumberFormatException e1) {
e1.printStackTrace();
}
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(previous);
buttonPanel.add(next);
buttonPanel.add(choose);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private String getLabelConstraint(int i) {
return "ComponentConstraint" + i;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestCardLayout().initUI();
}
});
}
}
I'm trying to make to make some text appear before my applet loads so I've made a simple SSCCE(.org):
import java.awt.*;
import javax.swing.*;
public class test extends JApplet {
public void init() {
this.add(new JLabel("Button 1"));
System.out.println("Hello world...");
try {
Thread.sleep(3000);
}catch(Exception hapa) { hapa.printStackTrace(); }
}
}
If you run it, the Button 1 will appear AFTER the 3 seconds when it's suppose to appear BEFORE that... what am I doing wrong?
I think the init() method has to return before the items are rendered.
JustinKSU covered the technical part of the question.
A better strategy would be to use the image param to show a 'splash' before the applet appears. See Special Attributes of Applets for further details.
I want one for a fixed amount of time... not just the loading.
In that case, put a CardLayout in the applet. Add the 'splash' to the first card, the rest of the GUI to another. At the end of the init() create a non-repeating Swing Timer that will flip to the card with the main GUI.
E.G.
// <applet code='SplashApplet' width='400' height='400'></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SplashApplet extends JApplet {
public void init() {
final CardLayout cardLayout = new CardLayout();
final JPanel gui = new JPanel(cardLayout);
JPanel splash = new JPanel();
splash.setBackground(Color.RED);
gui.add(splash, "splash");
JPanel mainGui = new JPanel();
mainGui.setBackground(Color.GREEN);
gui.add(mainGui, "main");
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cardLayout.show(gui, "main");
}
};
Timer timer = new Timer(3000, listener);
// only needs to be done once
timer.setRepeats(false);
setContentPane(gui);
validate();
timer.start();
}
}