JAVA GUI Button Question
so I have some code that looks like this:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class IPGUI extends JFrame implements ActionListener
{
private static JPanel contentPane;
private JButton btnConvertDocuments;
private JButton btnExtractImages;
private JButton btnParseRIDValues;
private JButton btnParseImageInfo;
//Create the frame
public IPGUI()
{
//Sets frame properties
setTitle("IP Extractor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//Creates new JPanel with boxlayout
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//////////////////New Button//////////////////
JButton btnConvertDocuments = new JButton("1. Convert Documents");
btnConvertDocuments.setAlignmentX(Component.CENTER_ALIGNMENT);
btnConvertDocuments.setMaximumSize(new Dimension(160, 0));
btnConvertDocuments.setPreferredSize(new Dimension(0, 50));
panel.add(btnConvertDocuments);
btnConvertDocuments.setActionCommand("w");
btnConvertDocuments.addActionListener((ActionListener) this);
JSeparator separator_3 = new JSeparator();
panel.add(separator_3);
//////////////////New Button//////////////////
btnExtractImages = new JButton("2. Extract Images");
btnExtractImages.setAlignmentX(Component.CENTER_ALIGNMENT);
btnExtractImages.setMaximumSize(new Dimension(160, 0));
btnExtractImages.setPreferredSize(new Dimension(0, 50));
panel.add(btnExtractImages);
btnExtractImages.setActionCom开发者_高级运维mand("x");
btnExtractImages.addActionListener((ActionListener) this);
JSeparator separator_2 = new JSeparator();
panel.add(separator_2);
//////////////////New Button//////////////////
JButton btnParseRIDValues = new JButton("3. Parse rId Values");
btnParseRIDValues.setAlignmentX(Component.CENTER_ALIGNMENT);
btnParseRIDValues.setMaximumSize(new Dimension(160, 0));
btnParseRIDValues.setPreferredSize(new Dimension(0, 50));
panel.add(btnParseRIDValues);
btnParseRIDValues.setActionCommand("y");
btnParseRIDValues.addActionListener((ActionListener) this);
JSeparator separator_1 = new JSeparator();
panel.add(separator_1);
//////////////////New Button//////////////////
JButton btnParseImageInfo = new JButton("4. Parse Image Info.");
btnParseImageInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
btnParseImageInfo.setMaximumSize(new Dimension(160, 0));
btnParseImageInfo.setPreferredSize(new Dimension(0, 50));
panel.add(btnParseImageInfo);
btnParseImageInfo.setActionCommand("z");
btnParseImageInfo.addActionListener((ActionListener) this);
}
//Actions performed when an event occurs.
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
//If btnConvertDocuments is clicked, the FileConverter method is called and the button is then disabled [so as to prevent duplicates].
if (command.equals("w"))
{
FileConverter fc = new FileConverter();
btnConvertDocuments.setEnabled(false);
}
//If btnExtractImages is clicked, the ImageExtractor method is called and the button is then disabled [so as to prevent duplicates].
else if (command.equals("x"))
{
ImageExtractor ie = new ImageExtractor();
btnExtractImages.setEnabled(false);
}
//If btnParseRIDValues is clicked, the XMLIDParser method is called and the button is then disabled [so as to prevent duplicates].
else if (command.equals("y"))
{
XMLIDParser xip = new XMLIDParser();
btnParseRIDValues.setEnabled(false);
}
//If btnParseImageInfo is clicked, the XMLTagParser method is called and the button is then disabled [so as to prevent duplicates].
else if (command.equals("z"))
{
XMLTagParser xtp = new XMLTagParser();
btnParseImageInfo.setEnabled(false);
}
}
}
Main
import java.awt.EventQueue;
public class IPDriver
{
public static void main(String[] args)
{
//Calls GUI and sets its visibility to true
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
IPGUI frame = new IPGUI();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
Error Produced
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at IPGUI.actionPerformed(IPGUI.java:119)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
SO I figure the error is coming from my main where I have EventQueue being called. And when I take out...
EventQueue.invokeLater(new Runnable()
{
public void run()
{
...and leave nothing but the try catch statement in, everything seems to work fine and the buttons become disabled. However, I was just wondering if it was necessary to leave the EventQueue statement in there, and what it's importance is?
Also a related question. I am trying to run this for an extended amount of folders (~100) and Button 1-3 work fine, but in button 4, it gets to the 18th folder and the GUI just freezes up. I was wondering if there was a way around this to prevent the freezing?
Thank you in advance for any help!
you have a local btnConvertDocuments in your IPGUI constructor, which is masking the class btnConvertDocuments object.
just have
btnConvertDocuments = new JButton("1. Convert Documents");
btnConvertDocuments.setAlignmentX(Component.CENTER_ALIGNMENT);
btnConvertDocuments.setMaximumSize(new Dimension(160, 0));
btnConvertDocuments.setPreferredSize(new Dimension(0, 50));
etc. for other buttons
The error is indeed coming from main, because that is where your application starts. However the error occurs because btnParseRIDValues
is null
In the piece of code below you overwrite some class variable by redefining the variable;
JButton btnParseRIDValues = new JButton("3. Parse rId Values");
try changing it to:
btnParseRIDValues = new JButton("3. Parse rId Values");
If you press btnParseImageInfo
or btnConvertDocuments
the same error will occur.
JButton btnParseImageInfo = new JButton("4. Parse Image Info.");
JButton btnConvertDocuments = new JButton("1. Convert Documents");
needs to be changed to:
btnParseImageInfo = new JButton("4. Parse Image Info.");
btnConvertDocuments = new JButton("1. Convert Documents");
Edit*
To prevent the GUI from freezing up you can run the actions threaded:
new Thread(new Runnable()
{
public void run()
{
FileConverter fc = new FileConverter();
}
}).start();
See http://www.javabeginner.com/learn-java/java-threads-tutorial for more info.
精彩评论