Is there a comparable mechanism to the .NET "set DisplayMember" for the JListBox Swing component?
Overriding the toString() method is not sufficient because I also want to be able to change the display member at runtime.
I also looked into implementing my own ListCellRenderer, but found it not very convenient.
Ist there a easier or more elegant approach?
Thanks for your time.
You should create a wrapper class around your business object that overrides toString(). This way you keep your own object clean and can at runtime swap wrappers.
public class MyWrapper()
{
private MyBusinessObject object;
public String toString()
{
return object.getImportantString();
}
}
I came up with a satisfying solution based on implementing a custom ListCellRenderer.
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
public class DynamicCellRenderer implements ListCellRenderer {
private final ListCellRenderer listCellRenderer;
private String displayMember;
public DynamicCellRenderer(String displayMember) {
this(displayMember, new DefaultListCellRenderer());
}
public DynamicCellRenderer(String displayMember, ListCellRenderer wrapped) {
listCellRenderer = wrapped;
this.displayMember = displayMember;
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Object displayMemberValue = getDisplayMemberValue(value);
return listCellRenderer.getListCellRendererComponent(list, displayMemberValue, index, isSelected, cellHasFocus);
}
private Object getDisplayMemberValue(Object value) {
// value is the domain class
// only works if display member is a method, TODO: fallback to field
// displayMember is something like "getName" -> value.getName() gets called
try {
return value.getClass().getMethod(displayMember).invoke(value);
} catch (Exception ex) {
// if anything went wrong it is the programmers fault -> propagate exception
throw new RuntimeException(ex);
}
}
public String getDisplayMember() {
return displayMember;
}
public void setDisplayMember(String displayMember) {
this.displayMember = displayMember;
}
}
In your client GUI code you can do something like this:
jListBox1.setCellRenderer(new DynamicCellRenderer("getName"));
...
...
// and later at some point
((DynamicCellRenderer) jListBox1.getCellRenderer()).setDisplayMember("getEmail");
Related
Just came across a slight issue I'm confused by if someone wouldn't mind having a quick look please.
My ComboBoxes are constructed and initialised as shown below, with me thinking it would be a good idea to keep the data as enums, as the options are all fixed:
private JComboBox<JobTitle> comboBox = new JComboBox<>();
but when adding elements to the DefaultComboBoxModel I've realised that I don't want to display the options as a constant (ie I don't want the options to be displayed in capitals).
So I created my enum like this (see below) in order to call jobTitleModel.addElement(JobTitle.Architect.getName())
But obviously that isn't going to work as the ComboBox is of type enum, not string.
public enum JobTitle {
ARCHITECT("Architect"), TOWN_PLANNER("Town Planner"), URBAN_DESIGNER("Urban Designer"), LANDSCAPE_GARDENER("Landscape Gardener");
private final String name;
private JobTitle(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
So I'm not really sure how to approach this? I could change the ComboBox and model to String, but I think I must be missing something obvious here. Thanks
Use a custom renderer for your JComboBox. For example the code below uses a renderer that extends from the default list cell renderer. All it does is to get the value held in each cell of the JComboBox, a JobTitle object, extracts the name from this object, and displays the name:
JComboBox<JobTitle> jobCombo = new JComboBox<>(JobTitle.values());
jobCombo.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList<?> list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
value = ((JobTitle) value).getName();
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
});
Why do this? Now if the user selects an item from the combo box, it is a full-fledged JobTitle enum object and not a String.
Regarding your comment:
If I wanted to make a method that does the above for a variety of comboBoxes and enums, is a switch the most elegant way to deal with that cast on line 9?
That's a whole new question and probably should be posted separately, but one possible solution is to give all the enums the same interface, something like:
public enum JobTitle implements Textable {
ARCHITECT("Architect"), TOWN_PLANNER("Town Planner"), URBAN_DESIGNER(
"Urban Designer"), LANDSCAPE_GARDENER("Landscape Gardener");
private final String text;
private JobTitle(String name) {
this.text = name;
}
#Override
public String getText() {
return text;
}
}
public interface Textable {
String getText();
}
And then creating my own renderer class:
public class TextableRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
value = ((Textable) value).getText();
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
}
This will work with any enum (or class) that implements the Textable interface.
And then used:
final JComboBox<JobTitle> jobCombo = new JComboBox<>(JobTitle.values());
jobCombo.setRenderer(new TextableRenderer());
I am designing classes based on DAO Pattern.
I have 3 classes and 1 GUI Form.
public interface SchoolYearDao {
List<SchoolYear> getAllSchoolYearInfo();
List<SchoolYear> getAllSchoolYearStart();
List<SchoolYear> getAllSchoolYearEnd();
List<SchoolYear> getSchoolYearById(int aSchoolYearId);
int getSchoolYearId(SchoolYear schoolyear);
boolean addSchoolYear(SchoolYear schoolyear);
}
public class SchoolYear {
//setters and getters
}
public class SchoolYearDaoImpl implements SchoolYearDao{
#Override
public List<SchoolYear> getAllSchoolYearStart() {
List<SchoolYear> listOfSchoolYearStart = new ArrayList<>();
SchoolYear mySchoolYear = new SchoolYear();
String SQL = "{CALL getAllSchoolYearInfo()}";
try(Connection con = DBUtil.getConnection(DBType.MYSQL);
CallableStatement cs = con.prepareCall(SQL);) {
try(ResultSet rs = cs.executeQuery();){
while(rs.next()){
mySchoolYear.setStart(rs.getInt("yearFrom"));
}
listOfSchoolYearStart.add(mySchoolYear);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,e.getMessage());
}
System.out.println(listOfSchoolYearStart);
return listOfSchoolYearStart;
}
}
The problem is with the GUI.
public class SchoolYearGUI extends javax.swing.JPanel {
public SchoolYearGUI() {
initComponents();
schoolYearStartJcbx.setModel(new DefaultComboBoxModel(schoolyear.getAllSchoolYearInfo().toArray());
schoolYearEndJcbx.setModel(new DefaultComboBoxModel(schoolyear.getAllSchoolYearEnd().toArray()));
}
}
I can't get the years to show correctly. I get this.
Instead of the actual integer numbers 2015,2016,2017 and so on...
I research online and found similar problems but most of them were not using a list of class as List<SchoolYear>. In this case, "SchoolYear" is the name of class.
I used toArray(); and tried Arrays.toString(array); but can't get it right.
I thought I'd change the return type to DefaultComboBoxModel of getAllSchoolYearStart() method but I realized I have to keep my List<SchoolYear> as return type in case I need to use the result set as model for JTables etc..
So, I want to just stick with List<SchoolYear> as return type. (If it's a good idea?)
What is the best way to get the actual value?
Thanks in advance.
=============== Solution ==============================
Thanks to MadProgrammer for the advice and to other answerers.
So I studied the listcellrenderer overnight and finally got the basic idea of how to use it.
public MainFrame() {
initComponents();
SchoolYearDaoImpl sy = new SchoolYearDaoImpl();
DefaultComboBoxModel model = new DefaultComboBoxModel(sy.getAllSchoolYearStart().toArray());
jcmbSchoolYearStart.setModel(model);
jcmbSchoolYearStart.setRenderer(new DefaultListCellRenderer() {
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(value instanceof SchoolYear){
SchoolYear schoolyear = (SchoolYear) value;
setText(""+schoolyear.getStart());
}
return this;
}
} );
}
I overridden the getListCellRendererComponent and created an if-statement to test if value is an instance of my class which is "SchoolYear" Then I cast whatever the raw value is to SchoolYear then used the getter of SchoolYear model, getStart() to get the value stored in the list.
I'm now moving the renderer to an external class file in my project.
Exactly as #MadProgrammer said, in new DefaultComboBoxModel(schoolyear.getAllSchoolYearInfo().toArray(), you put in an array of Objects, and the constructor of a JComboBox will try to use the toString() method to convert every instance of SchoolYear to present it as plain text. If you don't overwrite the default toString() method to present it as you like, you will see what you see in the combobox you have now: the class's name with some numbers.
You can implement the toString() method, but it's not the best way. You can construct some utility method, along with getSchoolYearId() you have, to get the ID of every object in the List and fill an array with the IDs.
private int[] getIDAndFillAnArray(List<SchoolYear> syrs) {
int[] ids = new int[syrs.size()];
for (int i=0; i<syrs.size(); i++) {
ids[i] = syrs.get(i).getSchoolYearId();
}
return ids;
}
And just use it like:
schoolYearStartJcbx.setModel(new DefaultComboBoxModel(getIDAndFillAnArray(schoolyear.getAllSchoolYearInfo()));
That's simple enough.
Use this class:
public class ComboItem {
private String value;
private String label;
public ComboItem(String value, String label) {
this.value = value;
this.label = label;
}
public String getValue() {
return this.value;
}
public String getLabel() {
return this.label;
}
#Override
public String toString() {
return label;
}
}
Put this method where you connect to your DB:
public ComboItem[] getListOfSchoolYearStart(params..)
{
List<ComboItem> result = new ArrayList<ComboItem>();
ComboItem[] items;
.....
rs = stmt.executeQuery(query);
while (rs.next()) {
ComboItem item = new ComboItem(rs.getInt("id_school") + "", rs.getString("description"));
result.add(item);
}
items = result.toArray(new ComboItem[result.size()]);
return items;
}
Add your JComboBox:
private ComboItem[] listOfSchoolYearStart;
private int selectedIdSchool=-1;
....
listOfSchoolYearStart= getListOfSchoolYearStart();
JComboBox comboList = new JComboBox(listOfSchoolYearStart);
//If you want to keep previous selection
if (listOfSchoolYearStart.length > 0)
{
boolean isFound=false;
for (ComboItem comb : listOfSchoolYearStart) {
if(Integer.parseInt(comb.getValue())==selectedIdSchool)
{
comboList.setSelectedItem(comb);
isFound=true;
break;
}
}
if(!isFound)
{
comboList.setSelectedIndex(0);
selectedIdSchool=Integer.parseInt(listOfSchoolYearStart[0].getValue());
}
}
This works for me at least, I hope it helps.
I have an object, Supply, that can either be an ElecSupply or GasSupply (see related question).
Regardless of which subclass is being edited, they all have a list of BillingPeriods.
I now need to instantiate N number of BillingPeriodEditors based on the contents of that list, and am pretty baffled as to how I should do it.
I am using GWTP. Here is the code of the SupplyEditor I have just got working:
public class SupplyEditor extends Composite implements ValueAwareEditor<Supply>
{
private static SupplyEditorUiBinder uiBinder = GWT.create(SupplyEditorUiBinder.class);
interface SupplyEditorUiBinder extends UiBinder<Widget, SupplyEditor>
{
}
#Ignore
final ElecSupplyEditor elecSupplyEditor = new ElecSupplyEditor();
#Path("")
final AbstractSubTypeEditor<Supply, ElecSupply, ElecSupplyEditor> elecSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, ElecSupply, ElecSupplyEditor>(
elecSupplyEditor)
{
#Override
public void setValue(final Supply value)
{
setValue(value, value instanceof ElecSupply);
if(!(value instanceof ElecSupply))
{
showGasFields();
}
else
{
showElecFields();
}
}
};
#Ignore
final GasSupplyEditor gasSupplyEditor = new GasSupplyEditor();
#Path("")
final AbstractSubTypeEditor<Supply, GasSupply, GasSupplyEditor> gasSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, GasSupply, GasSupplyEditor>(
gasSupplyEditor)
{
#Override
public void setValue(final Supply value)
{
setValue(value, value instanceof GasSupply);
if(!(value instanceof GasSupply))
{
showElecFields();
}
else
{
showGasFields();
}
}
};
#UiField
Panel elecPanel, gasPanel, unitSection;
public SupplyEditor()
{
initWidget(uiBinder.createAndBindUi(this));
gasPanel.add(gasSupplyEditor);
elecPanel.add(elecSupplyEditor);
}
// functions to show and hide depending on which type...
#Override
public void setValue(Supply value)
{
if(value instanceof ElecSupply)
{
showElecFields();
}
else if(value instanceof GasSupply)
{
showGasFields();
}
else
{
showNeither();
}
}
}
Now, as the list of BillingPeriods is a part of any Supply, I presume the logic for this should be in the SupplyEditor.
I got some really good help on the thread How to access PresenterWidget fields when added dynamically, but that was before I had implemented the Editor Framework at all, so I think the logic is in the wrong places.
Any help greatly appreciated. I can post more code (Presenter and View) but I didn't want to make it too hard to read and all they do is get the Supply from the datastore and call edit() on the View.
I have had a look at some examples of ListEditor but I don't really get it!
You need a ListEditor
It depends of how you want to present them in your actual view, but the same idea apply:
public class BillingPeriodListEditor implements isEditor<ListEditor<BillingPeriod,BillingPeriodEditor>>, HasRequestContext{
private class BillingPeriodEditorSource extends EditorSource<BillingPeriodEditor>{
#Override
public EmailsItemEditor create(final int index) {
// called each time u add or retrive new object on the list
// of the #ManyToOne or #ManyToMany
}
#Override
public void dispose(EmailsItemEditor subEditor) {
// called each time you remove the object from the list
}
#Override
public void setIndex(EmailsItemEditor editor, int index) {
// i would suggest track the index of the subeditor.
}
}
private ListEditor<BillingPeriod, BillingPeriodEditor> listEditor = ListEditor.of(new BillingPeriodEditorSource ());
// on add new one ...
// apply or request factory
// you must implement the HasRequestContext to
// call the create.(Proxy.class)
public void createNewBillingPeriod(){
// create a new one then add to the list
listEditor.getList().add(...)
}
}
public class BillingPeriodEditor implements Editor<BillingPeriod>{
// edit you BillingPeriod object
}
Then in you actual editor edit as is in the path Example getBillingPeriods();
BillingPeriodListEditor billingPeriods = new BillingPeriodListEditor ();
// latter on the clickhandler
billingPeriods.createNewBillingPeriod()
You are done now.
This might be a stupid question, but I'm having trouble thinking this through.
I wrote a method that uses a LinkedList to move through loaded MIDI instruments. I want to make a next and a previous button so that every time you click the button you traverse through the LinkedList.
If I hardcode itr.next(); or itr.previous(); multiple times I can traverse through the LinkedList
public void setInsturment(Synthesizer start,MidiChannel currentChannel[])
{
try
{
start.open();
Soundbank bank = start.getDefaultSoundbank();
start.loadAllInstruments(bank);
LinkedList<Instrument> currentInstrument = new LinkedList<Instrument>();
Instrument instrs[] = start.getLoadedInstruments();
currentInstrument.add(instrs[0]);
currentInstrument.add(instrs[1]);
currentInstrument.add(instrs[2]);
currentInstrument.add(instrs[3]);
currentInstrument.add(instrs[4]);
ListIterator itr = currentInstrument.listIterator();
itr.next();
itr.next();
itr.next();
// nextInstrument();
currentChannel[1].programChange(0,itr.nextIndex());
}
catch(MidiUnavailableException e)
{
System.out.print("error");
}
}
I'm having a lot of trouble making a button that can traverse through the list. Is there an efficient way to do this? I tried something like this with no success.
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == nextButton)
{
sound.nextInstrument();
}
public void nextInstrument()
{
itr.next();
}
Thanks in advance guys!
Well, hmm, a linked list is a List and its items can be accessed by index, this is not the optimal structure to access items by index but I really don't know if you can have a cursor on that kind of collection but you can store the current index on an instance variable.
If you really want random access, then you should consider using ArrayList instead of linked list.
Example:
class NextPrevList {
private int index = 0;
private List currentList; //initialize it with some list
public Object next() {
return list.get(++index);
}
public Object prev() {
//maybe add a check for out of bounds
if (index == 0) return null;
return list.get(--index);
}
}
Personally I think it would be more performant with an ArrayList rather than a LinkedList
The ListIterator#next() method returns the object of interest. If it were my project, I'd assign what is returned from that method to a class field and then notify the GUI of the change.
someInstrument = itr.next();
// fire notify observers method.
Code to the interface: List<Instrument>. This related example navigates a List<ImageIcon>, but the implementation could be changed as required.
MIDI instruments .. next and a previous button
Use an array (e.g. Instrument[]). It might be displayed in a JComboBox, a JList or a JSpinner to allow the user to select an instrument. Here is an example using a combo with renderer.
import java.awt.Component;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.*;
import javax.sound.midi.*;
class InstrumentSelector {
public static void main(String[] args) throws MidiUnavailableException {
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
final Instrument[] orchestra = synthesizer.getAvailableInstruments();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JComboBox orchestraSelector = new JComboBox(orchestra);
orchestraSelector.setRenderer(new InstrumentRenderer());
JOptionPane.showMessageDialog(null, orchestraSelector);
}
});
}
}
class InstrumentRenderer extends BasicComboBoxRenderer {
#Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
if (c instanceof JLabel && value instanceof Instrument) {
JLabel l = (JLabel)c;
Instrument i = (Instrument)value;
l.setText(i.getName());
}
return c;
}
}
I seem not to grasp the concept of Events and such. After reading a while on how to implement the listeners and such I came across the Java tutorial saying I should extend AbstractListModel to get the data event firing. For some reason it still doesn't work.
Is there anything I'm doing wrong?
And what kind of code is expected at addListDataListener(ListDataListener l) for it to work? Since I don't understand that either.
public class CarComboBox extends AbstractListModel<Object> implements ComboBoxModel<Object> {
private JdbcRowSet jdbc;
private int size = 0;
private String selection;
public CarComboBox() {
try {
jdbc = new Query().getCarInfo();
jdbc.beforeFirst();
while (jdbc.next()) {
size++;
}
jdbc.beforeFirst();
}
catch (SQLException ex) {
System.err.println(ex.toString());
}
}
#Override
public void setSelectedItem(Object anItem) {
selection = (String) anItem;
}
#Override
public Object getSelectedItem() {
return selection;
}
#Override
public void addListDataListener(ListDataListener l) {
}
#Override
public void removeListDataListener(ListDataListener l) {
}
#Override
public int getSize() {
return size;
}
#Override
public String getElementAt(int index) {
try {
jdbc.absolute(index + 1);
return jdbc.getString(2);
}
catch (SQLException ex) {
System.out.println(ex.toString());
}
return null;
}
}
And to add a listener to the CarComboBox I do:
CarComboBox ccb = new CarComboBox();
ccb.addListDataListener(new ListDataListener()
I'm guessing that you are using the official tutorial.
However you should not touch ListModel and ComboBoxModel. Those are more advanced features you probably do not need.
The 4 examples in the tutorial do NOT use ListModel and ComboBoxModel.
If you use a standard JComboBox (no ListModel or ComboBoxModel), what happens is that when someone makes a selection, an ActionEvent is fired. This event is magically fired by Swing; you don't have to worry about how it is generated. However what is your responsibility is to have some (zero, one or more) objects being able to receive and do something about the ActionEvent:
public class MyClass implements ActionListener {
JComboBox comboBox = ...;
...
// You must register explicitly every ActionListener that you
// want to receive ActionEvent's from comboBox.
// Here we register this instance of MyClass.
comboBox.addActionListener(this);
...
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JComboBox) {
System.out.println("MyClass registered an ActionEvent from a JComboBox.");
System.out.println("Selected: " +
((JComboBox) e.getSource()).getSelectedItem());
}
}
}
Note that if you don't have any other ActionEvent's fired by different Swing components you
can skip the if (e.getSource() instanceof JComboBox) since you know your ActionEvent always comes from a JComboBox.
In my example the JComboBox is inside MyClass, but it does not have to be:
JComboBox comboBox = ...;
MyClass myClass = ...;
comboBox.addActionListener(myClass);
...
comboBox.addActionListener(someOtherActionListener);
You don't need to override addListDataListener() and removeListDataListener() method. The AbstractListModel already take care of the listeners. Here is the implementation of AbstractListModel.addListDataListener():
public void addListDataListener(ListDataListener l) {
listenerList.add(ListDataListener.class, l);
}
The idea of abstract classes is that they do most of the work for you. Usually you only need to implement abstract methods.
XXListener and XXModel are different sides of the coin: the former is the observer to the latter which is the observable. The listener registers itself to the model when it wants to get notified on changes. It's the responsibility of the model to
manage its listeners (that's typically handled already by the AbstractXXModel, as already explained by #userWhateverNumber ;)
fire the notifications if appropirate: that's the part a custom model must take over, in your case
like
#Override
public void setSelectedItem(Object item) {
selection = item;
fireContentChanged(this, -1, -1);
}
Arguably (there are personal preferences around :-) you often don't need custom model implementations but can just as well re-use the provided DefaultXXModels. In your context and assuming the content of the resultset is immutable it might be an option to fill the default model with the data at construction time, like
DefaultComboBoxModel model = new DefaultComboBoxModel();
forEachRowInResultSet {
model.addElement(resultSet.getString(2));
}
If, on the other hand, the content changes then your model implementation is invalid anyway: the model must notify its listeners whenever something had changed
Object one = model.getElementAt(index);
Object other = model.getElementAt(index)
if (!one.equals(other)) {
listener must have received a contentsChanged
}