Can I ask for some advice / help please, I am writing a UI that upon the user making a selection from a drop down list it then populates details about the selection in text boxes. I am writing this as three classes the main class, the UI (User Interface class) and a third class was to hold StringArrays[ ] for various models of car for example.
My issue is I am struggling to access the StringArrays elements from the other class.The code below has it in the same class and this works.
I have done this with sortedList -within the code below- without problem but ideally wanted to use StringArrays and as a seperate class.
Is it possible to explain to me how I access an element of a StringArray from another class. If I try to basically access an element from the StringArray in a seperate class from the UI it advises that it does not see the variable Anglia(see below)
my main class as follows (All works)
package brandspecer;
/**
*
* @author Ian
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
UI tester = new UI();
}
}
MY UI Code not finished but all working and what's there functions.
package brandspecer;
/**
*
* @author Ian
*/
/** Creates a new instance of UI */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java .awt.event.*;
/**
*
* @author Ian
*/
public class UI {
JList jlist;
JLabel jlab;
JTextField jtxt1;
JTextField jtxt2;
JScrollPane jscrlp;
JButton jbtnBuy;
String speed = new String(" Speed ");
String choice;
String ABIB[] = new String[] {"Cort","Ford","Cortina","1979","GREY","2.0","OHV","15 to 17s","116" };// The StringArray can be used with the UI class
sortedList model = new sortedList();// This works and calls a sortedList from another class.
// fill model
JList list = new JList(model);
/** Creates a new instance of UI */
public UI() {
// Create JFrame container to begin.
JFrame jfrm = new JFrame("UI Demo");
// Specify a layout.
jfrm.getContentPane().setLayout(new FlowLayout());
// Set initial size of frame.
jfrm.setSize(220, 350);
// Terminate the program when user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the list selection mode to single selection.
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//Add list to a scroll pane.
jscrlp = new JScrollPane(list);
// Set the preffered size of the scroll pane.
jscrlp.setPreferredSize(new Dimension(197, 120));
// Make a label that displays the selection.
jlab = new JLabel(" Please select a make ");
//Add a selection listener for the list.
list.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent le)
{
//get the index of the item selected.
int idx = list.getSelectedIndex();
//Display selection.
if(idx != -1)
jlab.setText("Current selection " + model.getElementAt(idx));
else
jlab.setText(" Please select a make ");
}
} );
jbtnBuy = new JButton("Choose Brand");
jbtnBuy.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int idx = list.getSelectedIndex();
if(idx != -1)
jlab.setText("You selected " + model.getElementAt(idx));
if(idx == 0)
{
jtxt1.setText("Info 1 = "+ ABIB[7]);
jtxt2.setText("Info 2 = " + ABIB[6]);
}
else
jlab.setText("No Make selected");
}
});
//Create JTextField for answer.
jtxt1 = new JTextField(speed,15);
jtxt2 = new JTextField(" ",15);
jfrm.getContentPane().add(jscrlp);
jfrm.getContentPane().add(jbtnBuy);
jfrm.getContentPane().add(jlab);
jfrm.getContentPane().add(jtxt1);
jfrm.getContentPane().add(jtxt2);
jfrm.setVisible(true);
}
}
And finally my StringArray class code (I have not added the code for the SortedList class shown in the UI code above as this works and is not in question)
package brandspecer;
import javax.swing.*;
import java.util.*;
/**
*
* @author Ian
*/
public class StringArrays
{
public String Anglia[];
/** Creates a new instance of StringArrays */
public StringArrays()
{
String Anglia[]={"Anglia","933cc","109E","39 - 48","4 cyl SV","Petrol","2 door","0-60 (N/A)","55,807" };
}
// its this string I want to access.
}