FAQ Database Discussion Community


Keystroke/Hot Key for JButton in Java

java,swing,jbutton,hotkeys,jmenu
Initially I was using JMenu and establishing hot keys to work using accelerator. It was working perfectly. Now I want the same behavior in JButton but I am stuck. Here is the code that I wrote : Please share your ideas so I can go in the right path. import...

Focus on the button using tab key and press enter

java,swing,netbeans,jbutton,actionlistener
I have a simple jFrame with a submit button and a cancel button. Currently, everything works perfectly. User perform action on mouse click. Now I want to press 'tab' first to focus the button then it should fire whenever someone presses the enter key. It should work for both keys....

Java TimeUnit error

java,exception,time,jbutton
I wanted to make a Java program with Numberfields and a button. When you click the button it waits and the time that was put in the Numberfield.The Integers work fine, but the function does not work. Here is the code: public void jButton1_ActionPerformed(ActionEvent evt) { sekunden = Integer.parseInt(jNumberField3.getText()); minuten...

Create animation JButton Grid java

java,jbutton,grid-layout
How can I do a similar drop down animation on gridlayout? I'm trying to do the game Connect 4 with swing lib. Why this doesn't work? private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { JButton bottone = event.getSource(); for (int i = 0; i < 6; i++)...

JButton within JTable Icon/Animation Changing When Pressed

java,swing,jtable,jbutton,tablecellrenderer
I don't really know how to explain this in words. So I have a JTable with a JButton Column. I have set an icon for this button but when I press or hold the button, it changes into the image you see below. How can I disable this effect? I...

origin of an ActionEvent (keyboard vs mouse)

java,swing,jbutton,actionevent
I have a continous Action which I can call by pressing down a JButton b1. Currently I have added a MouseListener to b1 and I control the continous nature of this action by calling StartAction() in MousePressed() and StopAction() in MouseReleased(), if the source of the mouse event happens to...

how to make a JButton not clickable in java?

java,jbutton
I'm looking for a substitute of .setEnabled(false), so that the button is not clickable. I have read that I could remove the ActionListener from the button, but I just want the specific buttons to be not clickable, and do not know how to do it anyway. Or how can I...

Java Paint and JButton

java,jbutton,paint
I'm trying to have a JFrame with a button on it and when i click it the ball appear on the top left. Then, if i click it again the x,y position changes and it moves. The problem is i can't seem to get the ball to appear on the...

Java: Can't repaint ImageIcon

java,swing,jbutton,imageicon
I have a JFrame, and inside JFrame is a JPanel, and inside JPanel are 12 JButtons. I have set an ImageIcon for each button. Then, I need to give a JButton a new ImageIcon. I tried this: buttons[0].setIcon(new ImageIcon("path/to/new/icon")); But it didn't work. Also tried: buttons[0].revalidate(); my_jpanel.revalidate(); So what am...

How to use paint method in a JPanel

java,swing,jframe,jbutton,paint
I am working on a slot machine in Java and so far I created a button that will randomly generate two pictures. My code compiles but when I run it, all the stuff I put in the paint method does not show up. Is there anything I am missing? Thanks...

How to make a save button

java,swing,save,jbutton,joptionpane
I am trying to make a save button but I don't know how to do it. I will explain a little bit about my program. When someone start the program it shows a JOptionPane and the user need to write a nickname and then when he clicks on OK button...

Check if 3/9 texts of dynamically created JButtons matches? (for Tic Tac Toe game)

java,swing,for-loop,jbutton,dynamically-generated
JButton button[] = new JButton[9]; for(int i = 0;i < button.length; i++){ button[i] = new JButton(); button[i].setText(Integer.toString(i+1)); frame.add(button[i]); button[i].addActionListener(this); } What I have is 9 JButtons with name "button". To determine a win, how can I check if 3 button(either in rows, columns, or diagonal) matches. Thanks in advance....

Can't locate ImageIcon in JButton [duplicate]

java,icons,jbutton
This question already has an answer here: What is the correct path to display an ImageIcon png file for Windows 7? 4 answers I am trying to assign a png type image to a JButton which is already created. I've added a resource folder in the root folder of...

Issue with JFrame focus when disabling JButton

java,jframe,focus,jbutton
I'm having a really strange issue with my Java Application. I'm using Netbeans, and so I have a frameview with a main panel that has some buttons to choose a serial port and connect to it. It also has a menu bar to open up a bunch of different frames....

Why aren't the action listeners working

java,swing,jbutton,actionlistener
The ActionListeners are set up properly but for some reason are not working when I press one of the buttons. Any ideas? I have searched for ways to do it but nothing seems to work. Here is my code: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Master...

Cant figure out how to make my JButtons show up

java,swing,jbutton
I cannot for the life of me figure out how to place these buttons i have, the Next and Finish buttons that are supposed to be at the bottom of every page, if anyone can tell me how i can get these buttons working that would be awesome because this...

java swing - how to change window background

java,swing,jpanel,jbutton
I was trying to make a button that switches the window background from default to red. I haven't found any preset colors to match the default so i tried to get it from panel.getBackground when i created it. I have an error at line 11, i don't know how to...

“Un-rollover” a JButton when a JOptionPane is displayed

java,swing,jbutton,joptionpane
I have a situation where I need to display a JOptionPane after clicking on a JButton. The JButton has a default icon, and a rollover icon (which displays when, well, the mouse rolls-over the button). However, once the button is clicked and a JOptionPane appears, the rollover icon does not...

Why isn't my actionListener working?

java,swing,jbutton,actionlistener
I am trying to make a button show up if I press the hit button, if z==21, but the button I'm trying to make show up is not showing up. This is for a BlackJack game, but I pulled a little bit of code out of it, the part that...

How to start and stop a threads in netbeans from a jButton click

java,multithreading,jbutton
I write Java desktop app to fetch and post some data from my online rails backend app. The App have to call a get request every 5 second to update the relay state(example Arduino). here is my code: public class GUI extends javax.swing.JFrame { private Serial serial = null; private...

JPanel disappears when added to more than one other JPanel

java,user-interface,jpanel,jbutton,boxlayout
Hopefully you all can help me understand this mystery. I created a JPanel that contained a "Go Back" button and had a nice layout that I liked. I wanted to add this JPanel (I'll call it homeButtonPanel from here on out) to several other JPanels because I want them all...

Add JButton to JPanel from Different Class

java,swing,jpanel,jbutton
Hi is there a way to Add JButton to JPanel from Different Class. So basically the JPanel is in a Class A and JButton is in a Class B how can I put the button on the Panel which is in a different class. Hopefully this makes sense if you...

Formatting of components in Swing

java,swing,jbutton,jtextfield
I have just started programming in Swing. I am making a Fitness application using Swing. I have 2 questions: Whenever I click 'Calculate Scales' it should display the results only once, but it is displaying as many times as I click. How can I solve this? How can I set...

java swing action event after two jbuttons are clicked

java,swing,jbutton,mouselistener
I am trying to print "Hello" only when the jButton2 is clicked after the jButton1 and print "Bye" only when any other jButtons are clicked after jButton1. At the moment, the following code does not print anything. public void MouseClicked(java.awt.event.MouseEvent e){ if (e.getSource() == jButton1) { if (e.getSource() == jButton2)...

How would I access my toString() method in a ChangeListener in my Composite JButton class?

java,swing,constructor,jbutton
I have a JButton composition class and I have a toString I am sending to a JLabel in a different class. When I call the toString I'm getting something like: [email protected] public class EmptySpace{ private JButton button; protected int x; protected int y; protected String name; public EmptySpace(String text, int...

Action Listener doesn't change set variable to other value

java,user-interface,jbutton,distributed-computing
I am writing GUI for a chat, and I have problem I can't seem to find a solution. When button Send is clicked variable OKpressed should change to true and in function getUserInput it should recognize it changed but it doesn't.. It's acting like it still says false.. I tried...

New button creation inside JScrollPane

java,jdbc,jbutton,jscrollpane,windowbuilder
I've written a so called 'error less' code but I'm facing certain problems whilst using the application. This used to work perfectly before but as soon as I took the code over to the windowbuilder as a Jframe it started to not work. Here's my code: import java.sql.*; import java.util.ArrayList;...

How to access methods “across” a hierarchy of JButtons?

java,swing,jbutton
I have two types of buttons extending JButton and I need one button to access a get method from the other. but because because the isPressed() method is not a part of button I can not call it. Clarification: I have a type of button. When I press that button...

Keeping track of JButtons generated in a for loop

java,swing,for-loop,jbutton,tic-tac-toe
I am working on a Java tic-tac-toe game but I'm having trouble keeping track of my JButtons because I'm using a for loop to create 9 buttons: for(int i=0;i<button.length;i++){ button[i] = new JButton(); button[i].setText(Integer.toString(i+1)); frame.add(button[i]); button[i].addActionListener(this); } I want to check if my button text matches (in rows/columns/diagonal) and if...

JButton not shown on screen

java,swing,jframe,jbutton,jlabel
Im trying to make an application that will change the state of a traffic light in the click of a button. My code: Main import javax.swing.*; public class PP416 { public static void main(String[] args) { JFrame frame = new JFrame("Traffic light"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new TrafficPanel()); frame.pack(); frame.setVisible(true); } } JPanel...

Incrementing value of an integer in JTextArea

java,swing,jbutton,actionlistener,jtextarea
I have the following code in which I have created a JTextArea and Menubar in which I have added menuItem Inc. I have coded the value of "val" = 0 , so that when the code runs and frame appears, the first thing that is seen is zero. But i...

java: remove(component); does not remove button instantly

java,eclipse,jbutton
so I'm using this to remove the button from the JPanel. However, the button is not removed instantly, but disappears only after I minimize the window. Any suggestions? jBOption2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jPanel.remove(jButton2); ...

Detect clicks on button array with a single method

arrays,methods,jbutton,actionlistener
Im working in a school project, a minesweeper. Will be 20x20, so it has 400 buttons. Its there a way to add an actionEvent/actionPerformed and implement a generalized method for the whole array? Or there is an easier way?

Adding textfield and label on mouse click not working Java

java,swing,nullpointerexception,jbutton,actionlistener
I have this code: import java.awt.BorderLayout; 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.JPanel; import javax.swing.JTextField; public class Interface2 { private JLabel label; private JLabel label2; private JLabel label3; private JTextField textfield; private JTextField textfield2; private JTextField textfield3; private JButton button; private JButton button2; private JButton...

JButton ActionEvent ActionListener

java,swing,events,jbutton
How I can control the actionListener of two JButtons pressed one after another? I have got 2 JPanels. In each JPanel I've got a matrix of JButtons named tUsuari and tUsuariCPU void actionPerformed(ActionEvent){ /****// for (int i=0;i<tUsuari.length;i++){ for (int j=0;j<tUsuari.length;j++ ){ if (e.getSource()==tUsuari[i][j]){ System.out.println("hello"); for (int r=0;r<tUsuariCPU.length;r++){ for (int s=0;s<tUsuariCPU.length;s++...

Call a GUI from a JButton

java,user-interface,runtime,jbutton
I'd like to call a GUI from a swing component JButton. There will already be a GUI running at this time, but I'd like to execute another one through the use of a JButton. Any help would be greatly appreciated. P.S: I've tried the .show() method, and it takes over...

Checking Text Backspace Issue

java,swing,jbutton,jtextfield,documentfilter
I made it where a button is disabled and the only way to enable it is to type text into a field. Here is my code: import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter; class checkText extends DocumentFilter { public void replace(DocumentFilter.FilterBypass...

How can I update the text of a JButton when I click on it?

java,swing,refresh,jbutton,jlabel
I assume there is a better way than removing every element from the JPanel and remaking them, because that's the workaround I had but now obviously when the button is clicked it's told to remove itself so the action never completes. Here's my awful code for adding the button action...

How can I extend JComboBox to allow user to remove list items by clicking an x button, like an Internet browser tab?

java,swing,jbutton,jlabel,jcombobox
I am trying to create an object that resembles a JComboBox, but I want the user to be able to remove an item from the list. Imagine that each list element has to its right an x icon or button like what you find on a tab in your internet...

Add new label and textfield on button click Java

java,swing,jbutton,actionlistener,jlabel
I have this code with three buttons. I want to add a label and a textfield after a buttonclick. So for the 'button' I want to add the 'label' and 'textfield. For 'button2' I want to add 'label2' and 'textfield2'. And so on. I already have this code, but it...

JButton ActionListener not responding

java,swing,jbutton
I couldn't find the answer anywhere else online, so I came here. I apologize in advance if the mistake in my code is very obvious; I'm still quite new to java swing. Here's what's going on: I have created a JButton named toggleElevators, and I want it to change text...

How can I make this JButton work

java,swing,jbutton
I am working on a code that will generate a random number when you press a button and output that number. I have wrote this code and it compiles but when I press the button nothing works. Can someone please help. Here is some of my code. public class slotmachine...

How to place a JButton at a specific x - y point on a JPanel

java,swing,jbutton,layout-manager
GENERAL CODE: ISSUES BELOW WILL REFERENCE CERTAIN CLASSES public class Board extends JPanel { public Board() { BoardMethods a = new BoardMethods();//BoardMethods handles logic a.printBoard(getBoard());//irrelevant to question, helps determine winner JPanel panelLORDY = new JPanel(new FlowLayout()); //JButton alphaButton = new JButton("Hi"); //alphaButton.setBounds(213,131,100,100); //add(alphaButton); } public int[][] getBoard(){ return boardEvalMatrix; }...

Java Graphics - draw shape on Jbutton

java,swing,jbutton,paint
I am making a minimally small paint application for a class in Java. I need to make several buttons for the user to select different shapes, and on those buttons I am supposed to put an image of the shape they are using. For example, a button that lets a...

JButton toggle method not working as expected

java,swing,user-interface,jbutton
Here's what I'm trying to do: Create a JButton, and when clicked, its setText() will change to Happy, and then when clicked again, its setText() will change to Sad. Basically, I want it to toggle between Happy and Sad when clicked. (Sorry for the awful explanation) The issue is that...

Money accumulator buttons [closed]

java,loops,if-statement,jbutton,accumulator
I'm building a project for school and I want to make three money buttons (quarter, nickel and dime) that accumulates their value with every click of the button. When I'm done the text displayed will be grabbed and reParsed back to a double. I have that part done, I'm just...

ActionListener for one of the Jbuttons doesn't get called

java,swing,jbutton,actionlistener
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; @SuppressWarnings("serial") public class Gui extends JFrame { private ExpPanel...

how to get rid of visual effect when disabling buttons in java

java,user-interface,button,jbutton
When I'm setting the button to be disabled using this: jButton.setEnabled(false); then there is this visual effect visible on the second element -> How can I disable the button, but keep the the look of the first element?...

How can I stop jButton action?

java,if-statement,jbutton
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //codes for initializing & calculating value of a if(a==1){ //if this condition is true jButton action will Continue //jButton action code ......} else{ //if this condition is false jButton action will stop. if a value is not equal to 1 , I just want to...

JButton does not change a field value

java,swing,jframe,jbutton,actionlistener
I am facing a strange problem with a simple JFrame application which has only one button in order to change a field value. When I try to debug my application, it works fine, but when I try to run it, it doesn't work and gets stuck in the while loop....

How to add a jbutton to a jpanel

java,swing,jpanel,jbutton,jscrollpane
I've trying (unsuccessfully) to add loop generated JButtons to a JPanel. The thing is that the JPanel is over JScrollPane. Here is my code: String categoria = this.cmbCategoria.getSelectedItem().toString(); String[] partidos = myEstadio.buscarPartidos(categoria).split("/"); JButton b; for(String p : partidos){ b = new JButton(p); this.panelScroll.add(b, BorderLayout.CENTER); System.out.println(p); System.out.println(b.getLocationOnScreen()); As you can see,...

JButtons not showing up on JPanel on JFrame

java,swing,jframe,jpanel,jbutton
Im trying to display 9 3x3 grids in a 3x3 grid. None of the buttons are showing up though. I had to create all the individual buttons and i didnt know if they took up to much space of if there is an obvious error in my code. Thanks for...

JButton text won't update until other operations are done

java,swing,jbutton,actionlistener
I have a JButton which performs an action when clicked. In the actionPerformed I want to update the button text before calling the NotifyObserver, which contains a big number of calculations. The problem is that the buttontext won't update until all the operations called by the NotifyObserver are done. Here's...

Java GUI - JButton opens another frame from another class

java,swing,jframe,jbutton,actionlistener
i'm having trouble to get the 2nd frame to display the GUI Components. i've opened the frame using the JButton from the 1st frame. Here's the screen shot This is the first frame - ClientModule.java 2nd frame - ClientMenu.java Here's the codes i've tried ClientModule.java import java.net.*; import java.util.*; import...

How is action on JButton being invoked in this code?

java,swing,jbutton,actionlistener,windowlistener
I have problem understanding how is actionListener used in the following code and what is the addWindowListener method doing in the code below: kindly help me with it . public class SwingListenerDemo { private JFrame mainFrame; private JLabel statusLabel; public SwingListenerDemo(){ prepareGUI(); } public static void main(String[] args){ SwingListenerDemo swingListenerDemo...

Java JButton.setAction(…) null's button text

java,swing,jbutton,action
The following code renders a JButton without text: public abstract class Test { public static void main(String... args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); } SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame(); JPanel panel = new JPanel(); String text...

how to print 2D array in TextArea

java,jframe,jbutton,jtextarea
Hello guys I need some help I wanted to print 2D array into a JTextArea with the JButton. I have this method: public static void getMaze(){ for(int i = 0; i<rows; i++) for(int j = 0; j<columns; j++) array[i][j] = "[]"; array [i3][i4]="# "; array [i5][i6]="# "; array [i7][i8]="# ";...

JButtons aren't working?

java,swing,jbutton
For the game i'm making i made a button class to use for all the buttons in my menu. I finally managed to make my buttons appear on screen but now when i click them nothing happens. my button class: package menu; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon;...

Change variable value through event listener on JButton [duplicate]

java,variables,jbutton
This question already has an answer here: Why are only final variables accessible in anonymous class? 10 answers This is a pretty nooby question, but I can't work it out. I have a JButton and a variable. Simply put, the variable's value needs to change when the button is...

How to launch a second JFrame from clicking a JButton?

java,swing,jbutton,actionlistener
I'm making a To-Do List application and I have a PrimaryList frame and a SubList frame. When a user selects something from the PrimaryList (Grocery...or something like that) and then hits a forward arrow JButton, it is supposed to launch up the SubList frame. Now here is what I have...

Class can`t declare type

java,class,jbutton,subclass
I am going through thenewboston's tutorials and I have an unexpected error. I have tried to do everything that Eclipse suggest, but can't figure it out where the problem is. this is my Main Class import javax.swing.JFrame; class Main { public static void main(String args[]) { Gui go = new...

JButton not performing it's Required Function in java

java,jbutton,actionlistener
Here's My Code of Currency Converter Program . Earlier Null Pointer Exception Occurred , but it was Rectified . Now there are No Errors in the Program . Program Runs but Convert Button Does not Perform Any Operation . I believe Problem Lies in the ActionPerformed Function , But I'm...

How to put a JButton at a specific location?

java,swing,jframe,jpanel,jbutton
I know that there are already very similar questions to my question on stackoverflow, but neither one of them answers my question. I want to put my JButton on a specific location on my JFrame in JAVA. Here is my Code: public class DetectMotion extends JFrame { private JLabel label...

Adding unknown number of JButton to a JScrollPane

java,swing,jbutton,jscrollpane
I've look (almost) everywhere(here and here specifically) to find an answer on my problem. I'm trying to add an unknown number of JButton to a JScrollPane depending of the selection of a JComboBox. What I know is you have to add the element to a JPanel in order to get...

Calling a Java AbstractAction from a button/mouse release

java,swing,jbutton,actionlistener,abstract-action
In a simple calculator app, I use abstract actions to handle the buttons being clicked with the mouse and the respective number pad keys (with key bindings) being pressed. I wanted certain cosmetic changes to occur upon using the keyboard, such as changing the border of JButton number 1 when...

How to add a button in border? [closed]

java,swing,user-interface,jbutton
I'm writing a program and I want to add button and label on top right above JTabbedPane. I use MatteBorder and I want to add label (not title) and 1 button where the blue border is. thanks for your help. import java.awt.*; import static java.awt.Font.BOLD; import java.awt.event.*; import java.awt.event.*; import...

How to Position a JLabel in the specified Layout?

java,swing,jbutton,jlabel,layout-manager
I am trying to position my Labels and Button on screen. I found an answer here to set label's Position using: label3.setHorizontalAlignment(SwingConstants.CENTER); label3.setVerticalAlignment(SwingConstants.CENTER); but there has been no effect in my code. Also, on changing my layout from Flow Layout to Border Layout, only one Jbutton [CLEAR] appears on the...

how to identify a button in a group of loop generated buttons?

java,arrays,swing,netbeans,jbutton
I have a group of loop generated buttons made with this code this.panelCuerpo.setLayout(new GridLayout(4,5)); for(int i = 1; i<=20; i++){ final JToggleButton b = new JToggleButton(new ImageIcon("/images/available.png")); panelCuerpo.add(b); b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/available1.png"))); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt){ if(b.isSelected()){ b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/busy1.png")));...

Changing label text using ActionListener

java,jbutton,actionlistener,jlabel,settext
I'm trying to make a program that allows me to change the text of a label when clicking different buttons, however regardless of what button I click the text of the label changes to the last line of actionPerformed. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gui extends JFrame{...

Initializing a grid of JButton and adding them to a JFrame

arrays,user-interface,jframe,jbutton
I'm trying to write a program that plays Yahtzee in a JFrame. Our teacher has a way we're supposed to write it, and it involves adding ConfigurationButtons - a class that extends JButton - to the JFRame. Here is the code I have so far: builder= new PlayerPanel(); manager =...

Eclipse can`t find my photo for button

java,eclipse,button,jbutton
I`ve started learning JButton options and my eclipse can't find photos that I have saved. And yes, I've saved it to truly the right folder, format is also the right one (png), but I can't find it looking through eclipse and also when I'm running program, my stop faces errors...

Add a different ActionListener to anonymously referenced JButtons in a JToolBar

java,swing,awt,jbutton,jtoolbar
I'm trying to add four JButtons to a JToolBar, each with a different ActionListener. I'm wondering if there is a way to add an ActionListener to an anonymously referenced JButton, or if I have to specifically define each button and add each listener. Currently, this is what the code looks...

making Jbutton visible via another Jbutton

java,swing,jbutton
I have two buttons in my Jframe: button1 and button2. Both buttons have their respective codes to execute when they got clicked. However button2 should only be visible when a certain condition in button1 is satisfied. Need help. How should I do this?

How to set text on jButton

jbutton
So I am currently working on generating on small program, that takes for instance car parts, points to one of them and then gives you 3 options and you have to choose the correct one. I decided to customize the JButton's with options, because I didn't like the shapes and...

Enable JButton after CheckBox is checked

java,eclipse,jbutton,jcheckbox
I have a problem with a Java program,i have enabled a button after selecting JCheckbox,the result must be that the user must check the box and after the button is enabled. how can do it? Thanks....

How can I get a score to increase if the user clicked a Jbutton?

java,swing,jframe,jbutton
How can I get a score to increase if the user clicked a Jbutton (All of them ), and decreased if he clicked a random place in the frame ? here's the code package projet; 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.JPanel; public class Letstry...

Making a chess game in Java, I want to move the pieces

java,swing,jbutton,imageicon,chess
So I have my images stored as ImageIcon's on JButtons. I want the user to click on the JButton of the piece they want to use and then click on another JButton to move it there, how would I do this? I've tried using an actionListener to get the ImageIcon...

how to maintain transparency for JButtons (java)

java,swing,jbutton,transparency
i'm making a tank game. in my menu i want to use pictures as jbuttons, they are partly transparent and when they appear on screen the transparent parts become white. i tried using .setOpaque but this doesn't work. i can't think of any other method to get rid of the...

Use array elements from another method

java,arrays,jbutton
I have a questions about using array I have a method that creates JButton for a JPanel. I used JButton[] to store 7 buttons in it. The problem is those buttons must be setEnable(false) initially. they are only enabled when openButton is clicked. I would like to create another method...

Add dynamic buttons to jpanel in Java

java,swing,jpanel,jbutton
I'm trying to do a program in Java that will make a button appears in a JPanel where and when it's clicked. To do that, I have the method run, with the following code: public void run(){ panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e){ JButton button = new JButton(); button.setVisible(true);...

JButton and GUI freezes after click

java,swing,jpanel,jbutton
When Eclipse compiles this code everything works fine except the GUI freezes after the user clicks the "add" button. The answer is displayed and the work is shown. Can anyone shine some light on this problem and maybe give me some advice for the layout as well? import Aritmathic.MathEquation; public...

Why don't these images show up?

java,swing,applet,jbutton
I am working on a slot machine in Java and so far I created a button that will randomly generate two pictures. For some reason one of the pictures shows up while the other picture does not. I do not understand the issue as the code for both of these...

Same Jbutton selected state across multiple instances of same JPanel?

java,swing,jbutton
I have a toggle JButton that, when clicked, either installs or uninstalls some listeners elsewhere (but that's somewhat irrelevant I believe, as it could be just about any code that you don't want executed any number of times in a row, hence, the toggle, you click it once, it does...

How to change the value of an integer by pressing a JButton?

java,swing,jbutton
I'm making a program where everytime you press a JButton you decrease the value of an int. I want to make the i decrease by 20 each time the button is pressed. This is an example: static int i = 100; static final int f = 20; public static void...

BorderLayout Left Assignment

java,jbutton,border-layout
I have a JPanel with BorderLayout and some Buttons on SOUTH. When I add the Buttons they are in the middle. But I want them to be on the left. Is that possible?...

Issue with button icons, Java 8-Queens puzzle

java,swing,user-interface,icons,jbutton
The problem I am running into is this: I have a grid of buttons in a JPanel, these buttons are supposed to change to an image of a queen when I click them. The code looks like this: private Component createButtonBlack() { final JButton button = new BoardButton(); final ImageIcon...

Cardlayout With Jbutton Not Switching

java,jbutton,cardlayout
Hello I am fairly new to java programming and I am currently trying to work on a small game with a GUI, Right now I am stuck at an issue where I can only use a normal button with a card layout, unfortunately I want to use my own icon...

setLocation on a JButton doesn't seem to work as expected

java,swing,position,jbutton,layout-manager
Here is my shortened version of my code: JPanel card1 = new JPanel(); JLabel switch1 = new JLabel(new ImageIcon("switch1.jpg")); switch1.setLocation(TOPSWITCH, LEFTSWITCH1); JLabel switch2 = new JLabel(new ImageIcon("switch1.jpg")); switch2.setLocation(TOPSWITCH, LEFTSWITCH2); JLabel switch3 = new JLabel(new ImageIcon("switch1.jpg")); switch3.setLocation(TOPSWITCH, LEFTSWITCH3); card1.add(switch1); card1.add(switch2); card1.add(switch3); card1.setBackground(Color.BLACK); /* JButton goToRoom = new JButton("TEST"); goToRoom.setLocation(180,...

Buttons and paint method

java,swing,graphics,jbutton,actionlistener
I'm having trouble with my buttons. I know they're working because I've tested them out by exiting the problem through System.exit. Here is what my output looks like: http://imgur.com/Ks7mIFa When I click the close button, the handle on the switch should redraw to the other side and the close button...

JButton does not appear on the GUI

java,swing,layout,jbutton,layout-manager
when I compile and run my code everything seems to work fine except the JButton does not appear. I'm adding it to a JPanel that is on the frame. I'm new so I might not know what to lookout for here. Thanks! import java.awt.*; import java.awt.event.*; import java.text.*; import javax.swing.*;...

Setting an ActionListener for a Jbutton “class”

java,swing,jbutton
I am remaking a menu for one of my games where to make buttons I use a method called addButton**(text, Image, pressedImage, container). My problem is that I want to use the addActionListener method and allow each button to have their own action but I'm unable to do so. Any...

how to use a class in multiple panels

java,jbutton,redundancy
i'm making a tank game. to avoid redundancy to make my buttons in the menupanel i wrote a class button: package menu; import java.awt.Image; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; public class Button extends JButton{ public JButton button; public ImageIcon buttonImage; public int x, width, height; public String backgroundPath; public...

JButton with padding between its border and the button itself

java,swing,border,jbutton,padding
I've a JButton that has its background green-colored and its border as a LineBorder. I would like to insert a space between the button and the border, a kind of padding. I've tried with setMargin(new Insets(x,y,t,z)) but it seems not working. This is my piece of code. JButton JBtn=new JButton("sdfd");...

how to add action to new jbutton inside another button for playing jfugue pattern

java,jbutton,jfugue
I want to ask how to add action jbutton in java, im newbie on java. im making a program to optimize chord with Harmony Search algorithm using netbeans. jButton3 has a function to process the algorithms, but i need to make 1 button more named jButton1 only for playing the...

How to add Jbutton from another class to main class

java,jbutton
So am I trying to add JButton from another class into my main class, but I have no idea how to. Do I have to use a certain command or import a package to add the button? //my first class with JButton public class ScreenInitial { public ScreenInitial() { JPanel...

JLabel is disappearing

java,swing,jbutton,jlabel
I have been trying to look for the answer everywhere and I couldn't find a right solution so I am hoping that you guys will be able to help me. I have created a custom JButton just by overriding paintComponent(Graphics g) using drawImage. And then I created JLabel,which I wanted...

JOptionPane is acting weird [closed]

java,jbutton,joptionpane
I am trying to make a Jbutton work properly. My program requires the user to click on a "hint" button. When the user clicks, a Joption pane will pop up with one hint. Next time he hits, next hint appears and last hint is removed from stack, and so on....

JFrame isn't loading properly

java,swing,jframe,jbutton
I've made a Java Project with multiple classes, one's the Shell.java. That launches a GUI (that uses swing) with Buttons and Images that launch other classes with className.main(args); This works fine except with one button, but I've coded the part like all others... (code below): File morseFile = new File("c:\\temp\\morse.png");...

Java - How do you make a jButton# index take the value of an int variable?

java,loops,button,parameters,jbutton
I need to perform actions on hundreds of buttons. I'm looking for a way to use a loop for the jButton index, instead of writing hundreds of lines of code just to change the color of multiple buttons. I want something like this: for(int i = 1; i < 100;...

JButtons don't load on panel until hovered over?

java,swing,jbutton,layout-manager,null-layout-manager
When this is put on a frame, the buttons don't load until they are hovered over with the mouse, and then they stay up like they should. Here's the code: I've calling things like repaint() and revalidate() but none of them seem to have fixed the problem. The main and...