Reputation: 4522
I'm working on an application in swing and need to place two JFileChoosers on a form. I can't seem to place them at specific locations(it is required that they are placed one next to each other not one above another). The code:
public class ServerGUI extends JFrame implements ActionListener {
private JPanel JPanel1 = null;
private JButton startStopButton = null;
private JTextField portSelect = null;
private JLabel portLabel = null;
private JFileChooser rootDir = null;
private JLabel rootLabel = null;
private JFileChooser maintenanceDir = null;
private JLabel maintenanceLabel = null;
private JCheckBox maintenanceMode = null;
private JLabel serverInfo = null;
ServerGUI() {
/// create an instance of our server
webserver = new WebServer();
/// build form controls -- with default options
JPanel1 = new JPanel();
startStopButton = new JButton("Start Server");
portSelect = new JTextField("10008");
portLabel = new JLabel("Port:");
rootDir = new RootChooser(System.getProperty("user.dir"));
/// choose directories only
rootDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
rootLabel = new JLabel("Choose web root directory:");
maintenanceDir = new MainChooser(System.getProperty("user.dir"));
/// choose directories only
maintenanceDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
maintenanceLabel = new JLabel("Choose maintenance root directory:");
maintenanceMode = new JCheckBox("Switch to maintenance mode");
/// not selected by default
maintenanceMode.setSelected(false);
serverInfo = new JLabel("Start");
/// add controls onto form
add(JPanel1, "Center");
serverInfo.setBounds(0, 0, 20, 10);
JPanel1.add(serverInfo);
startStopButton.addActionListener(this);
startStopButton.setBounds(35, 10, 2, 3);
startStopButton.setLayout(null);
JPanel1.add(startStopButton);
JPanel1.add(maintenanceMode);
JPanel1.add(portLabel);
JPanel1.add(portSelect);
/// set layout for directory choosers
// rootDir.setBounds(0, 0, 100, 100);
// rootDir.setLayout(null);
JPanel1.add(rootLabel);
JPanel1.add(rootDir);
// maintenanceDir.setBounds(130, 20, 100, 100);
// maintenanceDir.setLayout(null);
JPanel1.add(maintenanceLabel);
JPanel1.add(maintenanceDir);
}
}
I have tried variations between setBounds(), setLocation() for labels, setBound() and setLayout(null) for the button and created an extended class of JFileChooser in an attempt to hardcode the Dlg location.
static class RootChooser extends JFileChooser {
private static final long serialVersionUID = 1L;
protected JDialog createDialog(Component parent)
throws HeadlessException {
JDialog dlg = super.createDialog(parent);
dlg.setLocation(0, 40); // new location does not apply
return dlg;
}
RootChooser(String filePath) {
super(filePath);
}
}
and
static class MainChooser extends JFileChooser {
private static final long serialVersionUID = 1L;
protected JDialog createDialog(Component parent)
throws HeadlessException {
JDialog dlg = super.createDialog(parent);
dlg.setLocation(200, 140); // does not work as expected
return dlg;
}
MainChooser(String filePath) {
super(filePath);
}
}
How may I place these controls at specific locations?
Upvotes: 0
Views: 431
Reputation: 36423
Doing this
maintenanceDir = new MainChooser(System.getProperty("user.dir"));
wont help when maintenanceDir
is declared as JFileChooser
you would now be down-casting your custom JFileChooser
MainChooser
to a default one JFileChooser
.
The problem is you dont create an instance of your own custom JFileChooser
rather a normal JFileChooser
which does not have your custom createDialog(Component parent)
method:
...
private JFileChooser rootDir = null;
private JLabel rootLabel = null;
private JFileChooser maintenanceDir = null;
...
should be:
...
private RootChooser rootDir = null;
private JLabel rootLabel = null;
private MainChooser maintenanceDir = null;
...
Upvotes: 2