Hello, I try to rename file or directory using JFileChooser() and JButton(). But it gives me a NullPointer exeption. I have FileChooser, Button and TextField where I write new name for my file or directory. Can you say where I go wrong and how to solve this problem. // button action button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String newFileName = textField.getText(); // exeption here File oldFile = new File(fileChooser.getSelectedFile(), null); File newFileOrDirectoryName = new File(newFileName); if (oldFile.renameTo(newFileOrDirectoryName)) { System.out.println("renamed"); } else { System.out.println("Error"); } } }); This is a full code: package ua.edu.lp; import java.awt.Color; import java.awt.EventQueue; import …

Member Avatar
Member Avatar
+0 forum 2

hello to all i have problem about button events the problem is that if i handle an event of button and show again a window(result of event) that also has buttons then how to handle events of buttons of second window please help me

Member Avatar
Member Avatar
+0 forum 2

I've been trying to resize my jbuttons to the size of my pictures, but the jbuttons keep on occupying the whole frame. The size of the photos are 150x267. import javax.swing.*; import java.awt.event.KeyListener; import java.awt.*; import java.awt.event.KeyEvent; import java.util.Timer; import java.util.TimerTask; public class StartGame extends JFrame implements KeyListener { Timer timer; JPanel startpane, oakpane, choosepane; JButton a,b,c; int enter = 0; public StartGame() { super("Animon"); this.setBounds(100,100,1000,1000); startpane = new JPanel(); oakpane = new JPanel(); choosepane= new JPanel(); //start JLabel pic = new JLabel(new ImageIcon("animon2.png")); pic.setSize(400, 500); startpane.addKeyListener(this); startpane.setFocusable(true); startpane.add(pic); add(startpane); //oakpane addoakpane(); //choosepane this.addKeyListener(this); this.setFocusable(true); this.setVisible(true); } public void keyPressed(KeyEvent …

Member Avatar
Member Avatar
+0 forum 2

Heya all, I'm working on a small 'file browser', where the list of files is printed in a console (for now), but where the user inserts the file's path in a GUI. My gui is rather simple: one frame and one panel containing a JTextField and a JButton. I want the JButton to be activated when the user presses enter in the JTextArea (like in any other file browser) to increase useability. In order to do so, i made the button focusable and the default focus, and that works on windows, but not on linux. I know the default look …

Member Avatar
Member Avatar
+0 forum 2

import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JComboBox; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; public class OtakuHigh extends JFrame{ private JLabel label, label1, label2, label3, label4, label5, label6, label7, label8; private JTextField text1, text2, text4, text5, text7, text8; private JComboBox Mbox, Dbox, Sbox, Ybox, SCbox; private JButton Qbutton; String[] Month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String[] Day = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}; String[] …

Member Avatar
Member Avatar
+0 forum 2

Hi guys, I'm having trouble with my code and I'm kind of new in Java so I don't know what to do. I have to do a program with Buttons and each button has to take me to another class (I don't know if I'm being clear). For example if I click the button "Bingo" my class named Bingo (which I already have) should run but I don't know how to do that. Here's my code: import java.awt.*; import javax.swing.*; import java.io.*; import java.awt.event.*; public class GameFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; JFrame …

Member Avatar
Member Avatar
+0 forum 4

Hey everyone, I want to add action listeners to JButtons that were created dynamically by an ArrayList. Here is the code for the button creation. numOfCourse = 6; courseBtnArray.add(new JButton("Course 1")); gbc.gridx = 2; for (int i = 0; i < numOfCourses; i++) { gbc.gridy++; add(courseBtnArray.get(i), gbc); courseBtnArray.add(new JButton("Course " + (i + 2))); } gbc.gridy++; add(courseBtnArray.get(courseBtnArray.size() - 1), gbc); System.out.println("the size of the textbox arraylist is:" + courseBtnArray.size()); So how can i refer to these buttons because they themseleves don't have a referance. Here is one way: `courseBtnArray.get(1).addActionListener(clickListener);`. This works, but the user does have the ability to add …

Member Avatar
Member Avatar
+0 forum 2

Ok so I am trying to build a little program and I want to be able to click on the skate or dice button and have the program open a new JFrame that has been built in another class. Currently it opens its own JFrame that was built in this method. I have the other classes built i have put the skate class below the main menu method. If someone could help me get this work I would greatly appreciate it. Another thing I tried is the getContentPane and that creates and exception in the thread Main method public class …

Member Avatar
Member Avatar
+0 forum 3

Hello DaniwWeb Members :) I'm a fairly new programmer and I'm kinda embarressed to ask this question considering that the answer is probably blatently obvious. So forgive my ignorance :) But here goes, I have a String called "Ctype", It is relevant to my combobox which I created. My combobox works perfectly fine, and I can get it to work without using a "JButton" but I would like to use one. If I selected one of my options in the Combobox, it will instantly follow the instruction instead of waiting until I pressed "Next". I changed the code around abit …

Member Avatar
Member Avatar
+0 forum 2

HOw can i assign a default action of Enter key after cell of jtable is edited to a Button???

Member Avatar
Member Avatar
-1 forum 2

there are two panel in one panel there is search button and in the another one there is Jtable for displaying search result. In that jtable initially all the records are displayed. But when i try to search, it works but the problem is that i need to click search button two times for searching 1st time and from second time onwards it works normally means even with single click results are displayed properly. what mistake am i making ? please suggest.

Member Avatar
Member Avatar
+0 forum 5

Hello, I'm new to this site, and i really need help with something. I have been working on a checkerboard project for my second programming class, and now I'm stuck. I now how to set the icons, but now I am absolutely confounded as to how I move the icon from one jbutton to the nu=ext as a way of movement. I want to be able to do this by use of a set of mouse clicks. I also need to figure out how to do the AI for the opponent. Someone told me that i should set up a …

Member Avatar
Member Avatar
+0 forum 85

Ive created a memory game which has a grid of JButtons, when the button is clicked it changes the ImageIcon background to an animation which shows the front of the card. The problem is that when the button is clicked, the gif doesn't begin playing until the mouse is moved off the button and if the mouse is moved back on before the animation is finished it causes the animation to restart. Has anyone encountered this problem before? Or can think of a workaround. thanks

Member Avatar
Member Avatar
+0 forum 4

Hi everyone, I have a javascript, jQuery, and c++ background so im not completely new but am just starting to learn Java in a University setting. In our first program we are to run it within a Do...While loop so the user can click a JButton for yes or no, if they wish to run it again. It's a simple Prime Factorization program. I missed a day this week and think I missed the instruction I am missing for this loop to work. From what I have been reading for the last few hours is that I need to have …

Member Avatar
Member Avatar
+0 forum 10

First of all, I'm new to GUI so I may have made some terrible mistakes.. right, so I have a two dimensional array of buttons and I need to identify which button in the array is clicked, hovered over, and hovered off. For now I just want my program to say: "*click* I am button" + x + y //when a button is pressed "*hover over* I am button" + x + y //when the mouse enters a button "*hover off* I am button" + x + y //when the mouse exits a button So far I’ve solved the clicking …

Member Avatar
Member Avatar
+0 forum 2

I tried to put an image on a JButton and it's messed up: [url]http://imgur.com/ecBOt[/url] code: [CODE]import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Polluted extends JFrame implements ActionListener { ImageIcon bg = new ImageIcon("bg.png"); Image bgImage = bg.getImage(); public static void main(String[] args) { new Polluted(); } public Polluted() { JPanel panel = new JPanel(); Image playImage = getToolkit().createImage("play.png"); ImageIcon playButton = new ImageIcon(playImage); this.setSize(800, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Polluted"); this.setResizable(false); this.setLayout(new GridBagLayout()); JButton play = new JButton(playButton); this.add(play); this.setVisible(true); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Toolkit bgKit = Toolkit.getDefaultToolkit(); bgImage = bgKit.getImage("bg.png"); …

Member Avatar
Member Avatar
+0 forum 8

Help with java Button How about I'm making a music player but I like the look of the button you added the image as ImageIcon because for example the play button is round and I want to lose those edges rectangular button and wanted to find a way to insert that image and which can also be manipulated as a button. But I want you when you click on this image of an effect I click for example the color becomes a little lighter or something like that is highlighted thanks. This is the image I'm using: [img]http://us.123rf.com/400wm/400/400/elementals83/elementals831103/elementals83110300015/8991919-botones-de-web-azul-para-la-interfaz-del-reproductor-de-medios.jpg[/img] PD: Sorry …

Member Avatar
Member Avatar
+0 forum 4

i am using an array of JButtons b[23].... my objective is that when i click a button it has to change its background color to black but it is giving a nullpointer exception.... if(ae.getActionCommand().equals("0")) { System.out.println("button b[o] is pressed"); b[0].setBackground(Color.black); } } can anyone help me with this please........

Member Avatar
Member Avatar
+0 forum 5

Hello friends.I got some problems with a program I'm developing for a assignment. I want to add a image(background) to my Jframe and add some buttons. I wrote the code. I can load image into the frame but when I create the Jbutton and re-size it to smaller button it's not minimizing. the button shows all over the frame.. is there any solution.. ? thank you [CODE]import java.awt.Dimension; import javax.swing.*; public class GUItest01 extends JFrame { public static void main(String[] args) { JFrame frame = new JFrame("main"); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); ImageIcon ii = new ImageIcon("C:/icon.jpg"); JLabel lable = new JLabel(ii); JScrollPane jsp …

Member Avatar
Member Avatar
+0 forum 4

Good Day... the problem is that the Jbutton is not displaying in my Jpanel. can you help me, this will help me a lot, Thanks Here's the code [CODE] import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import javax.swing.JButton; import javax.swing.JOptionPane; public class GameBoard extends JPanel implements ActionListener { JButton start = new JButton("START"); JButton controls = new JButton("CONTROLS"); JButton credit = new JButton("CREDITS"); JButton exit = new JButton("EXIT"); public GameBoard() { JFrame frame = new JFrame("The Palace"); frame.setSize(800,600); frame.setResizable(false); frame.setVisible(true); } public void initComponents() { //to detect button clicks start.addActionListener(this); controls.addActionListener(this); credit.addActionListener(this); exit.addActionListener(this); add(start); add(controls); add(credit); add(exit); } public …

Member Avatar
Member Avatar
+0 forum 4

I have been playing about with some code tonight and trying to add things to it, so i know it wont make sense when talking about cats and dogs, females and males and music, I run this code and the GUI comes up I click add CD and the required fields appear but i get the error Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox I think this has something to do with the actionlistener looking for a combobox when it has a jbutton but i am unsure how to fix it I have tried casting the …

Member Avatar
Member Avatar
+0 forum 6

I'm trying to remove a JButton from a JPanel but my code does not work. Any help is appreciated. This is the JButton. Problem is the line 25 I guess. [CODE] class SquareButton extends JButton implements MouseListener { String text = null; GameArea ga = null; public SquareButton(GameArea ga) { this.ga = ga; setPreferredSize(new Dimension(30, 30)); setOpaque(false); addMouseListener(this); setFont(new Font("Verdana", Font.BOLD, 15)); setHorizontalAlignment(CENTER); setVerticalAlignment(CENTER); } public Insets getInsets() { return new Insets(0,0,0,0); } public void mouseClicked(MouseEvent e) { if(e.getButton() == 3) { changeIcon(); } else if(e.getButton() == 1) { ga.remove(this); ga.add(new SquareLabel()); revalidate(); } } private void changeIcon() { if(text …

Member Avatar
Member Avatar
+0 forum 3

I have a [ICODE]JFrame[/ICODE] window to which I am trying to add 3 panels. One panel is for input (text areas and their labels which I already have arranged within the panel), one is for a set of buttons, and one is for output. I want the the input and button panels to share the left half of the window (input panel on top and button panel below) and the output panel to take up the entire right half of the window by itself. Because of this, [ICODE]GridLayout[/ICODE], [ICODE]BoxLayout[/ICODE], and [ICODE]BorderLayout[/ICODE] don't work by themselves. I just tried making one …

Member Avatar
Member Avatar
+0 forum 10

[CODE]mport java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JButton; public class Main extends JApplet implements ActionListener,ItemListener { //private Image im; // image handler //private static final String IMAGE = "enviro2.jpg"; //starFish tapakSulaiman = new starFish(this); public void init() { // getImageSimple(); this.setSize(780,540); Container pane = getContentPane(); pane.setLayout(new BorderLayout()); JPanel input= new JPanel(); input.setLayout(new GridLayout(1,4)); ImageIcon fish = new ImageIcon("images/fish.jpg"); JButton bFish= new JButton("Fish",fish); bFish.setLocation(10, 40); bFish.setSize(100,100); input.add(bFish); Icon starFish = new ImageIcon("starfish.jpeg"); JButton bSFish= new JButton("Star Fish",starFish); // bFish.setSize(100,100); input.add(bSFish); Icon shell = new ImageIcon("shell.jpeg"); JButton bShell= new JButton("Shell",shell); …

Member Avatar
Member Avatar
+0 forum 6

I'm in the process of creating a program that would change a background color by pressing a button. Now, I already have everything set up. The background image is just an image of a rainbow, and when a button is clicked, it would change the whole background color. The thing is, I do not want the buttons at the top...I want the buttons to be in the center (see the images I included...2nd image is what I would like - I just edited it with Windows Paint). Is there an easy way to do this? Thanks. [CODE] import javax.swing.*; import …

Member Avatar
Member Avatar
+0 forum 2

Hi, first time poster, and fairly new to java swing.Im trying to make a scrabble game and am stuck while trying to place a letter on the board, or even removing one for that matter. The playing board is just an array of JButtons all derived from a ScrabbleButton object, some are subclassed for double word scores or player tiles. The problem is, I cant place a tile on the board by overwriting one of the buttons in the array with a new ScrabbleButton object here is the code for the board being initialised [CODE]public void buildBoard() { layout= new …

Member Avatar
Member Avatar
+0 forum 12

I have a class which extends JDialog which by default has some text on it and a couple of buttons. Most of the instance of the buttons are "Ok" and "Cancel" so the two buttons have their bounds set. If I try to change the label on the Ok button to something longer such "To Engage Plan" the text is truncated since the default size is too small. I want to be able to set the size of button based on its current insets, margins etc and use the new string to calculate the button width. I have tried: [CODE]okButton.setText(okLabel); …

Member Avatar
Member Avatar
+0 forum 3

Hi everybody, I've been working with gui recently, and I need help with my JButtons. I need to figure out how to set a button on the keyboard to click that JBUtton. I.e. When i hit the enter key it picks say the third button on the list or something like that. I've tried .setMnemonic with VK_Enter, but it still requires the user to press ALT+Enter. Is there a way to make it so that just a simple "Enter" keystroke (Ascii value 13) click the button? Thanx in advance!

Member Avatar
Member Avatar
+0 forum 2

Basically what I would to know is you have a button on one JPanel, and when the user clicks it, they are re-directed to the other JPanel. I would appreciate it if i could have some sort of hint or know about the overall structure of implementing this. Thanks,

Member Avatar
Member Avatar
+0 forum 2

Hi, This is my first forum post and I'm very new to both the daniweb and programming community. I've been working on a project for a month or so and it involves clicking JButton to change the values of the text associated with the JButton clicked. Everything runs fine except one button expands to cover the entire screen when I click a button. What am I doing wrong? Code's right here [CODE]// The Driver class import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import …

Member Avatar
Member Avatar
+0 forum 1

The End.