I want to add a timer button to my main frame but it is in another class and I don't know how to use it in my main class.
I need a timer button in my frame but I can't make it without another class.
In that class I can't call my main frame.
This is my code:
class ButtonTimer extends Thread{
private JButton button = new JButton(" ");
private int count = 1;
public ButtonTimer() {
Timer time = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
button.setText(String.valueOf(count));
count++;
}
});
time.start();
JFrame frame1 = new JFrame();
frame1.add(button);
frame1.setBounds(0, 20, 100, 50);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
}
}
public class game {
public static void main(String[] args) {
JFrame frame2 = new JFrame();
frame2.setBounds(0, 0, 1000, 5000);
frame2.setVisible(true);
JLayeredPane jlp = new JLayeredPane();
jlp.setBounds(0, 0, 1000, 500);
frame2.add(jlp);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonTimer();
}
});
}
}
How can I do this?
You Could...
Create a custom JButton that wraps a Timer internally to it.
This allows you to self contain the button and timer into a single unit and reuse it where ever you want...
You Could...
Create a custom Timer which takes a reference to a JButton and updates the text automatically on each trigger...
You Could...
Create a custom ActionListener or even Action that takes a reference to a JButton and updates the text and then pass this into a Timer instance of your choice...
Have a try. Here we create a JButton in your main frame and then we set the text on actionPerformed of another class.
public class game1 {
private static JFrame frame2;
private static JButton button1=new JButton(" ");
public static void main(String[] args) {
frame2 = new JFrame();
frame2.setBounds(0, 0, 1000, 5000);
JLayeredPane jlp = new JLayeredPane();
jlp.setBounds(0, 0, 1000, 500);
jlp.add(button1);
frame2.add(jlp);
frame2.add(button1);
frame2.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonTimer();
}
});
}
private static class ButtonTimer {
private JButton button = new JButton(" ");
private int count = 1;
public ButtonTimer() {
javax.swing.Timer timer = new javax.swing.Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
button.setText(String.valueOf(count));
button1.setText(String.valueOf(count));
count++;
}
});
timer.start();
JFrame frame1 = new JFrame();
frame1.add(button);
frame1.setBounds(0, 20, 100, 50);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
}
}
}
Related
I wrote a program to randomly select an image and display it in the window every time a button is pressed. Mow I am trying to figure out how to make the "Roll Dice" button the default allowing enter press.
Driver:
public class MCobbM10A1 {
static int SCREEN_WIDTH = 500;
static int SCREEN_HEIGHT = 600;
static WidgetPanel widget;
static GraphicPanel myPanel;
public static void main(String[] args) {
JFrame myFrame = new JFrame("Dice Roller");
JFrame.setDefaultLookAndFeelDecorated(true);
myPanel = new GraphicPanel();
myPanel.setBounds(0, 0, 500, 200);
myFrame.add(myPanel);
JPanel lastPanel = new JPanel();
myFrame.add(lastPanel);
widget = new WidgetPanel();
widget.setBounds(0, 250, 500, 300);
myFrame.add(widget);
widget.rollButton.addActionListener((ActionEvent newRoll) -> {
new MCobbM10A1().myNewRollButtonPressed();
});
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 500, 300);
}
private void myNewRollButtonPressed() {
myPanel.newRoll();
}
}
And here is the WidgetPanel:
public class WidgetPanel extends JPanel {
JButton rollButton;
WidgetPanel() {
this.setBackground(Color.WHITE);
this.setLayout(new BorderLayout());
rollButton = new JButton();
rollButton.setBounds(350, 200, 100, 25);
rollButton.setText("Roll Dice");
this.add(rollButton);
JLabel fixBug = new JLabel();
this.add(fixBug);
}
}
how to make the "Roll Dice" button the default allowing enter press.
You assign the default button to the JRootPane of your frame.
frame.getRootPane().setDefaultButton(…);
One way to do this is:
#Override
public void addNotify()
{
super.addNotify();
JFrame frame = (JFrame)SwingUtilities.windowForComponent( rollButton );
frame.getRootPane().setDefaultButton(rollButton);
}
The addNotify() method is invoked when a panel is added to the frame.
Unrelated to your question but why are you creating a new object for each roll?
widget.rollButton.addActionListener((ActionEvent newRoll) -> {
new MCobbM10A1().myNewRollButtonPressed(); // <--- new MCobbM10A1 object
});
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 500, 300);
}
private void myNewRollButtonPressed() {
myPanel.newRoll();
}
Why not just do this?
widget.rollButton.addActionListener((ae) ->
myPanel.newRoll());
I have the main class that instantiates a GridBagLayout with a JLabel visbility set to false.
I would like to set the label visible when the program is running, I have tried this but it won't work. It will just display the default layout.
Main class:
gui = new gui();
gui.display();
gui.label.setVisible(true);
Gridbag layout class:
public JFrame frame;
public JLabel label1;
/**
* Launch the application.
*/
public static void display(){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gridLayout window = new gridLayout();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
* Create the application.
*/
public gridLayout() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
#SuppressWarnings("static-access")
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 600, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
frame.getContentPane().setLayout(gridBagLayout);
}
label1 = new JLabel(new ImageIcon("hi"));
GridBagConstraints gbc_label1 = new GridBagConstraints();
gbc_label1.insets = new Insets(0, 0, 5, 5);
gbc_label1.gridx = 1;
gbc_label1.gridy = 1;
label1.setVisible(false);
frame.getContentPane().add(label1, gbc_label1);
You want to display a label while a programme is running, right? This has nothing to do with the layout manager.
I give you an example where the label is visible as long as a dialog (representing your task/programme) is displayed; and I hope you can adopt it to your needs. Possibly you have to put the programme/task in an own thread.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Y extends JFrame {
public static final long serialVersionUID = 100L;
public Y() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 240);
JLabel lb= new JLabel("Programme is running ...");
lb.setVisible(false);
add(lb, BorderLayout.CENTER);
JButton b= new JButton("Launch programme (dialog)");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lb.setVisible(true);
JDialog dlg= new JDialog(Y.this, "The dialog", true);
dlg.setSize(100, 100);
dlg.setVisible(true);
lb.setVisible(false);
}
});
add(b, BorderLayout.SOUTH);
setVisible(true);
}
static public void main(String args[]) {
EventQueue.invokeLater(Y::new);
}
}
I need to make a GUI that asks the users for details and then save them in a linked list. I wanted to use the CardLayout to switch from one frame to another, which is something I'm doing for the first time. I have done probably less half of what I need to do and here I am quite lost in this part. The code below compiles and executes but when I click the buttons, the desired change does not happen . What could be wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDatabaseWindow extends JPanel{
public static final String FRONT_PAGE = "Front Page";
public static final String BROWSE_MEMORIES = "Browse Memories";
public static final String ADD_EDIT = "Add Edit";
public static final String VIEW_MEMORY = "View Memory";
public static void createAndShowGUI() {
final MyDatabaseWindow mdbw = new MyDatabaseWindow();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton addButton = new JButton("Add");
final JButton editButton = new JButton("Edit");
final JButton deleteButton = new JButton("Delete");
final JButton browseButton = new JButton("Browse");
final JButton searchButton = new JButton("Search");
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(browseButton);
buttonPanel.add(searchButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToAddPage();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToSearch();
}
});
JFrame frame = new JFrame("Memory Files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 540);
frame.setLocation(250, 100);
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private CardLayout cardLayout = new CardLayout();
private JPanel cardShowingPanel = new JPanel(cardLayout);
public MyDatabaseWindow() {
Window1 win1 = new Window1();
cardShowingPanel.add(win1, FRONT_PAGE);
Window2 win2 = new Window2();
cardShowingPanel.add(win2, BROWSE_MEMORIES);
Window3 win3 = new Window3();
cardShowingPanel.add(win3, ADD_EDIT);
Window4 win4 = new Window4();
cardShowingPanel.add(win4, VIEW_MEMORY);
setLayout(new BorderLayout());
add(cardShowingPanel, BorderLayout.NORTH);
}
public void goToAddPage() {
cardLayout.first(cardShowingPanel);
}
public void goToBrowse() {
cardLayout.first(cardShowingPanel);
cardLayout.next(cardShowingPanel);
}
public void goToSearch() {
cardLayout.last(cardShowingPanel);
}
public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}
}
class Window1 extends JPanel {
public Window1() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window2 extends JPanel {
public Window2() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window3 extends JPanel {
public Window3() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window4 extends JPanel {
public Window4() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
The main problem is the use of null-layouts and these lines of code:
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
First you add the panel using the CardLayout to BorderLayout.CENTER, then you "overlay" it with your buttonPanel, which is using null-layout.
I would go with a simple FlowLayout (the default layout-manager for a JPanel) for the buttonPanel and add it to the BorderLayout.SOUTH of the contentPane. I would also strongly recommend reading this tutorial.
So remove the following lines of code:
buttonPanel.setLayout(null);
...
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
and change frame.getContentPane().add(buttonPanel); to frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);.
Also forget about the null-layout / setBounds() in your Window-classes.
(Note that you still won't see the text change if you press a button because you always add a JLabel with the same text ("Memory Files") to your Windows.)
As you read this code, you will realize I got one action event to work, it opens up a new JPanel that displays the button that will run the ballBounce, but for now im stuck trying to get a working button within that frame because that frame is already within a actionEvent, any help?
public class MainJPanelOperation
{
public static void main(String[] a)
{
JPanel panel1 = new JPanel(new GridLayout(5, 10, 1, 1));
JButton t1 = new JButton();
JButton t2 = new JButton();
JButton letsStart = new JButton("Start The Program!");
JButton t3 = new JButton();
JButton t4 = new JButton();
//letsStart.setBounds(200,250,12,12);
panel1.add(t1);
panel1.add(t2);
panel1.add(letsStart);
panel1.add(t3);
panel1.add(t4);
final JFrame frame1 = new JFrame("Game");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(panel1);
frame1.setSize(1000,1000);
frame1.setVisible(true);
t1.setVisible(false);
t2.setVisible(false);
t3.setVisible(false);
t4.setVisible(false);
letsStart.setBackground(Color.yellow);
panel1.setBackground(Color.black);
letsStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("panel 2 main menu intro online");
JPanel panelMM = new JPanel(new GridLayout(5, 10, 1, 0));
JButton MM1 = new JButton("BallBounce");
panelMM.add(MM1);
JFrame frameMM = new JFrame("Game/Main Menu");
frameMM.add(panelMM);
frameMM.setSize(1000,1000);
frameMM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameMM.setVisible(true);
frame1.setVisible(false);
}
});//end of start sequence
}
}
JPanel panelMM = new JPanel(new GridLayout(5, 10, 1, 0));
JButton MM1 = new JButton("BallBounce");
panelMM.add(MM1);
final JFrame frameMM = new JFrame("Game/Main Menu");
frameMM.add(panelMM);
frameMM.setSize(1000,1000);
frameMM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
letsStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("panel 2 main menu intro
frameMM.setVisible(true);
frame1.setVisible(false);
}
});
You can make frameMM final and there is no need to have all of your code inside the ActionListener.
Try This :it is working inside a Action Listener.
JButton MM1 = new JButton("BallBoe");
MM1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("panel 2");
}
});
or class MainJPanelOperation implements ActionListener
You can use class MainJPanelOperation
{
static JButton MM1;
//your code
}
MM1=new JButton("Button");
MM1.addActionListener(this);
write a Method outside Main()
public void ActionPerformed(ActionEvent e)
{
if(e.getSource()==MM1)
{
System.out.print("");
}
if(e.getSource()==Buttonobject)
{
//your code for button Pressing Event
}
}
Is there any good way to change a JFrame opacity real time. right now i need to restart the window to get the opacity
if (Variables.LoggerOpacity){
if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) {
AWTUtilities.setWindowOpaque(Frame, true);
AWTUtilities.setWindowOpacity(Frame, 0.60f);
}
}
When i use
AWTUtilities.setWindowOpacity(Frame, 0.60f);
On a button JCheckBox i won't change the opacity.
Q: How can i change the opacity realtime?
Even if you have set the JFrame to static, you should be able to reference it if your opacity method is within the same class, if it isn't - create a getter method to reference your JFrame and pass that to your function. Here's an example program that executes and the opacity works fine:
public class JFrameOpacityExample extends JFrame {
private static JFrame myFrame;
private static boolean loggerOpacity;
private static JButton button;
public static void main(String[] args) {
myFrame = new JFrame("Test Frame");
myFrame.setSize(400, 400);
myFrame.setVisible(true);
JPanel panel = new JPanel();
button = new JButton("Press me");
button.setBounds(100, 100, 50, 50);
button.setVisible(true);
panel.add(button);
myFrame.add(panel);
loggerOpacity = true;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Object src = evt.getSource();
if (src == button && loggerOpacity) {
AWTUtilities.setWindowOpacity(myFrame, 0.40f);
}
}
});
}
}
Add the following command to a frame's constructor. The name of the frame in this example is MyFrame.
jCheckBox1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
AWTUtilities.setWindowOpacity(MyFrame.this, 0.2f);
}
});