jbutton

adding action listeners to a new jbutton created in a loop in java and called from another class

你说的曾经没有我的故事 提交于 2019-12-24 22:20:11
问题 I am trying to add an ActionListener to a JButton created in a loop, then call the ActionListener from another class (the controller class), but its not working. I don't know why. Here is the first class public class Browse extends JPanel { private JButton play_lists_btn; public Browse() { int increment = 0; while (increment < 5) { add(createButton(increment)); increment++; } } private JButton createButton(final int i) { play_lists_btn = new JButton(); play_lists_btn.setText(" This is " + i);

Button on FrameOne shall close FrameTwo. ActionListener referes object of another class in JAVA

坚强是说给别人听的谎言 提交于 2019-12-24 21:55:51
问题 I have two frames in separate classes (FrameOne and FrameTwo) FrameOne has two buttons. One to open FrameTwo and One shall close FrameTwo. How to open FrameTwo I know. But not how to close from Frame One. How do I code it to make it working? Thanks. (I know that there are similar questions. I red them all. But it didn't gave me the answer. Also GUI guides didn't helped.) public class Main { public static void main(String[] args) { FrameOne frame = new FrameOne(); } } FrameOne class: public

How to create a JButton with unicode image in Java?

青春壹個敷衍的年華 提交于 2019-12-24 18:17:07
问题 I have the following code : JButton Get_Unicode_Button(String unicodeText) { JButton button=new JButton("\\u"+unicodeText); // JButton button=new JButton("\u2605"); // JButton button=new JButton("\u267b"); // JButton button=new JButton("\u1F602"); // ? return button; } I want to get a button displaying an image from unicode, I have a list of unicodes like this : "2605", "267b", "1F602", but it seems the way I implemented it above doesn't work, what's the right way to do it ? Especially the

Why is my simple JFrame displaying weird?

为君一笑 提交于 2019-12-24 17:11:19
问题 I am a newbie at Java Swing/AWT and I have this code following working for a simple PopUp dialog which closes on any of the JButtons beind clicked, but displays real wonky. Does anybody have suggestions on what and how to fix? import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax

Refreshing chart when a new file is opened

こ雲淡風輕ζ 提交于 2019-12-24 14:13:45
问题 I have to represent data returned from a file chooser to a chart. Everything works fine until I press the button again and choose a different file. The problem is that instead of representing the new dataset, it's adding it to the previous one. I tried the revalidate , repaint and remove methods, but nothing worked (or I didn't know where to put those methods. My code looks like this: JButton theButton = new JButton("Choose the file"); theButton.addActionListener(new ActionListener() { public

NullPointerException for instantiated button in action listener

我是研究僧i 提交于 2019-12-24 13:25:39
问题 The following code is a snippet of a program I am getting a nullpointer exception from. When I press the "Add" button on the GUI, an error message pointing to this line: buttonPanel.addButton.setEnabled(false); is displayed. I'm guessing that the addButton is null for some reason although I instantiated it in the constructor of buttonPanel: addButton = new JButton("Add"); addButton.addActionListener(buttonListener); Why is the null pointer error Exception in thread "AWT-EventQueue-0" java

Java JButton release action

人盡茶涼 提交于 2019-12-24 13:20:06
问题 I am new to java and I'm having some problems with Jbuttons. I'm thinking of playing a button click sound when the button is pressed and playing a different sound when the button is released. I already know how to assign an actionlistener to a jbutton, but I have no idea of how to do this, and I didn't find an answer in Google. Thanks in advance. 来源: https://stackoverflow.com/questions/22942131/java-jbutton-release-action

How do I close a JDialog window using a JButton?

你。 提交于 2019-12-24 12:18:16
问题 I've tried a bunch of different ways to close the window, but since you can't send additional parameters to to Action Listener method I can't dispose the frame due to a pointer exception for the frame. This is my current code. import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class errorRequiredFieldMissing extends JDialog{ JLabel error; JButton exit; public static JFrame frame; public errorRequiredFieldMissing()

Accessing Variable within JButton ActionListener

冷暖自知 提交于 2019-12-24 09:29:32
问题 This seems like a very simple problem, but I'm having a lot of trouble figuring out how to deal with it. Sample Scenario: final int number = 0; JFrame frame = new JFrame(); frame.setVisible(true); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setSize(400, 400); final JTextArea text = new JTextArea(); frame.add(text, BorderLayout.NORTH); JButton button = new JButton(number + ""); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { number++; //

JAVA: Swing JButton componnent (NullPointerException)

若如初见. 提交于 2019-12-24 08:50:11
问题 I am trying to show text - "You pressed Button" when you pressed one of buttons. I am getting an NullPointerException . I have initialized the buttons inside the constructor of the class and after initialization, I called the following method from main() . Here is the code: import java.awt.event.*; import javax.swing.*; import java.awt.*; public class ButtonDemo implements ActionListener{ JLabel jlab; ButtonDemo(){ JFrame jfrm = new JFrame("A Button Example"); jfrm.setLayout(new FlowLayout())