Results 1 to 6 of 6
  1. #1
    StevenTNorris is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default JButton not clickable

    I have a JDialog that has both a JLabel and JButton inside it. The JButton is unclickable and I cannot figure out why. I've tried removing other elements. I've tried creating another JDialog as a test and setting to visible immeidately with the same issue. Below is the class with the JDialog in it. It's a fairly extensive program, so just ask and I'll supply other classes as necessary. Any help or direction is welcomed, not just full-on solutions. I really just need something else to try or someone who's had the same problem before me.

    Java Code:
    package si.gui;
    
    
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.KeyEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import si.classes.HotDocTreeWriterController;
    import si.listeners.*;
    
    public class GUIMenu extends JMenuBar{
    	/**
    	 * Serial ID
    	 */
    	private static final long serialVersionUID = 1L;
    	private JMenu fileMenu;
    	private JMenuItem openMenu;
    	private JMenuItem saveMenu;
    	private JMenuItem exportMenu;
    	private JMenuItem scanFileMenu;
    	private JMenu scanMenu;
    	private JMenuItem scanLibraryMenu;
    	private JMenuItem scanFolderMenu;
    	private JMenu editMenu;
    	private JMenuItem findMenu;
    	private JMenu insertedMenu;
    	private JMenuItem insertedRegMenu;
    	private JMenuItem insertedDeepMenu;
    	private JMenuItem imageMenu;
    	private JMenu searchMenu;
    	private JMenuItem textSearchMenu;
    	private JMenuItem varSearchMenu;
    	private JMenu helpMenu;
    	private JMenuItem versionMenu;
    	private JMenuItem helpDocMenu;
    	private HotDocTreeWriterController control;
    	private GUI gui;
    	private ImageIcon icon;
    	private JDialog searchCancel;
    	private JButton cancelButton;
    	private JLabel searchLabel;
    	private GridBagConstraints c;
    	private JPanel scanningPane;
    	private JButton scanCancel;
    	private JLabel scanning;
    	
    	public GUIMenu(GUI gui){
    		this.gui = gui;
    		this.control = gui.getControl();
    		fileMenu = new JMenu("File");
    		openMenu = new JMenuItem("Open");
    		saveMenu = new JMenuItem("Save");
    		exportMenu = new JMenuItem("Export");
    		scanMenu = new JMenu("Scan");
    		scanLibraryMenu = new JMenuItem("Scan Library");
    		scanFolderMenu = new JMenuItem("Scan Folder");
    		scanFileMenu = new JMenuItem("Scan File");
    		editMenu = new JMenu("Edit");
    		findMenu = new JMenuItem("Find");
    		insertedMenu = new JMenu("Check Inserts");
    		insertedRegMenu = new JMenuItem("Top Level");
    		insertedDeepMenu = new JMenuItem("Deep");
    		imageMenu = new JMenuItem("Images");
    		searchMenu = new JMenu("Search");
    		textSearchMenu = new JMenuItem("Text");
    		varSearchMenu = new JMenuItem("Variable");
    		helpMenu = new JMenu("Help");
    		versionMenu = new JMenuItem("About");
    		helpDocMenu = new JMenuItem("Documentation");
    		searchCancel = new JDialog(gui, "Searching");
    		cancelButton = new JButton("Cancel");
    		searchLabel = new JLabel("Searching...");
    		scanningPane = new JPanel();
    		scanCancel = new JButton("Cancel");
    		scanning = new JLabel("Scanning...");
    		c = new GridBagConstraints();
    		icon = new ImageIcon(ClassLoader.getSystemResource("si/images/icon.png"));
    	}
    	
    	public void initialize(){
    		helpMenu.setMnemonic(KeyEvent.VK_H);
    		versionMenu.setMnemonic(KeyEvent.VK_A);
    		helpDocMenu.setMnemonic(KeyEvent.VK_D);
    		helpMenu.add(helpDocMenu);
    		helpMenu.add(versionMenu);
    		openMenu.setMnemonic(KeyEvent.VK_O);
    		fileMenu.add(openMenu);
    		scanMenu.setMnemonic(KeyEvent.VK_C);
    		fileMenu.add(scanMenu);
    		scanFolderMenu.setMnemonic(KeyEvent.VK_F);
    		scanMenu.add(scanFolderMenu);
    		scanLibraryMenu.setMnemonic(KeyEvent.VK_L);
    		scanMenu.add(scanLibraryMenu);
    		scanFileMenu.setMnemonic(KeyEvent.VK_I);
    		scanMenu.add(scanFileMenu);
    		saveMenu.setMnemonic(KeyEvent.VK_S);
    		fileMenu.add(saveMenu);
    		saveMenu.setEnabled(false);
    		exportMenu.setMnemonic(KeyEvent.VK_E);
    		fileMenu.add(exportMenu);
    		exportMenu.setEnabled(false);
    		fileMenu.setMnemonic(KeyEvent.VK_F);
    		findMenu.setMnemonic(KeyEvent.VK_F);
    		insertedMenu.setMnemonic(KeyEvent.VK_I);
    		insertedRegMenu.setMnemonic(KeyEvent.VK_T);
    		insertedDeepMenu.setMnemonic(KeyEvent.VK_D);
    		insertedMenu.add(insertedRegMenu);
    		insertedMenu.add(insertedDeepMenu);
    		imageMenu.setMnemonic(KeyEvent.VK_I);
    		searchMenu.setMnemonic(KeyEvent.VK_S);
    		textSearchMenu.setMnemonic(KeyEvent.VK_T);
    		varSearchMenu.setMnemonic(KeyEvent.VK_V);
    		searchMenu.add(textSearchMenu);
    		searchMenu.add(varSearchMenu);
    		searchMenu.add(imageMenu);
    		editMenu.add(findMenu);
    		editMenu.add(insertedMenu);
    		editMenu.add(searchMenu);
    		editMenu.setMnemonic(KeyEvent.VK_E);
    		editMenu.setEnabled(false);
    		this.add(fileMenu);
    		this.add(editMenu);
    		this.add(helpMenu);
    		
    		//Searching dialog setup
    		searchCancel.setLayout(new GridBagLayout());
    		searchCancel.setSize(100, 100);
    		c.fill = GridBagConstraints.BOTH;
    		c.gridx = 0;
    		c.gridy = 0;
    		c.gridwidth = 3;
    		c.weightx = 0;
    		c.weighty = 0;
    		c.ipadx = 0;
    		c.ipady = 20;
    		searchCancel.add(searchLabel,c);
    		c.fill = GridBagConstraints.NONE;
    		c.gridx = 1;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		c.weightx = 0;
    		c.weighty = 0;
    		c.ipadx = 0;
    		c.ipady = 0;
    		searchCancel.add(cancelButton,c);
    		
    		//Scanning panel setup
    		scanningPane.setLayout(new FlowLayout());
    		scanningPane.add(scanning);
    		scanningPane.add(scanCancel);
    	}
    	
    	/**
    	 * Gets file menu
    	 * @return file menu
    	 */
    	public JMenu getFileMenu(){
    		return fileMenu;
    	}
    	
    	/**
    	 * Gets scanning pane
    	 * @return scanning pane
    	 */
    	public JPanel getScanningPane(){
    		return scanningPane;
    	}
    	
    	/**
    	 * Gets export menu
    	 * @return export menu
    	 */
    	public JMenuItem getExportMenu(){
    		return exportMenu;
    	}
    	
    	/**
    	 * Gets save menu
    	 * @return save menu
    	 */
    	public JMenuItem getSaveMenu(){
    		return saveMenu;
    	}
    	
    	/**
    	 * Gets edit menu
    	 * @return edit menu
    	 */
    	public JMenu getEditMenu(){
    		return editMenu;
    	}
    	
    	/**
    	 * Returns the search cancel dialog
    	 * @return search cancel dialog
    	 */
    	public JDialog getSearchCancel(){
    		return searchCancel;
    	}
    	
    	public void addListeners(){
    
    		imageMenu.addActionListener(new SearchListener(gui,"Images"));
    		varSearchMenu.addActionListener(new SearchListener(gui,"Variables"));
    		textSearchMenu.addActionListener(new SearchListener(gui,"Text"));
    		helpDocMenu.addActionListener(new HelpDocListener(gui));
    		versionMenu.addActionListener(new VersionListener(gui));
    		scanFolderMenu.addActionListener(new ScanListener(gui, ScanListener.FOLDER));
    		openMenu.addActionListener(new OpenListener(gui));
    		saveMenu.addActionListener(new SaveListener(gui));
    		exportMenu.addActionListener(new ExportListener(gui));
    		scanLibraryMenu.addActionListener(new ScanListener(gui, ScanListener.LIBRARY));
    		scanFileMenu.addActionListener(new ScanListener(gui,ScanListener.FILE));
    		findMenu.addActionListener(new FindListener(gui));
    		insertedRegMenu.addActionListener(new InsertedListener(gui,false));
    		insertedDeepMenu.addActionListener(new InsertedListener(gui,true));
    		scanCancel.addActionListener(new CancelListener(gui));
    		cancelButton.addActionListener(new CancelListener(gui));
    	}
    	
    	/**
    	 * Sets up viewpanel when scan is finished.
    	 */
    	public void scanFinished(){
    		fileMenu.setEnabled(true);
    		if(control.getNode() != null){
    			gui.getLocationField().setText(control.getNode().getFile().getAbsolutePath());
    			gui.getViewPanel().removeAll();
    			gui.populateViewPane(gui.getViewPanel(), control.getNode(),0);
    			saveMenu.setEnabled(true);
    			exportMenu.setEnabled(true);
    			editMenu.setEnabled(true);
    			gui.getExportButton().setEnabled(true);
    			gui.getDebugBox().setEnabled(true);
    		}
    		else{
    			gui.getLocationField().setText("");
    			gui.getViewPanel().removeAll();
    			gui.getViewPanel().updateUI();
    			saveMenu.setEnabled(false);
    			exportMenu.setEnabled(false);
    			editMenu.setEnabled(false);
    			gui.getExportButton().setEnabled(false);
    			gui.getDebugBox().setEnabled(false);
    		}
    	}
    	
    	/**
    	 * Sets up results window when search is completed
    	 * @param cancelled if search is cancelled
    	 * @param searchType type of search
    	 */
    	public void searchFinished(boolean cancelled,String searchType){
    		searchCancel.setVisible(false);
    		searchCancel.setModalityType(ModalityType.MODELESS);
    		if(!cancelled){
    			String tempString = control.getSearchResults();
    			JTextArea tempText = new JTextArea(30,30);
    			if(tempString != null){
    				tempText.setText(tempString);
    				tempText.setEditable(false);
    				tempText.setSelectionStart(0);
    				tempText.setSelectionEnd(0);
    				JScrollPane tempScroll = new JScrollPane(tempText);
    				JOptionPane.showMessageDialog(this, tempScroll, searchType + " in...", JOptionPane.OK_OPTION, icon);
    			}
    			else{
    				JOptionPane.showMessageDialog(this, "No matches found.", "Not Found", JOptionPane.OK_OPTION, icon);
    			}
    		}
    	}
    	
    	
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: JButton not clickable

    What button? Your code has multiple buttons. Be specific with your questions. We don't have time to guess what your are talking about.

    Did you add an ActionListener to the button?

    If you need more help then post a proper Short, Self Contained, Correct Example that demonstrates the problem. Your problem is with a button so, the SSCCE should be a frame with a single button.

  3. #3
    StevenTNorris is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: JButton not clickable

    I mentioned it was the button in the JDialog. There's only one of those. It's called cancelButton, and it does have an action listener. I've tried a SSCCE, and the problem does not arise. The issue isn't with the principle. I understand the coding concept; my implementation just has some bug in it I can't seem to find. In that case, an SSCCE would not help. What I need is direction with my existing code, not principles and concepts help.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: JButton not clickable

    > I mentioned it was the button in the JDialog. There's only one of those.

    It is NOT obvious to me. The code you posted isn't executable. There is so much code that is unrelated to the dispay of a button that I don't know what is going on.

    > What I need is direction with my existing code

    As you said the concept is simple. You add an ActionListner to the button, add the button to the window and it should work.

    So your problem is that you can't see the forest through the trees. So start commenting out code that is not related to the button so you can see the trees then the mistake will be easier to find.

  5. #5
    delblue is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: JButton not clickable

    This is old so I don't know whether you've fixed it. I've just run into a similar problem. All I can say so far is that I can fire the action listener on the button by tabbing to the button and pressing space to 'press' it. I can't seem to click it normally; the button doesn't respond visual or fire the action listener with the mouse. I used GridBagLayout onto tabs on a dialog to layout button and label combinations. My initial guesses would be I've messed up something to do with the context pane on the dialog, or something strange with the Grid Bag Layout. I'll see how I get on. I've never had this issue before programming swing ui.

  6. #6
    delblue is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: JButton not clickable

    I suspect it's some kind of interaction between the JLabel and the JButton in my case. I think the layout manager might be overlaying the JLabel's clickable region over the JButton meaning it can't be clicked. I've omitted a JLabel 'in front' of a JButton in the row and the button works. So, my guess is perhaps a layout issue; check your GridBagConstraints. Sorry my post is lacking in detail.
    Last edited by delblue; 11-09-2012 at 09:05 PM.

Similar Threads

  1. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  2. Make a clickable moving image?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 04-08-2011, 07:54 PM
  3. Double Clickable Application
    By ellias2007 in forum Advanced Java
    Replies: 7
    Last Post: 03-08-2010, 04:08 PM
  4. Clickable icons in a JPanel \ JList
    By szakee in forum AWT / Swing
    Replies: 1
    Last Post: 04-13-2009, 11:16 PM
  5. Putting clickable icons on an image
    By szakee in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2009, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •