You should try to use classes and organize your code better. Below I have redesigned your existing code. I did not add anything, but I did move some stuff around and remove a couple unnecessary variables for this example.
I changed your ActionListener to an AbstractAction, just to let you know. :-p
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class GuiTester extends JPanel {
// Create a file chooser
private static final JFileChooser fc = new JFileChooser();
private JLabel inputFile;
public GuiTester(int width, int height) {
Font smallFont = new Font("sans", Font.PLAIN, 9);
JLabel description0 = new JLabel(" ");
JLabel description6 = new JLabel(" ");
JButton startButton = new JButton("Enter");
JButton inputSelectorButton = new JButton(new FileChooserAction(
"Select Input File"));
JButton outputSelectorButton = new JButton("Select Output File");
inputFile = new JLabel("Input File: Please select a file");
inputFile.setFont(smallFont);
inputFile.setAlignmentX(JComponent.CENTER_ALIGNMENT);
inputSelectorButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
outputSelectorButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
startButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
add(description0);
add(description6);
add(inputFile);
add(inputSelectorButton);
add(outputSelectorButton);
add(startButton);
setSize(width, height);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
}
public JLabel getInputFileLabel() {
return inputFile;
}
@Override
public void invalidate() {
super.invalidate();
// Invalidate the label, you really don't need this, but you should
// reference children in here if you want to explicitly invalidate them
// when the parent gets invalidated. The parent is responsible for
// telling its children what to do and when to do it.
inputFile.invalidate();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// This gets called when you repaint, since you are adding children
// to this panel, painting is pointless. You can however fill the
// background if you wish.
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Gui Test");
f.setContentPane(new GuiTester(370, 400));
f.setSize(370, 400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
}
});
}
public class FileChooserAction extends AbstractAction {
public FileChooserAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
Container c = ((JButton) e.getSource()).getParent();
GuiTester g = null;
if (c instanceof GuiTester) {
g = (GuiTester) c;
}
int returnVal = fc.showOpenDialog(c);
if (g != null && returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String fileName = "Input File: Please select a file";
if (file.exists())
fileName = file.getPath();
else
fileName = "File not found, please select a file";
System.out.println(fileName);
g.getInputFileLabel().setText(fileName);
g.validate();
g.repaint();
} else {
System.out.println("Open command cancelled by user.");
}
}
}
}