I have two java classes
SortsGui.java
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
import Sorts.*;
import javax.swing.event.*;
import java.awt.event.*;
public class SortsGui
{
JFrame myMainWindow = new JFrame("Sorts");
JPanel sortPanel = new JPanel();
MyMenuBar mbr = new MyMenuBar();
//first panel components
JTextField txtField = new JTextField();
JTextField txtField2 = new JTextField();
JTextField txtField3 = new JTextField();
JTextField txtField4 = new JTextField();
JTextField txtField5 = new JTextField();
JTextField txtField6 = new JTextField();
JTextField txtField7 = new JTextField();
JTextField txtField8 = new JTextField();
JTextField txtField9 = new JTextField();
String sortsArray[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20"};
JComboBox sortComboBox = new JComboBox(sortsArray);
//end first panel
public void runGUI()
{
myMainWindow.setBounds(10, 10, 800, 800);
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createSortTestPanel();
myMainWindow.getContentPane().add(sortPanel);
myMainWindow.setJMenuBar(mbr);
myMainWindow.setVisible(true);
}
public void createSortTestPanel()
{
MigLayout layout = new MigLayout("" , "[grow]");
sortPanel.setLayout(layout);
sortPanel.add(txtField,"growx");
sortPanel.add(txtField2,"growx");
sortPanel.add(txtField3,"growx");
sortPanel.add(txtField4,"growx");
sortPanel.add(txtField5,"wrap,growx");
sortPanel.add(txtField6,"growx");
sortPanel.add(txtField7,"growx");
sortPanel.add(txtField8,"growx");
sortPanel.add(txtField9,"growx");
selectNothing();
}
public void selectNothing()
{
sortPanel.addAncestorListener(new RequestFocusListener(false));
}
public static void main(String[] args)
{
SortsGui sG = new SortsGui();
sG.runGUI();
}
class RequestFocusListener implements AncestorListener
{
private boolean removeListener;
public RequestFocusListener()
{
this(true);
}
public RequestFocusListener(boolean removeListener)
{
this.removeListener = removeListener;
}
@Override
public void ancestorAdded(AncestorEvent e)
{
JComponent component = e.getComponent();
component.requestFocusInWindow();
if (removeListener)
component.removeAncestorListener( this );
}
@Override
public void ancestorMoved(AncestorEvent e) {}
@Override
public void ancestorRemoved(AncestorEvent e) {}
}
}
MyMenuBar.java
package Sorts;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
public class MyMenuBar extends JMenuBar
{
JLabel lblSorts = new JLabel("Select amount of numbers to sort");
String sortsArray[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20"};
JComboBox sortComboBox = new JComboBox(sortsArray);
SortsGui sG= new SortsGui();
public MyMenuBar()
{
setBorderPainted(true);
makePopUpMenu();
}
void makePopUpMenu()
{
add(lblSorts);
sortComboBox.addItemListener(new sortComboBoxChanged());
add(sortComboBox);
}
class sortComboBoxChanged implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
System.out.println(sortComboBox.getSelectedItem());
sG.selectNothing();
System.out.println("Losing Focus");
}
}
}
In this you can see that I have tried to call SortsGui into MyMenuBar however that produces a cannot find symbol error for SortsGui. So is it possible to call a class from a parent directory? And if it is could someone please correct my error?
Parent Directory
Sorts Directory
You need create a new package for SortsGui as it is in the default package and there is ambiguity.
e.g.:
Then you have to import the package in Mymenubar