JTabbedPane+Panel stealing focus
Hi all,
I have a question about JTabbedPanes being used with JPanel. I have a JTabbedPane with 2 tabs, both tabs have 1 JPanel in each tab. The first tab has a JPanel using Flow Layout, the Second Tab has a panel using GridBag layout. While on the first tab, click on the little box in the upper left and it will drop down that menu that will show you "restore", "minimize", "maximize", and "close". Now go to the second tap and click the box again and you will notice that the menu drops down behind the Panel, but not behind the JTabbedPane. The menu isn't really a big deal but, I have a bunch of tool tips on all the Radios/Boxes/ and Fields and all the tool tips are also being displayed behind the Panel. Also, when you switch back to tab 1 the menu still works fine, so its solely with tab 2, is it the Grid Bag constraints?
Thanks a ton, heres the code...
publicclass TestMain{
publicstaticvoid main(String [] args){
Preferences pf =new Preferences();
}
}
import javax.swing.*;
import java.awt.*;
import javax.swing.plaf.*;
import java.awt.event.*;
publicclass Preferencesextends JFrame{
static JFrame frame;
staticboolean xmlDisabled =true;
static JCheckBox overlapCheckingBox;
//Global Variables
staticprivate String xmlLocation ="";
staticprivatelong updateDelay = 120000;
staticprivateboolean updateBoxIsChecked =false;
//Tab1
private JButton saveButton;
//Tab2
private JRadioButton localXML;//G1
private JRadioButton internetXML;//G1
private JRadioButton disableXML;//G1
staticprivate JRadioButton xmlOverwrite;//G2
staticprivate JRadioButton promptUser;//G2
staticprivate JRadioButton autoDetermine;//G2
staticprivate JTextField locationField;
staticprivate JTextField updateTimeField;
staticprivate JCheckBox updateBox;
staticprivate JCheckBox autoDetermineBox;
/** Creates a new instance of Preferences */
public Preferences(){
frame =new JFrame("Preferences");
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setContentPane(createContentPane());
frame.setLocation(300,250);
frame.pack();
frame.setSize(new Dimension(550,340));
frame.setVisible(true);
}
private Container createContentPane(){
Container container = this.getContentPane();
/*Font textFont = new Font("Dialog", Font.PLAIN, 12);
GridBagLayout gridbag = new GridBagLayout();
JPanel contentPane = new JPanel(gridbag);
container.setLayout(gridbag);
GridBagConstraints gbc = new GridBagConstraints();*/
//JTabbedPane
JTabbedPane tabbedPane =new JTabbedPane();
container.add(tabbedPane);
container.setLayout(new FlowLayout());
//Panel1
JPanel panel1 =new JPanel();
panel1.setLayout(new GridLayout(0,2));
//Label
panel1.add(new JLabel("Enable Time Overlap Checking:"));
//OverlapCheckBox
overlapCheckingBox =new JCheckBox();
overlapCheckingBox.setToolTipText("Check to Enable OverLap Checking!");
panel1.add(overlapCheckingBox);
panel1.add(new JLabel(""));
saveButton =new JButton("Save Changes");
ButtonListener saveButtonListener =new ButtonListener();
saveButton.addActionListener(saveButtonListener);
container.add(saveButton);
tabbedPane.addTab("General",panel1);
tabbedPane.addTab("XML Prefs",createXMLPanel());
//tabbedPane.setBackground(new ColorUIResource(0x69, 0x0f, 0x0f));
autoDetermine.doClick();
disableXML.doClick();
return container;
}
private Panel createGeneralPanel(){
Panel panel =new Panel();
return panel;
}
private Panel createXMLPanel(){
Panel panel =new Panel();
//panel.setBackground(new ColorUIResource(0x69, 0x0f, 0x0f));
GridBagLayout gridbag =new GridBagLayout();
JPanel contentPane =new JPanel(gridbag);
panel.setLayout(gridbag);
GridBagConstraints gbc =new GridBagConstraints();
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 4;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 0;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
disableXML =new JRadioButton("Disable XML Parsing");
disableXML.setToolTipText("Select this to Disable Updating the schedule via XML");
gridbag.setConstraints(disableXML, gbc);
ButtonListener disableXMLListener =new ButtonListener();
disableXML.addActionListener(disableXMLListener);
panel.add(disableXML);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 3;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 1;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
localXML =new JRadioButton("Local XML Parsing");
localXML.setToolTipText("Select this to Update the schedule via a local XML file");
gridbag.setConstraints(localXML, gbc);
ButtonListener localXMLListener =new ButtonListener();
localXML.addActionListener(localXMLListener);
panel.add(localXML);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 4;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 2;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
internetXML =new JRadioButton("Internet XML Parsing");
internetXML.setToolTipText("Select this to Update the schedule via an XML file on the Internet");
gridbag.setConstraints(internetXML, gbc);
ButtonListener internetXMLListener =new ButtonListener();
internetXML.addActionListener(internetXMLListener);
panel.add(internetXML);
ButtonGroup xmlGroup =new ButtonGroup();
xmlGroup.add(disableXML);
xmlGroup.add(localXML);
xmlGroup.add(internetXML);
gbc.fill = gbc.HORIZONTAL;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 9;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 3;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
locationField =new JTextField("XML file location");
locationField.setToolTipText("Enter the location of the XML file");
gridbag.setConstraints(locationField, gbc);
panel.add(locationField);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 5;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 4;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
updateBox =new JCheckBox("Check every this many minutes:");
updateBox.setSelected(true);
updateBox.setToolTipText("If unchecked, it will only grab the XML once");
gridbag.setConstraints(updateBox, gbc);
panel.add(updateBox);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 3;// how many horizontal cells it uses
gbc.gridx = 5;// its horizontal cell/grid position
gbc.gridy = 4;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
updateTimeField =new JTextField(Long.toString(((updateDelay/1000)/60)));
updateTimeField.setToolTipText("Input the number of minutes of how often to check for changes in the XML file");
updateTimeField.setColumns(3);
gridbag.setConstraints(updateTimeField, gbc);
panel.add(updateTimeField);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 4;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 5;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
xmlOverwrite =new JRadioButton("XML overwrites ALL");
xmlOverwrite.setToolTipText("Select this to have the XML file's schedule overwrite all");
gridbag.setConstraints(xmlOverwrite, gbc);
ButtonListener xmlOverwriteListener =new ButtonListener();
xmlOverwrite.addActionListener(xmlOverwriteListener);
panel.add(xmlOverwrite);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 4;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 6;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
promptUser =new JRadioButton("Prompt User for each Change");
promptUser.setToolTipText("Select this to be prompt for each change detected int the XML file");
gridbag.setConstraints(promptUser, gbc);
ButtonListener promptUserListener =new ButtonListener();
promptUser.addActionListener(promptUserListener);
panel.add(promptUser);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 11;// how many horizontal cells it uses
gbc.gridx = 0;// its horizontal cell/grid position
gbc.gridy = 7;// its vertical cell/grid position
gbc.anchor = gbc.WEST;// within the cell it defines it as
gbc.weightx = .1;
gbc.weighty = .1;
autoDetermine =new JRadioButton("Only overwrite non-human adjusted/added schedules");
autoDetermine.setToolTipText("Select this to have only non-human adjusted/added schedules be overwritten/adjusted automatically");
gridbag.setConstraints(autoDetermine, gbc);
ButtonListener autoDetermineListener =new ButtonListener();
autoDetermine.addActionListener(autoDetermineListener);
panel.add(autoDetermine);
ButtonGroup xml2Group =new ButtonGroup();
xml2Group.add(xmlOverwrite);
xml2Group.add(promptUser);
xml2Group.add(autoDetermine);
gbc.fill = gbc.NONE;
gbc.insets =new Insets(2, 2, 2, 2);// sets 2 pixels of space around
gbc.gridheight = 1;// how many vertical cells it uses
gbc.gridwidth = 7;// how many horizontal cells it uses
gbc.gridx = 2;// its horizontal cell/grid position
gbc.gridy = 8;// its vertical cell/grid position
gbc.anchor = gbc.CENTER;// within the cell it defines it as
gbc.weightx = .5;
gbc.weighty = .1;
autoDetermineBox =new JCheckBox("Remove items not on downloaded schedule");
autoDetermineBox.setToolTipText("Select this to remove non-human items that are not on the downloaded schedule");
gridbag.setConstraints(autoDetermineBox, gbc);
panel.add(autoDetermineBox);
return panel;
}
privatestaticboolean verifyPreferences(){
boolean value =true;
value = validateXMLPanel();
return value;
}
staticprivateboolean validateXMLPanel(){
boolean value =true;
if(!xmlDisabled){
if(locationField.getText().equals("") || locationField.getText().equals("CANNOT BE LEFT EMPTY!") || locationField.getText().equals("XML file location")){
locationField.setForeground(Color.RED);
locationField.setText("CANNOT BE LEFT EMPTY!");
value =false;
}
elseif(locationField.getText() !="")
locationField.setForeground(Color.BLACK);
if(updateBox.isSelected()){
if(updateTimeField.getText().equals("") || updateTimeField.getText().matches("0*")){
updateTimeField.setText("1");
}
try{if(Integer.parseInt(updateTimeField.getText()) > 999)
updateTimeField.setText("999");
}catch(Exception e){updateTimeField.setText("999");}
if(updateTimeField.getText().matches(".*\\D.*")){
updateTimeField.setForeground(Color.RED);
value =false;
}else
updateTimeField.setForeground(Color.BLACK);
}
}
System.out.println(value);
return value;
}
staticclass ButtonListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent e){
String button = e.getActionCommand();
if(button =="Save Changes"){
if(verifyPreferences()){
if(!xmlDisabled){
xmlLocation = locationField.getText();
updateDelay = ((Long.parseLong(updateTimeField.getText())*1000)*60);
}
frame.dispose();
}
}
elseif(button =="Disable XML Parsing"){
xmlDisabled =true;
locationField.setEnabled(false);
updateBox.setEnabled(false);
updateTimeField.setEnabled(false);
xmlOverwrite.setEnabled(false);
promptUser.setEnabled(false);
autoDetermine.setEnabled(false);
autoDetermineBox.setEnabled(false);
}
elseif(button =="Local XML Parsing" || button =="Internet XML Parsing"){
xmlDisabled =false;
locationField.setEnabled(true);
updateBox.setEnabled(true);
updateTimeField.setEnabled(true);
xmlOverwrite.setEnabled(true);
promptUser.setEnabled(true);
autoDetermine.setEnabled(true);
if(autoDetermine.isSelected())
autoDetermineBox.setEnabled(true);
}
elseif(button =="XML overwrites ALL" || button =="Prompt User for each Change" )
autoDetermineBox.setEnabled(false);
elseif(button =="Only overwrite non-human adjusted/added schedules")
autoDetermineBox.setEnabled(true);
}
}
}
Sorry it's a little long. I'm not an excellent programmer by any means, so any helpful advice about formatting, or standards/guidelines that I've failed to comply with will be well taken :)
-Airplane
P.S. Some of the code has been altered slightly so clicking buttons doesn't really perform anything but enable/disable other buttons.

