Results 1 to 6 of 6
Thread: JButton not clickable
- 10-28-2011, 08:47 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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); } } } }
- 10-28-2011, 10:06 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 10-31-2011, 03:30 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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.
- 10-31-2011, 04:24 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 11-09-2012, 07:30 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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.
- 11-09-2012, 08:59 PM #6
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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
-
ActionListener for JButton after changing Button to JButton
By ravi.joshi53 in forum Java AppletsReplies: 2Last Post: 10-07-2011, 07:35 AM -
Make a clickable moving image?
By scheffetz in forum New To JavaReplies: 1Last Post: 04-08-2011, 07:54 PM -
Double Clickable Application
By ellias2007 in forum Advanced JavaReplies: 7Last Post: 03-08-2010, 04:08 PM -
Clickable icons in a JPanel \ JList
By szakee in forum AWT / SwingReplies: 1Last Post: 04-13-2009, 11:16 PM -
Putting clickable icons on an image
By szakee in forum AWT / SwingReplies: 3Last Post: 04-06-2009, 10:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks