JList using HashMap key (String) as its display? - java

I've been self studying Java for a couple months and have run into a challenge. I'm making a contact list application. I chose to use HashMap<String, Contact> as my storage for the contacts. The challenge I'm running into is the my unfamiliarity with Swing. I'm trying for the first time to use JList. I have got JList to work with a normal array of strings but now I want to use the Key value from the HashMap for the JList display.
I have read elsewhere that a custom ListModel will do, but I have failed to find anything concrete. The oracle document How to Use Lists uses the DefaultListModel in its examples. I have read using AbstractListModel or ListModel are steps in the right direction. There are three main classes so far:
Class Contact
public class Contact {
private String name;
private String phoneNumber;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Class Book
import java.util.HashMap;
import java.util.Map;
public class Book {
private Map<String, Contact> addressbook = new HashMap<String, Contact>();
public Map<String, Contact> getAddressbook() {
return addressbook;
}
public void setAddressbook(Map<String, Contact> addressbook) {
this.addressbook = addressbook;
}
}
Class UserInterface
This is where I'm having difficulty creating a custom list model that takes the String keys from my HashMap located in class Book.
import java.awt.BorderLayout;
public class UserInterface extends JPanel implements ActionListener {
private static final long serialVersionUID = 2161244209167568887L;
// Contact list display
JList contactList;
// Menu bar and accompanying menu items
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem newContactMenuButton;
private JMenuItem exitAppMenuButton;
// Buttons
private JButton newContactButton;
private JButton openContactButton;
private JButton deleteContactButton;
// Panels to place components into
private JPanel mainPanel;
private JPanel buttonPanel;
// For message dialogs
private JFrame messageDialog;
public UserInterface() {
// Add the JList
contactList = new JList(new ContactListModel()); // ??
// Creating the menu bar and its items
// Adding ActionListeners to the menu buttons
menuBar = new JMenuBar();
menu = new JMenu("File");
newContactMenuButton = new JMenuItem("New Contact");
exitAppMenuButton= new JMenuItem("Exit");
newContactMenuButton.addActionListener(this);
exitAppMenuButton.addActionListener(this);
menu.add(newContactMenuButton);
menu.add(exitAppMenuButton);
menuBar.add(menu);
// Creating the Buttons
// Adding ActionListeners to the buttons
newContactButton = new JButton("New Contact");
openContactButton = new JButton("Open Contact");
deleteContactButton = new JButton("Delete Contact");
newContactButton.addActionListener(this);
openContactButton.addActionListener(this);
deleteContactButton.addActionListener(this);
// Creating the Panels with Grid Layouts
mainPanel = new JPanel(new GridLayout());
buttonPanel = new JPanel(new GridLayout(0, 1));
// Adding components to the Panels
mainPanel.add(contactList);
buttonPanel.add(newContactButton);
buttonPanel.add(openContactButton);
buttonPanel.add(deleteContactButton);
// Adding and aligning the Panels
setBorder(BorderFactory.createEmptyBorder(45, 45, 45, 45));
add(mainPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
}
public void CreateAndShowUI() {
JFrame frame = new JFrame("Addressbook Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new UserInterface());
frame.setJMenuBar(this.menuBar);
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newContactButton) {
JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
}
if (e.getSource() == openContactButton) {
JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
}
if (e.getSource() == deleteContactButton) {
if(contactList.isSelectionEmpty()) {
JOptionPane.showMessageDialog(messageDialog, "No contact selected.");
} else if(!contactList.isSelectionEmpty()) {
}
}
if (e.getSource() == newContactMenuButton) {
JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
}
if (e.getSource() == exitAppMenuButton) {
System.exit(0);
}
}
}
final class ContactListModel extends AbstractListModel {
Book book = new Book();
Map<String, Contact> bookList = book.getAddressbook();
public Object getElementAt(int keys) {
keys = // ??
return keys;
}
#Override
public int getSize() {
return bookList.size();
}
}
Any genuine point in the right direction is highly appreciated. In the meantime I'll keep searching.
EDIT: Updated & Answered
Here are the relevant updated code bits. As user carmickr suggested I used DefaultListModel to handle the data from the address book HashMap.
private DefaultListModel<Set<String>> model;
private JList<Set<String>> contactList;
Then inside the UserInterface constructor:
// Create the DefaultListModel object
// Add the JList
model = new DefaultListModel<Set<String>>();
model.addElement(book.AB.keySet());
contactList = new JList<Set<String>>(model);

I'm having difficulty creating a custom list model that takes the String keys from my HashMap located in class Book.
You don't need to create a custom model. You can just add each Contact object to the DefaultListModel. The point of using models is that the model holds all the data and you use methods of the model to access the data.
Then the simplest way to get the JList to work is to implement the toString() method in your Contact object to return the property that you want to see in the JList.
EDIT: Updated & Answered
Here are the relevant updated code bits. As user carmickr suggested I used DefaultListModel to handle the data from the address book HashMap.
private DefaultListModel<Set<String>> model;
private JList<Set<String>> contactList;
Then inside the UserInterface constructor:
// Create the DefaultListModel object
// Add the JList
model = new DefaultListModel<Set<String>>();
model.addElement(book.AB.keySet());
contactList = new JList<Set<String>>(model);

Related

List of Values in Java Swing Form

How can I display an LoV in a Java Swing form as shown in Oracle Forms?
I have data concerning user identifiers and user names. And I want to display the user names in LoV, but if a user selects a name corresponding to a user, its identifier should be returned.
EDIT 1:
Here is the form code that I've used to display 'Lov'
import db.DBHelper;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class LovForm extends JDialog implements ActionListener {
private Connection conn;
private DBHelper db;
private JList list;
private DefaultListModel model;
private UserDetail userDetail;
private JButton btnOk;
public LovForm(Connection c, UserDetail u) {
this.conn = c;
this.userDetail = u;
initComponents();
}
private void initComponents() {
db = new DBHelper(this.conn);
btnOk = new JButton("Ok");
btnOk.addActionListener(this);
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
model = new DefaultListModel();
try {
ResultSet rs = db.getAllAppUsers();
if (rs != null) {
while (rs.next()) {
String name = rs.getString("NAME");
model.addElement(name);
}
list = new JList(model);
}
} catch (Exception e) {
list = new JList();
}
list.setPreferredSize(new Dimension(150, 250));
JScrollPane listScroller = new JScrollPane(list);
JLabel lbl = new JLabel("Select an application user");
c.add(lbl);
c.add(listScroller);
c.add(btnOk);
this.setTitle("List of Users");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(new Dimension(200, 250));
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setModal(true);
this.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e
) {
if (e.getSource() == btnOk) {
String selectedItem = (String) list.getSelectedValue();
userDetail.setUserName(selectedItem);
this.dispose();
}
}
}
I would add a wrapper class for the User, which contains its name and its id.
Do not forget to override the toString() method of the User (that method will be used when rendering the list).
While populating the list, create User objects, and therefore you will have access to both its name and its id.
See code bellow:
private void initComponents() {
// your code....
try {
ResultSet rs = db.getAllAppUsers();
if (rs != null) {
while (rs.next()) {
String name = rs.getString("NAME");
int id = rs.getInt("ID");
model.addElement(new User(name, id));
}
list = new JList(model);
}
} catch (Exception e) {
list = new JList();
}
// your code...
}
#Override
public void actionPerformed(ActionEvent e
) {
if (e.getSource() == btnOk) {
User selectedItem = (User) list.getSelectedValue();
userDetail.setUserName(selectedItem.getName());
int id = selectedItem.getId();
this.dispose();
}
}
public class User {
private String name;
private Integer id;
public User(String name, Integer id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public Integer getId() {
return id;
}
#Override
public String toString() {
return name;
}
}
You could use a JComboBox.
You would create a custom Object to store both pieces of data. Then you would create a custom renderer to display whatever data you want in the combo box. Your processing code would then use the other property.
Check out Combo Box With Custom Renderer for an example of the rendering code you would use. This renderer is more complete than most rendering code you will find in the forum because it will still support selection of the item from the combo box using the keyboard.
I see you updated your question to show you are using a JList. Well the answer is still the same. You need a custom renderer for your JList. You can base your renderer off the example from the above link except you would extend the DefaultListCellRenderer.

Java code needs to restrict the user input

I got a serious problem on my code.
I got a swing JDBC code which I need to fill a table of names, addresses and an ID for the person, different than the table's ID. I created a swing input code for it, however, I wish it NOT to include the ID number's possibility - therefore, to make the swing have nothing else but the name and address being able to be set by the used, and not to show the ID at all.
Is there a possibility for it?
The creation of new partner, which has the name, address and the IdentityNumber strings, all private and their getters and setters public.
{
protected final String FRAME_TITLE = "Vehicle Repository";
private DatabaseHandler dbHandler;
private JTabbedPane tabbedPane;
private JTable partnerTable;
private JpaControlledTableModel<Partner> partnerTableModel;
#Override
public void onCreate() {
setDefaults(FRAME_TITLE);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
dbHandler = new DatabaseHandler();
dbHandler.open();
tabbedPane = new JTabbedPane();
partnerTableModel = new AsyncFullQueryingTableModel<>(dbHandler.getPartnerJpaController(), dbHandler.getEntityClassesToControllersMap());
parterTable = new JTable(partnerTableModel);
tabbedPane.addTab("Partners", new JScrollPane(parterTable));
getContentPane().add(tabbedPane, BorderLayout.CENTER);
}
#Override
public JMenuBar createJMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Data");
menuBar.add(menu);
JMenuItem menuItem;
menuItem = new JMenuItem(newPartnerAction);
menu.add(menuItem);
return menuBar;
}
private Action newPartnerAction = new AbstractAction("New parnter") {
#Override
public void actionPerformed(ActionEvent e) {
Partner partner = new Partner();
EntityEditorDialog<Partner> editorDialog = EntityEditorDialogFactory.createEditorDialog(partner, dbHandler.getPartnerJpaController());
editorDialog.setVisible(true);
if (partner.getId() != null) {
partnerTableModel.refresh();
}
}
};
private String getString(String message) {
return JOptionPane.showInputDialog(rootPane, message, "Data input", JOptionPane.QUESTION_MESSAGE);
}
private Partner getPartner(String message) {
Object[] partners = dbHandler.getPartnerJpaController().findEntities().toArray();
if (partners.length == 0) {
return null;
} else {
return (Partner) JOptionPane.showInputDialog(rootPane, message, "Data input", JOptionPane.QUESTION_MESSAGE, null, partners, partners[0]);
}
}
#Override
public void dispose() {
dbHandler.close();
super.dispose();
}
}
};
and not to show the ID at all
You can remove a column from display in the JTable:
table.removeColumn( table.getColumn(...) );

ListSelectionListener and CardLayout

I'm creating a program that reads data from a file, displays it on a GUI that has a JList and JButtons. I am trying to write it with CardLayout so the appropriate JPanel can be displayed when an item is selected from the JList or a JButton is clicked (i.e. next, previous, first and last). I am able to successfully read from the file and display data to the GUI. I've run into 2 problems and I've tried searching online for answers but cant seem to figure it out:
1) How do I get the JPanels to switch using CardLayout?
2) How do I get the data to be displayed in the GUI in text fields when a user clicks an item from the JList? The JList does appear and my ListSelectionListener is working because when I click on a particular item, it will print to the console (as a test).
If I comment out all of the JPanels except for 1, then it is correctly displayed but when I place all of them, then it does not switch.
So far, I have this for my ListSelectionListener (as an inner class):
public class CancerSelectionListener implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent e) {
Integer selection = (Integer)(((JList) e.getSource()).getSelectedIndex());
if(selection == 0) {
System.out.println("blah"); // works
// switch to the corresponding JPanel in CardLayout
}
}
}
String[] tester;
String teste;
listModel = new DefaultListModel();
for(int i = 0; i < 36; i++) {
tester = _controller.readCancer(i); // reads from the file, this part works!
teste = tester[0];
listModel.addElement(teste);
}
cancerList = new JList(listModel);
cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cancerList.setSelectedIndex(-1);
cancerList.setVisibleRowCount(5);
cancerListScroller = new JScrollPane(cancerList);
CardLayout myCardLayout;
myCardLayout = new CardLayout();
mainPanel2.setLayout(myCardLayout);
myCardLayout.show(mainPanel2, "test");
CancerPanels.aplPanel apl = new CancerPanels.aplPanel();
CancerPanels.fcPanels fc = new CancerPanels.fcPanels();
CancerPanels.vhlsPanels vhls = new CancerPanels.vhlsPanels();
CancerPanels.pdgPanels pdg = new CancerPanels.pdgPanels();
CancerPanels.cebpaPanels cebpa = new CancerPanels.cebpaPanels();
mainPanel2.add(apl.aplReturn(), "test");
mainPanel2.add(fc.fcReturn());
mainPanel2.add(vhls.vhlsReturn());
mainPanel2.add(pdg.pdgReturn());
mainPanel2.add(cebpa.cebpaReturn());
// I have 37 JPanels that are placed in the JPanel that uses CardLayout but I didn't post all of them as it would take up lots of space
The data for each JPanel is populated from static inner classes in the CancerPanels class (only showing 1 as each is very long!)
public class CancerPanels extends CancerGUI {
static JPanel cards;
static CancerController _cons = new CancerController();
static String[] cancerData;
static JScrollPane treatmentsScroller = new JScrollPane(txtTreatments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane causesScroller = new JScrollPane(txtCauses, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane symptomsScroller = new JScrollPane(txtSymptoms, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public static class aplPanel extends JPanel {
public JPanel aplReturn() {
treatmentsScroller.setViewportView(txtTreatments);
txtTreatments.setEditable(false);
causesScroller.setViewportView(txtCauses);
txtCauses.setEditable(false);
symptomsScroller.setViewportView(txtSymptoms);
txtSymptoms.setEditable(false);
cards = new JPanel(new GridLayout(6,1));
cancerData = _cons.readCancer(0);
resultName.setText(cancerData[0]);
txtSymptoms.setText(cancerData[1]);
txtCauses.setText(cancerData[2]);
txtTreatments.setText(cancerData[3]);
resultRate.setText(cancerData[4]);
resultPrognosis.setText(cancerData[5]);
cards.add(resultName);
cards.add(symptomsScroller);
cards.add(causesScroller);
cards.add(treatmentsScroller);
cards.add(resultRate);
cards.add(resultPrognosis);
return cards;
}
}
Edit:
Here is my most recent attempt. I can scroll through the JList but it doesn't properly display the correct corresponding JPanel (in fact it doesn't display anything, except whenever I click the last button, I don't know why that button works). I successfully managed to place an ItemListener on a JComboBox but ultimately, I want the CardLayout to work. Our instructor provided us with sample code to use but when I try it, the JPanels do not switch (or if they do they're hidden, not sure why).
Each of my listeners are public inner classes in the overall CancerGUI class.
public CancerGUI() {
CancerPanels.aplPanel apl = new CancerPanels.aplPanel();
CancerPanels.fcPanels fc = new CancerPanels.fcPanels();
CancerPanels.vhlsPanels vhls = new CancerPanels.vhlsPanels();
// more than 30 JPanels that I add to the JPanel that uses CardLayout, so I only posted 3
// each of them uses the GridLayout
mainPanel2 = new JPanel(new CardLayout());
mainPanel2.add(apl.aplReturn(), "1");
mainPanel2.add(fc.fcReturn(), "2");
mainPanel2.add(vhls.vhlsReturn(), "3");
CancerActionButtons _cab = new CancerActionButtons();
btnNext = new JButton("Next");
btnPrevious = new JButton("Previous");
btnFirst = new JButton("First");
btnLast = new JButton("Last");
btnClear = new JButton("Clear");
btnNext.addActionListener(_cab);
btnPrevious.addActionListener(_cab);
btnFirst.addActionListener(_cab);
btnLast.addActionListener(_cab);
CancerItemListener _item = new CancerItemListener(); // this listener works!
renalC.addItemListener(_item);
skinC.addItemListener(_item);
brainC.addItemListener(_item);
bladderC.addItemListener(_item);
ovarianC.addItemListener(_item);
pancC.addItemListener(_item);
breastC.addItemListener(_item);
String[] tester;
String teste;
listModel = new DefaultListModel();
for(int i = 0; i < 36; i++) {
tester = _controller.readCancer(i);
teste = tester[0];
listModel.addElement(teste);
}
cancerList = new JList(listModel);
cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cancerList.setSelectedIndex(-1);
cancerList.setVisibleRowCount(5);
cancerListScroller = new JScrollPane(cancerList);
ListSelection _list = new ListSelection();
cancerList.addListSelectionListener(_list);
JScrollPane treatmentsScroller = new JScrollPane(txtTreatments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
treatmentsScroller.setViewportView(txtTreatments);
JScrollPane causesScroller = new JScrollPane(txtCauses, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
causesScroller.setViewportView(txtCauses);
JScrollPane symptomsScroller = new JScrollPane(txtSymptoms, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
symptomsScroller.setViewportView(txtSymptoms);
public class ListSelection implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent e) {
String selection = (String)(((JList)e.getSource()).getSelectedValue());
((CardLayout) mainPanel2.getLayout()).show(mainPanel2, selection);
((CardLayout) mainPanel2.getLayout()).show(mainPanel2, selection);
}
}
public class CancerActionButtons implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "First":
((CardLayout) mainPanel2.getLayout()).first(mainPanel2);
cancerCount = 1;
break;
case "Last":
((CardLayout) mainPanel2.getLayout()).last(mainPanel2);
cancerCount = 11;
break;
case "Previous":
((CardLayout) mainPanel2.getLayout()).previous(mainPanel2);
cancerCount--;
cancerCount = cancerCount < 1 ? 11 : cancerCount;
break;
case "Next":
((CardLayout) mainPanel2.getLayout()).next(mainPanel2);
cancerCount++;
cancerCount = cancerCount > 11 ? 1 : cancerCount; //
break;
}
cancerList.setSelectedIndex(cancerCount-1);
}
}
/**
* Inner class that responds to any user interaction with a JComboBox for
* general types of cancers.
*/
public class CancerItemListener implements ItemListener {
#Override
public void itemStateChanged(ItemEvent e) {
JPanel showPanel = new JPanel();
if(e.getStateChange() == ItemEvent.SELECTED) {
String selection = (String) e.getItem();
if(selection.equalsIgnoreCase("skin cancer")) {
CancerPanels.skin skin = new CancerPanels.skin();
showPanel = skin.skinReturn();
} else if (selection.equalsIgnoreCase("bladder cancer")) {
CancerPanels.bladder bladder = new CancerPanels.bladder();
showPanel = bladder.bladderReturn();
} else if (selection.equalsIgnoreCase("pancreatic cancer")) {
CancerPanels.pancreatic pancreatic = new CancerPanels.pancreatic();
showPanel = pancreatic.returnPancreatic();
} else if (selection.equalsIgnoreCase("renal cancer")) {
CancerPanels.renal renal = new CancerPanels.renal();
showPanel = renal.returnRenal();
} else if (selection.equalsIgnoreCase("ovarian cancer")) {
CancerPanels.ovarian ovarian = new CancerPanels.ovarian();
showPanel = ovarian.ovarianReturn();
} else if (selection.equalsIgnoreCase("breast cancer")) {
CancerPanels.breast breast = new CancerPanels.breast();
showPanel = breast.returnBreast();
} else if (selection.equalsIgnoreCase("brain cancer")) {
CancerPanels.brain brain = new CancerPanels.brain();
showPanel = brain.returnBrain();
} else if (selection.equalsIgnoreCase("von hippel-lindau syndrome")) {
CancerPanels.vhlsPanels vhls = new CancerPanels.vhlsPanels();
showPanel = vhls.vhlsReturn();
}
JOptionPane.showMessageDialog(null, showPanel);
}
}
}
Seperate class where the JPanels are made before being added to CardLayout:
public class CancerPanels extends CancerGUI {
static String name;
static JPanel cards;
static CancerController _cons = new CancerController();
static String[] cancerData;
static JScrollPane treatmentsScroller = new JScrollPane(txtTreatments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane causesScroller = new JScrollPane(txtCauses, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane symptomsScroller = new JScrollPane(txtSymptoms, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public static class aplPanel extends JPanel {
public JPanel aplReturn() {
treatmentsScroller.setViewportView(txtTreatments);
txtTreatments.setEditable(false);
causesScroller.setViewportView(txtCauses);
txtCauses.setEditable(false);
symptomsScroller.setViewportView(txtSymptoms);
txtSymptoms.setEditable(false);
cards = new JPanel(new GridLayout(6,1));
cancerData = _cons.readCancer(0);
resultName.setText(cancerData[0]);
txtSymptoms.setText(cancerData[1]);
txtCauses.setText(cancerData[2]);
txtTreatments.setText(cancerData[3]);
resultRate.setText(cancerData[4]);
resultPrognosis.setText(cancerData[5]);
cards.add(resultName);
cards.add(symptomsScroller);
cards.add(causesScroller);
cards.add(treatmentsScroller);
cards.add(resultRate);
cards.add(resultPrognosis);
return cards;
}
In essence what you are trying to do is to change the state of one class from another.
How this is done with Swing GUI's is no different for how it is done for non-GUI programs: one class calls the public methods of another class.
One key is to have wiring to allow this to occur which means references for one class needs to be available to the other class so that appropriate methods can be called on appropriate references. The devil as they say is in the details.
"1) How do I get the JPanels to switch using CardLayout?" -- So the class that holds the CardLayout could for instance have the public methods, next(), previous(), and perhaps show(SOME_STRING_CONSTANT) or some other swapView(...) method.
"2) How do I get the data to be displayed in the GUI in text fields when a user clicks an item from the JList?" -- This will involve the use of listeners -- the class holding the JTextFields will listen for notification from the class that holds the JList, and when notified gets the necessary information from the list-displaying class. A PropertyChangeListener could work well here.
e.g.,
public class CancerSelectionListener implements ListSelectionListener {
private CardDisplayingView cardDisplayingView = null;
public CancerSelectionListener(CardDisplayingView cardDisplayingView) {
this.cardDisplayingView = cardDisplayingView;
}
#Override
public void valueChanged(ListSelectionEvent e) {
int selection = ((JList) e.getSource()).getSelectedIndex();
if(selection == 0) {
if (cardDisplayingView != null) {
cardDisplayingView.swapView(...);
}
}
}
}

Creating an actionlistener from a method in another class

Hi well my code so far does something like this: Click a button, opens a combobox. I want to select an option on the ComboBox and depending on which option is picked i want to open another combobox using getSelectIndex().
Here are parts of my code which are relevant. I know I have to make the other comboboxes not visible or removed but at the moment I'm just trying to make a combobox appear. As you can see i have inserted the actionlistener for the button which works and opens the combobox.however when selecting a string in the combobox no event occurs. However when I run it, no comboboxes appear.
public class Work extends JFrame {
// variables for JPanel
private JPanel buttonPanel;
private JButton timeButton;
public Work()
{
setLayout(new BorderLayout());
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.setPreferredSize(new Dimension(400, 500));
add(buttonPanel,BorderLayout.WEST);
timeButton = new JButton("Time");
buttonPanel.add(timeButton);
buttontime clickTime = new buttontime(); // event created when time button is clicked
timeButton.addActionListener(clickTime);
Time timeObject = new Time();
timeObject.SelectTime();
buttontime2 selectDest = new buttontime2();
timeObject.getAirportBox().addActionListener(selectDest);
}
public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox
public void actionPerformed(ActionEvent clickTime) {
Time timeObject = new Time();
timeObject.SelectTime();
add(timeObject.getTimePanel(),BorderLayout.EAST);
timeObject.getTimePanel().setVisible(true);
timeObject.getTimePanel().revalidate() ;
timeObject.getAirportBox().setVisible(true);
}
}
public class buttontime2 implements ActionListener{
public void actionPerformed(ActionEvent selectDest) {
Time timeObject = new Time();
timeObject.SelectTime();
if(timeObject.getAirportBox().getSelectedIndex() == 1) {
timeObject.getEastMidBox().setVisible(true);
}
else if(timeObject.getAirportBox().getSelectedIndex() == 2) {
timeObject.getBirmBox().setVisible(true);
}
else if(timeObject.getAirportBox().getSelectedIndex() == 3) {
timeObject.getMancbox().setVisible(true);
}
else if(timeObject.getAirportBox().getSelectedIndex() == 4) {
timeObject.getHeathBox().setVisible(true);
}
}
}
public static void main (String args[]) {
events mainmenu = new events(); //object is created
mainmenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainmenu.setSize(800,500);
mainmenu.setVisible(true);
mainmenu.setLayout(new BorderLayout());
mainmenu.setTitle("Learning how to use GUI");
mainmenu.setBackground(Color.BLUE);
mainmenu.setResizable(false);
}
}
my other class TIME
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Time
{
private JComboBox timeAirportbox;//comboboxes declared
private JComboBox eastMidbox;
private JComboBox mancBox;
private JComboBox heathBox;
private JComboBox birmBox;
private String[] airport = {"","EM", "Bham", "Manc", "Heath"};//array of airports declared
private String[] destination = {"","NY", "Cali", "FlO", "MIAMI", "Tokyo"};//array of destinations declared
private JPanel timePanel;
public void SelectTime() {
//combobox objects created
timePanel = new JPanel();
timePanel.setBackground(Color.BLUE);
timePanel.setPreferredSize(new Dimension(400, 400));
timeAirportbox = new JComboBox(airport);//array is inserted into the JComboBox
timePanel.add(timeAirportbox);
timeAirportbox.setVisible(false);
eastMidbox = new JComboBox(destination);
timePanel.add(eastMidbox);
eastMidbox.setVisible(false);
mancBox = new JComboBox(destination);
timePanel.add(mancBox);
mancBox.setVisible(false);
heathBox = new JComboBox(destination);
timePanel.add(heathBox);
heathBox.setVisible(false);
birmBox = new JComboBox(destination);
timePanel.add(birmBox);
birmBox.setVisible(false);
}
public JPanel getTimePanel() {
return timePanel;
}
public JComboBox getAirportBox() {
return timeAirportbox;
}
public JComboBox getEastMidBox() {
return eastMidbox;
}
public JComboBox getMancbox() {
return mancBox;
}
public JComboBox getHeathBox() {
return heathBox;
}
public JComboBox getBirmBox() {
return birmBox;
}
}
The Time object that is built in Work constructor is not used:
Time timeObject = new Time();
timeObject.SelectTime();
buttontime2 selectDest = new buttontime2();
timeObject.getAirportBox().addActionListener(selectDest);
As you are only applying the action listener selectedDest to the combobox of that timeObject, which is not used, then the listener will never be called.
You can do two things to make it work:
Move the code that creates the listener and assign it to the combox in the first listener buttontime
Create the Time object only once and store it as a member of your Work instance. As your listener is a non-static inner classes of the Work class, it will be able to use it instead of creating a new Time object.
Edit: I didn't see that in your second listener, you were AGAIN building a new Time object. This object is really a different one than the one you have created earlier, so modifying one will not affect the other. You really should create the Time object once and store it as a member variable of your Work class, and then use this object in your listeners instead of recreating it.
To be clear, do it like this:
public class Work extends JFrame {
// ...
private Time timeObject;
public Work() {
// ...
timeObject = new Time();
timeObject.SelectTime();
buttontime2 selectDest = new buttontime2();
timeObject.getAirportBox().addActionListener(selectDest);
}
public class buttontime implements ActionListener {
public void actionPerformed(ActionEvent clickTime) {
// use timeObject, don't create it and don't call SelectTime()
// example:
add(timeObject.getTimePanel(),BorderLayout.EAST);
// ....
}
}
public class buttontime2 implements ActionListener {
public void actionPerformed(ActionEvent clickTime) {
// use timeObject, don't create it and don't call SelectTime()
}
}
}
Also to note:
You should not extends JFrame, there is no reason to do so. Refactor your code so that your frame is just a member variable of your Work class.
Follow Java standard code conventions, especially use case properly with class names: buttonlistener should be ButtonListener, and method should start with lowercase: SelectTime should be selectTime.

return an ArrayList to swing with logger interface

I'm using the code in this answer to provide logging functionality, and it works well! Thanks gustafc.
Sending messages to a swing JTextArea from different places
I want to use this approach and add the function: writeEntry(String, String) to write to an ArrayList that I can hopefully access from swing; i.e, click 'show' and have it write this list of word pairs to a JText element.
so adding a bit to gustafc's code.
the interface:
package com.example.logging;
public interface SimpleActivityLogger {
void logAction(String message);
void writeEntry(String s1); //*** I added this ***
}
passing it to another class as per gustafc's example:
public class SimpleComponentLogger implements SimpleActivityLogger{
private JTextComponent target;
private ArrayList data;
public SimpleComponentLogger(JTextComponent target, ArrayList data){
this.target = target;
this.data = data;
}
public void logAction(final String message){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
target.setText(String.format("%s%s%n", new Object[]{target.getText(), message}));
}
});
}
public void writeEntry(String s1){
data.add(s1);
System.out.println("data array length: " + data.size());
}
}
and then making an implementation:
// this originally extended NotesThread,
// but I assume you won't have that on your system
public class LookupIterator3 extends Thread {
private SimpleActivityLogger logger;
// Constructor that passes logger instance
public LookupIterator3(SimpleActivityLogger logger){
this.logger = logger;
}
public void doLookup(){
this.start();
}
public void run() {
String[] words = {"the", "quick", "smart", "fox", "jumps", "over", "the", "lazy", "dog"};
for(int i=0; i<words.length; i++){
synchronized(words){
try{
logger.logAction(words[i]);
words.wait(500);
}
catch(InterruptedException ie){ie.printStackTrace();}
}
logger.writeEntry(words[i]);
}
}
}
however, when I try to access the ArrayList from swing, it comes back size()=0.
Here's a big chunk of swing, but you should be able to just copy and paste it:
public class MySwingTest extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField filename = new JTextField();
private JTextField dir = new JTextField();
private JTextPane output, answersPane;
private JScrollPane scroller;
private SimpleComponentLogger logger;
private ArrayList answer;
public MySwingTest() {
JMenu fileMenu;
JMenuBar menuBar;
JMenuItem menuOpen, menuExit;
JButton answerButton = new JButton("show answers");
answerButton.addActionListener(new MyListener());
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
menuOpen = new JMenuItem("open file");
menuExit = new JMenuItem("exit");
answer = new ArrayList();
output = new JTextPane();
logger = new SimpleComponentLogger(output, answer);
scroller = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
output.setEditable(false);
answer = new ArrayList();
answersPane = new JTextPane();
answersPane.setSize(100, 200);
answersPane.setEditable(false);
fileMenu.add(menuOpen);
fileMenu.add(menuExit);
menuOpen.addActionListener(new MyListener());
menuExit.addActionListener(new MyListener());
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(filename);
p.add(dir);
Container cp = getContentPane();
cp.add(menuBar,BorderLayout.NORTH);
cp.add(p);
cp.add(scroller, BorderLayout.CENTER);
cp.add(answersPane, BorderLayout.WEST);
cp.add(answerButton, BorderLayout.SOUTH);
dir.setEditable(false);
filename.setEditable(false);
}
// Inner class listener
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String eventCommand = e.getActionCommand();
if(eventCommand.equals("exit")){
System.exit(0);
}
if(eventCommand.equals("show answers")){
String entry = (String)answer.get(0);
answersPane.setText(entry);
}
else {
LookupIterator3 lu3 = new LookupIterator3(logger);
lu3.doLookup();
}
}
}
public static void main(String[] args) {
run(new MySwingTest(), 450, 600);
}
public static void run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
}
Sorry about the long question, but this is doing my head in!
Any suggestions?
(I know I'm going to get ragged for writing a long qn, but I don't know what I could have left out)
OK. long edit complete.
Open that baby up and select "open file" from the menu. It will run without a filechooser.
The problem is actually quite simple: in your MySwingTest constructor you have twice the line :
answer = new ArrayList();
once before you create your logger and once after. Simply drop the second call and it should work fine.
Note: it is really a bad practice to share ArrayList between your instances (responsability is spread over several classes). It would be a lot simpler if you only had your logger creating directly its own ArrayList and providing access to it with a getter. Even better, it could directly offer an access to the objects it contains (preventing the possibility for other instances to modify the content of the ArrayList).
Note 2: consider using interface-declaration over class-declaration: use List instead of ArrayList in order to reduce coupling.
Note 3: I would strongly encourage you tu type your collections: List<String> answer = new ArrayList<String>();

Categories