coding in Java Eclipse here. Making a booking system. The idea is to take the info from the database ,store it in the ArrayList and from the ArrayList show it in the GUI through JTable. Having some problems with the last part and just can't figure it out..
ArrayList:
import java.util.ArrayList;
public class CarList
{
private ArrayList<Car> cars;
public CarList()
{
cars = new ArrayList<Car>();
}
public int getNumberOfCars()
{
return cars.size();
}
public Car getCar(String CarMake)
{
for (int i = 0; i < cars.size(); i++)
{
if (cars.get(i).getMake() == CarMake)
{
return cars.get(i);
}
}
return null;
}
public int size()
{
return cars.size();
}
public void add(Car car)
{
if (!this.ModelExists(car.getModel()))
{
cars.add(car);
}
}
public Boolean ModelExists(String Model)
{
for (Car c : cars)
{
if (c.getModel().equals(Model))
{
return true;
}
}
return false;
}
public void remove(String CarMake)
{
for (int i = 0; i < cars.size(); i++)
{
if (cars.get(i).getMake() == CarMake)
{
cars.remove(i);
}
}
}
public String toString()
{
String returnStr = "";
for (int i = 0; i < cars.size(); i++)
{
Car temp = cars.get(i);
returnStr += temp + "\n";
}
return returnStr;
}
}
Adapter to get the data from the db to the arraylist:
public CarList getAllCars()
{
MyDatabase myDB = new MyDatabase();
CarList cars = new CarList();
try
{
myDB.openMySQLDatabase("db", "root", "");
String sql = "SELECT Make, Model, LicenseNumber, Color, Year," +
"HorsePower, TimeUntilService, ConsumptionPerKm," +
"NumberOfSeats, NumberOfDoors, Transmission, ClimateControl,Price "
+ "FROM cars";
System.out.println(sql);
Object[][] result = myDB.returnSQLQueryResult(sql);
for (int rows = 0; rows < result.length; rows++)
{
System.out.println("result row");
String make = (String) result[rows][0];
String model = (String) result[rows][1];
String licenseNumber = (String) result[rows][2];
String color = (String) result[rows][3];
int year = (int) result[rows][4];
String horsePower = (String) result[rows][5];
String timeUntilService = (String) result[rows][6];
String consumptionPerKm = (String) result[rows][7];
int numberOfSeats = (int) result[rows][8];
int numberOfDoors = (int) result[rows][9];
String transmission = (String) result[rows][10];
String climateControl = (String) result[rows][11];
int price = (int) result[rows][12];
cars.add(new Car(make, model, licenseNumber, color, year, horsePower,
timeUntilService, consumptionPerKm, climateControl, numberOfSeats, numberOfDoors, transmission, climateControl, price));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
myDB.closeDatabase();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
System.out.println(cars.size());
return cars;
}
JTable:
panelBottomRight = new JPanel();
panelBottomRight.setLayout(new BorderLayout());
panelBottomRight.setBorder(new TitledBorder(BorderFactory
.createLineBorder(Color.black), "[Cars]", 2, 0));
tableBottomRightCenter = new JPanel();
tableBottomRightCenter.setLayout(new BorderLayout());
String[] columnNames = { "Make", "Model", "LicenseNumber", "Color",
"Year", "HorsePower", "TimeUntilService",
"ConsumptionPerKm", "NumberOfSeats", "NumberOfDoors",
"ClimateControl" };
CarList cars= new CarList();
String[][] data = {};
// Create table with database data
tableBottomR = new JTable(data, columnNames);
tableBottomR.setAutoCreateRowSorter(true);
tableBottomR.getTableHeader().setReorderingAllowed(false);
tableBottomR.setModel(new DefaultTableModel(data, columnNames)
{
#Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return false;
}
});
tableBottomRightCenter.add(tableBottomR, BorderLayout.CENTER);
scrollPane2 = new JScrollPane(tableBottomR);
scrollPane2
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tableBottomRightCenter.add(scrollPane2);
panelBottomRight.add(tableBottomRightCenter, BorderLayout.CENTER);
There are a few things that jump out.
In you CarList, the getCar method is comparing object references instead of comparing the contents of the String
For String comparison, you should be using String#equals, for example...
public Car getCar(String CarMake) {
for (int i = 0; i < cars.size(); i++) {
//if (cars.get(i).getMake() == CarMake) {
if (cars.get(i).getMake().equals(CarMake)) {
return cars.get(i);
}
}
return null;
}
You don't seem to be using the getAllCars method to populate the table model, but are simply creating a series of empty table models.
Personally, I'm not a fan of DefaultTableModel, especially given the fact that you have a Car object and CarList object, i would require you to undo all this work to use it, instead, I prefer to create my own, specialised, implementation, which allows me to provide greater control, for example...
public class CarModel extends AbstractTableModel {
private String[] columnNames = { "Make", "Model", "LicenseNumber", "Color",
"Year", "HorsePower", "TimeUntilService",
"ConsumptionPerKm", "NumberOfSeats", "NumberOfDoors",
"ClimateControl" };
private CarList carList;
public CarModel(CarList list) {
carList = list;
}
public CarList getCarList() {
return carList;
}
#Override
public int getRowCount() {
return getCarList().getNumberOfCars();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int column) {
return columnNames[column];
}
#Override
public Class<?> getColumnClass(int columnIndex) {
Class type = String.class;
switch (columnIndex) {
case 0:
type = String.class;
break;
// ...etc...
}
return type;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Car car = getCarList().getCarAt(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = car.getMake();
break;
//...etc...
}
return value;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
This, obviously, will require you to add a getCarAt(int) method to your CarList to return the Car at the given index.
Then, you simply need to extract the data from the database and apply the resulting CarList to the table model, for example...
CarList carList = getAllCars();
CarTableModel model = new CarTableModel(carList);
Then, you just need to add it to your UI, for example...
JTable table = new JTable(model);
add(new JScrollPane(table));
Take a look at How to use tables for more details and examples...
Related
Lang: Java
We are trying to copy the list of pojo to the clipboard in the table format.
Bullet point: what we are trying to achieve here :
1. convert list of pojo into table format
2. If the user copy it in some excel sheet then the it should be copied easily or even if the user try to copy in notepad it should print in the table format.
3. Add some meta data to clipboard to determine the pojo when we will import the table again.
For converting the list of pojo to table format i have used the jtable but i am not able to export all the jtable content to clipboard.
can anyone suggest if i should follow the jtable approach and copy the table to clipboard or any other solution is also available.
Update: as suggested in the comment I tried using the flavours
public class ClipboardTest implements ClipboardOwner {
public static void main(String[] args) {
ClipboardTest clipboardTest = new ClipboardTest();
clipboardTest.copyToClipboard();
//clipboardTest.getFromClipboard();
}
public void copyToClipboard() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Pojo data = new Pojo("1", "2", "ame2", "2", "2");
MyObjectSelection dataSelection = new MyObjectSelection(data);
StringSelection selection = new StringSelection("testing string");
clipboard.setContents(dataSelection, ClipboardTest.this);
System.out.println("copied to clipboard");
}
public void getFromClipboard() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable clipboardContent = clipboard.getContents(this);
DataFlavor[] flavors = clipboardContent.getTransferDataFlavors();
System.out.println("flavors.length = " + flavors.length);
for (int i = 0; i < flavors.length; i++) {
System.out.println("flavor[" + i + "] = " + flavors[i]);
}
}
// ClipboardOwner implementation
#Override
public void lostOwnership(Clipboard clipboard, Transferable transferable) {
System.out.println("ClipboardTest: Lost ownership");
}
}
---
myobjectselection.java
public class MyObjectSelection implements Transferable, ClipboardOwner {
private static DataFlavor dmselFlavor = new DataFlavor(Pojo.class,
"Test data flavor");
private Pojo selection;
public MyObjectSelection(Pojo selection) {
this.selection = selection;
}
// Transferable implementation
#Override
public DataFlavor[] getTransferDataFlavors() {
System.out.println("getTransferDataFlavors");
DataFlavor[] ret = { dmselFlavor };
return ret;
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return dmselFlavor.equals(flavor);
}
#Override
public synchronized Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return this.selection;
} else {
throw new UnsupportedFlavorException(dmselFlavor);
}
}
// ClipboardOwner implementation
#Override
public void lostOwnership(Clipboard clipboard, Transferable transferable) {
System.out.println("MyObjectSelection: Lost ownership");
}
}
--
pojo.java
public class Pojo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String name1;
private String name2;
private String name3;
private String name4;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public String getName3() {
return name3;
}
public void setName3(String name3) {
this.name3 = name3;
}
public String getName4() {
return name4;
}
public void setName4(String name4) {
this.name4 = name4;
}
public Pojo(String name, String name1, String name2, String name3,
String name4) {
super();
this.name = name;
this.name1 = name1;
this.name2 = name2;
this.name3 = name3;
this.name4 = name4;
}
}
When i am trying to copy the string value to clipboard it is working but when i am trying to copy the pojo then is it not working.
For every flavor you want to support, you must provide methods to encode the object in the specified format.
This means you'll likely need to provide an encoder for plain text, html and CVS to cover the basics.
So, based on your Pojo, I wrote a Transferable that supports:
List (of Pojos)
HTML
CVS
Plain text
And serialised (as an additional to List, but it's essentially the same)
PojoTransferable
public class PojoTransferable implements Transferable {
public static final DataFlavor POJO_LIST_DATA_FLAVOR = new DataFlavor(List.class, "application/x-java-pojo-list;class=java.util.List");
public static final DataFlavor HTML_DATA_FLAVOR = new DataFlavor("text/html", "HTML");
public static final DataFlavor CSV_DATA_FLAVOR = new DataFlavor("text/csv", "CVS");
public static final DataFlavor PLAIN_DATA_FLAVOR = new DataFlavor("text/plain", "Plain text");
public static final DataFlavor SERIALIZED_DATA_FLAVOR = new DataFlavor(Pojo.class, "application/x-java-serialized-object; Pojo");
private static String[] HEADERS = new String[]{"name", "name1", "name2", "name3", "name4"};
private static Pojo POJO_HEADER = new Pojo("name", "name1", "name2", "name3", "name4");
private List<Pojo> pojos;
public PojoTransferable(List<Pojo> pojos) {
this.pojos = pojos;
}
#Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{POJO_LIST_DATA_FLAVOR, HTML_DATA_FLAVOR, CSV_DATA_FLAVOR, SERIALIZED_DATA_FLAVOR, PLAIN_DATA_FLAVOR};
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
boolean supported = false;
for (DataFlavor mine : getTransferDataFlavors()) {
if (mine.equals(flavor)) {
supported = true;
break;
}
}
return supported;
}
#Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
Object data = null;
if (POJO_LIST_DATA_FLAVOR.equals(flavor)) {
data = pojos;
} else if (HTML_DATA_FLAVOR.equals(flavor)) {
data = new ByteArrayInputStream(formatAsHTML().getBytes());
} else if (SERIALIZED_DATA_FLAVOR.equals(flavor)) {
data = pojos;
} else if (CSV_DATA_FLAVOR.equals(flavor)) {
data = new ByteArrayInputStream(formatAsCVS().getBytes());
} else if (PLAIN_DATA_FLAVOR.equals(flavor)) {
data = new ByteArrayInputStream(formatAsPlainText().getBytes());
} else {
throw new UnsupportedFlavorException(flavor);
}
return data;
}
protected String formatAsCVS(Pojo pojo) {
StringJoiner sj = new StringJoiner(",");
sj.add(pojo.getName());
sj.add(pojo.getName2());
sj.add(pojo.getName3());
sj.add(pojo.getName4());
return sj.toString();
}
public String formatAsCVS() {
StringBuilder sb = new StringBuilder(128);
sb.append(formatAsCVS(POJO_HEADER));
for (Pojo pojo : pojos) {
sb.append(formatAsCVS(pojo));
}
return "";
}
protected Map<Integer, Integer> columnWidthsFor(Pojo pojo) {
Map<Integer, Integer> columnWidths = new HashMap<>();
columnWidths.put(0, pojo.getName().length());
columnWidths.put(1, pojo.getName1().length());
columnWidths.put(2, pojo.getName2().length());
columnWidths.put(3, pojo.getName3().length());
columnWidths.put(4, pojo.getName4().length());
return columnWidths;
}
protected void apply(Map<Integer, Integer> pojoWidths, Map<Integer, Integer> columnWidths) {
for (int index = 0; index < 5; index++) {
int currentWidth = 2;
if (columnWidths.containsKey(index)) {
currentWidth = columnWidths.get(index);
}
int columnWidth = 2;
if (pojoWidths.containsKey(index)) {
columnWidth = pojoWidths.get(index);
}
columnWidths.put(index, Math.max(currentWidth, columnWidth));
}
}
protected String formatAsPlainText(Pojo pojo, String format) {
return String.format(format, pojo.getName(), pojo.getName1(), pojo.getName2(), pojo.getName3(), pojo.getName4());
}
public static String fill(int padding) {
return String.format("%" + padding + "s", "").replace(" ", "-");
}
public String formatAsPlainText() {
Map<Integer, Integer> columnWidths = new HashMap<>();
apply(columnWidthsFor(POJO_HEADER), columnWidths);
for (Pojo pojo : pojos) {
apply(columnWidthsFor(pojo), columnWidths);
}
StringJoiner sjFormat = new StringJoiner("|");
StringJoiner sjSep = new StringJoiner("+");
for (int index = 0; index < 5; index++) {
int currentWidth = 0;
if (columnWidths.containsKey(index)) {
currentWidth = columnWidths.get(index);
}
sjFormat.add(" %-" + currentWidth + "s ");
sjSep.add(fill(currentWidth + 2));
}
sjFormat.add("%n");
sjSep.add("\n");
String seperator = sjSep.toString();
String format = sjFormat.toString();
StringBuilder sb = new StringBuilder(128);
sb.append(formatAsPlainText(POJO_HEADER, format));
for (Pojo pojo : pojos) {
sb.append(seperator);
sb.append(formatAsPlainText(pojo, format));
}
return sb.toString();
}
public String formatAsHTML() {
StringBuilder sb = new StringBuilder(128);
sb.append("<html><body>");
sb.append("<table border='1'>");
sb.append("<tr>");
for (String header : HEADERS) {
sb.append("<th>").append(header).append("</th>");
}
sb.append("</tr>");
for (Pojo pojo : pojos) {
sb.append("<tr>");
sb.append("<td>").append(pojo.getName()).append("</td>");
sb.append("<td>").append(pojo.getName1()).append("</td>");
sb.append("<td>").append(pojo.getName2()).append("</td>");
sb.append("<td>").append(pojo.getName3()).append("</td>");
sb.append("<td>").append(pojo.getName4()).append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
return sb.toString();
}
}
Test...
Then I wrote a really simple test...
List<Pojo> pojos = new ArrayList<>(25);
pojos.add(new Pojo("one", "two", "three", "four", "five"));
PojoTransferable pt = new PojoTransferable(pojos);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(pt, new ClipboardOwner() {
#Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("Lost");
}
});
try {
Object data = cb.getData(PojoTransferable.POJO_LIST_DATA_FLAVOR);
if (data instanceof List) {
List listOfPojos = (List)data;
System.out.println("listOfPojos contains " + listOfPojos.size());
for (Object o : listOfPojos) {
System.out.println(o);
}
}
} catch (UnsupportedFlavorException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
When I run it, it prints out...
listOfPojos contains 1
test.Pojo#5b480cf9
which tells use that the List flavor worked (for our needs), but then I opened up a text edit, Word and Excel and pasted the contents into them...
nb: Excel used the HTML formatting, go figure
I've run into a very strange issue. I wrote a model for a table and for the most part it works perfect. When I update the row data via the setValueAt function it works and the data in the ArrayList is updated and complete. But when i run the getTotalPay function the same array is empty and i get a null value exception.
I've rewrote the code a number of different ways and tried a couple of different approaches to the problem but the problem persists. I know i've done something wrong i just cannot figure out what the problem is. After search the net and not finding a solution i'm posting this.
Things i've tried are replacing the variable, putting the data into two different variables.
Here is the model
public class WeeklyWagesModel extends AbstractTableModel {
private String defaultValue = "Click to edit";
private String[] headings = { "Date From", "Date To", "Wages", "Pay Date" };
private ArrayList<String[]> theData = new ArrayList<String[]>();
public WeeklyWagesModel() {
theData.add(createDefaultRow());
}
#Override
public int getColumnCount() {
return headings.length;
}
public String getColumnName(int col) {
return headings[col];
}
#Override
public int getRowCount() {
return theData.size();
}
#Override
public Object getValueAt(int row, int col) {
return theData.get(row)[col];
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
theData.get(row)[col] = String.valueOf(value);
if (row == (theData.size() - 1)) {
theData.add(createDefaultRow());
}
fireTableDataChanged();
}
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
public ArrayList<String[]> getData() {
return theData;
}
public String getTotalPay() {
System.out.println("Total Pay: " + theData.size());
BigDecimal total = BigDecimal.ZERO;
for (String[] eachRow : theData) {
if (StringUtils.isNumeric(eachRow[2])) {
total.add(new BigDecimal(eachRow[2]));
}
}
return NumberFormat.getCurrencyInstance().format(total);
}
public String getTotalWeeks() {
return "";
}
private String[] createDefaultRow() {
String[] newRow = new String[headings.length];
for (int i = 0; i < newRow.length; i++) {
newRow[i] = defaultValue;
}
return newRow;
}
}
I want to initialize my model. Here is an extract of my main class where I create my GUI items:
public class IceIHM extends JFrame{
List<String> listInit = new ArrayList<String>() {
{ add("None"); add("None"); add("None"); add("None"); add("None");add("None"); add("None"); }};
String[] tableauObjectTitle = {"Name (OBJECT)","Extended Name","Reference", "Entity Type","Relation", "Extended Relation","PMID"};
String[] tableauSubjectTitle = {"Name (SUBJECT)","Extended Name","Reference", "Entity Type","Relation", "Extended Relation","PMID"};
List<List<String>> dataObject = new ArrayList<List<String>>(){
{ add(listInit); add(listInit);}};
List<List<String>> dataSubject = new ArrayList<List<String>>(){
{ add(listInit); add(listInit);}};
ModelTableau modelObject = new ModelTableau(dataObject,tableauObjectTitle);
ModelTableau modelSubject = new ModelTableau(dataSubject, tableauSubjectTitle);
JTable tableauObject = new JTable(modelObject);
JTable tableauSubject = new JTable(modelSubject);
Here is my class ModelTableau:
public class ModelTableau extends AbstractTableModel{
private List<List<String>> data = new ArrayList<List<String>>();
private String[] headers;;
public ModelTableau(List<List<String>> data, String[] headers){
super();
this.data=data;
this.headers=headers;
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return headers.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex).get(columnIndex);
}
public void removeRow(int row) {
data.remove(row);
fireTableRowsDeleted(row, row);
}
public void addRow(List<String> listData) {
data.add(listData);
fireTableRowsInserted(data.size()-1, data.size()-1);
}
public void setColumnName(int i, String name) {
headers[i] = name;
fireTableStructureChanged();
}
}
Data are well initialized, but not my headers. Can anyone help me to find my mistake?
Override getColumnName(), as shown here, and return headers[col].
#Override
public String getColumnName(int col) {
return headers[col];
}
I have a JTable with a custom JTableModel, now I want to create a custom comparator, so the user can order the all column with my custom method.
I have this code:
ColumnListener
public class ColumnListener extends MouseAdapter {
protected JTable table;
public ColumnListener(JTable t) {
table = t;
}
public void mouseClicked(MouseEvent e) {
TableColumnModel colModel = table.getColumnModel();
int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
int modelIndex = colModel.getColumn(columnModelIndex)
.getModelIndex();
if (modelIndex < 0)
return;
if (sortCol == modelIndex)
isSortAsc = !isSortAsc;
else
sortCol = modelIndex;
for (int i = 0; i < columnsCount; i++) {
TableColumn column = colModel.getColumn(i);
column.setHeaderValue(getColumnName(column.getModelIndex()));
}
table.getTableHeader().repaint();
Collections.sort(v,new MyComparator(isSortAsc,columnModelIndex));
table.tableChanged(new TableModelEvent(MyTableModelSpese.this));
table.repaint();
}
}
MyComparator
class MyComparator implements Comparator {
protected boolean isSortAsc;
protected Integer numeroColonna;
public MyComparator( boolean sortAsc,Integer numberColumn) {
isSortAsc = sortAsc;
numeroColonna = numberColumn;
}
public int compare(Object o1, Object o2) {
if (!(o1 instanceof NotaSpese) || !(o2 instanceof NotaSpese))
return 0;
NotaSpese s1 = (NotaSpese) o1;
NotaSpese s2 = (NotaSpese) o2;
int result = 0;
if(numeroColonna==1){
//ordinamento per data
result = s1.getDataOperazioneData().compareTo(s2.getDataOperazioneData());
}
if (!isSortAsc)
result = -result;
return result;
}
public boolean equals(Object obj) {
if (obj instanceof MyComparator) {
MyComparator compObj = (MyComparator) obj;
return compObj.isSortAsc == isSortAsc;
}
return false;
}
}
When I try to order a column, Data (column ==1) and I see the collection after the order
Collections.sort(v,new MyComparator(isSortAsc,columnModelIndex));
the order of the object in collection v is ok.
But if I see the element in the table are not ordered by my method.
I see this data
2015-01-31
2015-12-30
2014-12-28
How can I fixed it?
I think you can avoid using a custom MouseAdapter if you use a TableRowSorter
TableRowSorter tableRowSorter = new TableRowSorter(tableModel.getModel());
//add your comparator to every column you want. in this case column with index 1
tableRowSorter.setComparator(1, new MyComparator(false));
table.setRowSorter(tableRowSorter);
You could also parametarize your Comparator, so you don't have to cast to NotaSpese
private class MyComparator implements Comparator<NotaSpese> {
protected boolean isSortAsc;
public MyComparator(boolean sortAsc) {
isSortAsc = sortAsc;
}
#Override
public int compare(NotaSpese o1, NotaSpese o2) {
int result = o1.getDataOperazioneData().compareTo(o2.getDataOperazioneData());
if (!isSortAsc) {
result = -result;
}
return result;
}
}
I want my JComboBox to group multiple options together, similar to the HTML optgroup:
<select>
<optgroup label="A">
<option/>
<option/>
</optgroup>
</select>
I could not find any solution for this in Swing. Manipulating the UI-Renderer for the Combobox seems to be a bad idea, as it's OS & L&F-dependent (and they are private so cannot extend).
Consider the following implementation as a basic guide how to apply custom styling and create non-selectable items:
public class ExtendedComboBox extends JComboBox {
public ExtendedComboBox() {
setModel(new ExtendedComboBoxModel());
setRenderer(new ExtendedListCellRenderer());
}
public void addDelimiter(String text) {
this.addItem(new Delimiter(text));
}
private static class ExtendedComboBoxModel extends DefaultComboBoxModel {
#Override
public void setSelectedItem(Object anObject) {
if (!(anObject instanceof Delimiter)) {
super.setSelectedItem(anObject);
} else {
int index = getIndexOf(anObject);
if (index < getSize()) {
setSelectedItem(getElementAt(index+1));
}
}
}
}
private static class ExtendedListCellRenderer
extends DefaultListCellRenderer {
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (!(value instanceof Delimiter)) {
return super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
} else {
JLabel label = new JLabel(value.toString());
Font f = label.getFont();
label.setFont(f.deriveFont(f.getStyle()
| Font.BOLD | Font.ITALIC));
return label;
}
}
}
private static class Delimiter {
private String text;
private Delimiter(String text) {
this.text = text;
}
#Override
public String toString() {
return text.toString();
}
}
}
You can do this in a custom renderer, as discussed in How to Use Combo Boxes: Providing a Custom Renderer.
I don't believe that there is one simple way of doing this, but there is a way to do it.
I would implement a data model class that indicates the grouping that you've describe above. Place instances of those data models in your javax.swing.ComboBoxModel implementation instance.
You can then implement a javax.swing.ListCellRenderer to format the output as you like with indents for the text data. You may just want to extend the javax.swing.DefaultListCellRenderer or possibly borrow its implementation wholesale from the Java source.
As for the L&F you should be able to stay within normal guidelines by using the above methods and you won't have to fight with figuring out how to implement it. Look at the default Swing components they will provide a lot of insight in to how to deal with L&F.
Additionally, I think there are mechanisms (you'll have to forgive me, it's been YEARS since I've done full Swing development) to allow you to determine if an item is selectable or not.
I was wanting this myself today, and I've spent the day figuring it out piecing things together to implement a similar model with a JList rather than with the suggested JComboBox. I've finally come up with a solution using GlazedLists EventList and SeparatorList with the corresponding DefaultEventListModel. I override the CellRenderer and the DefaultListSelectionModel. In the end I posted my own answer to my own question on this: How to prevent selection of SeparatorList.Separator in a JList?
Here's my final working code:
public class MyFrame extends javax.swing.JFrame {
private final EventList<BibleVersion> bibleVersions;
private final SeparatorList<BibleVersion> versionsByLang;
private boolean[] enabledFlags;
public MyFrame(){
bibleVersions = new BasicEventList<>();
bibleVersions.add(new BibleVersion("CEI2008", "Testo della Conferenza Episcopale Italiana", "2008", "Italian"));
bibleVersions.add(new BibleVersion("LUZZI", "Diodati Nuova Riveduta - Luzzi", "1927", "Italian"));
bibleVersions.add(new BibleVersion("NVBSE", "Nova Vulgata - Bibliorum Sacrorum Editio", "1979", "Latin"));
bibleVersions.add(new BibleVersion("NABRE", "New American Bible - Revised Edition", "2011", "English"));
bibleVersions.add(new BibleVersion("KJV", "King James Version", "1611", "English"));
versionsByLang = new SeparatorList<>(bibleVersions, new VersionComparator(),1, 1000);
int listLength = versionsByLang.size();
enabledFlags = new boolean[listLength];
ListIterator itr = versionsByLang.listIterator();
while(itr.hasNext()){
enabledFlags[itr.nextIndex()] = !(itr.next().getClass().getSimpleName().equals("GroupSeparator"));
}
jList = new javax.swing.JList();
jList.setModel(new DefaultEventListModel<>(versionsByLang));
jList.setCellRenderer(new VersionCellRenderer());
jList.setSelectionModel(new DisabledItemSelectionModel());
ListSelectionModel listSelectionModel = jList.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
}
public static class BibleVersion {
private String abbrev;
private String fullname;
private String year;
private String lang;
public BibleVersion(String abbrev, String fullname, String year, String lang) {
this.abbrev = abbrev;
this.fullname = fullname;
this.year = year;
this.lang = lang;
}
public String getAbbrev() {
return abbrev;
}
public void setAbbrev(String abbrev) {
this.abbrev = abbrev;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
#Override
public String toString() {
return this.getAbbrev() + " — " + this.getFullname() + " (" + this.getYear() + ")"; //To change body of generated methods, choose Tools | Templates.
}
}
private static class VersionComparator implements Comparator<BibleVersion> {
#Override
public int compare(BibleVersion o1, BibleVersion o2) {
return o1.getLang().compareTo(o2.getLang());
}
}
private static class VersionCellRenderer extends DefaultListCellRenderer{
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof SeparatorList.Separator) {
SeparatorList.Separator separator = (SeparatorList.Separator) value;
BibleVersion bibleversion = (BibleVersion)separator.getGroup().get(0);
String lbl = "-- " + bibleversion.getLang() + " --";
label.setText(lbl);
label.setFont(label.getFont().deriveFont(Font.BOLD));
label.setBackground(Color.decode("#004400"));
label.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
label.setEnabled(false);
} else {
label.setFont(label.getFont().deriveFont(Font.PLAIN));
label.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
}
return label;
}
}
private class DisabledItemSelectionModel extends DefaultListSelectionModel {
private static final long serialVersionUID = 1L;
#Override
public void setSelectionInterval(int index0, int index1) {
if(index0 < index1){
for (int i = index0; i <= index1; i++){
if(enabledFlags[i]){
super.addSelectionInterval(i, i);
}
}
}
else if(index1 < index0){
for (int i = index1; i <= index0; i++){
if(enabledFlags[i]){
super.addSelectionInterval(i, i);
}
}
}
else if(index0 == index1){
if(enabledFlags[index0]){ super.setSelectionInterval(index0,index0); }
}
}
#Override
public void addSelectionInterval(int index0, int index1) {
if(index0 < index1){
for (int i = index0; i <= index1; i++){
if(enabledFlags[i]){
super.addSelectionInterval(i, i);
}
}
}
else if(index1 < index0){
for (int i = index1; i <= index0; i++){
if(enabledFlags[i]){
super.addSelectionInterval(i, i);
}
}
}
else if(index0 == index1){
if(enabledFlags[index0]){ super.addSelectionInterval(index0,index0); }
}
}
}
private class SharedListSelectionHandler implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
StringBuilder output = new StringBuilder();
int firstIndex = e.getFirstIndex();
int lastIndex = e.getLastIndex();
boolean isAdjusting = e.getValueIsAdjusting();
output.append("Event for indexes ");
output.append(firstIndex);
output.append(" - ");
output.append(lastIndex);
output.append("; isAdjusting is ");
output.append(isAdjusting);
output.append("; selected indexes:");
if (lsm.isSelectionEmpty()) {
output.append(" <none>");
} else {
// Find out which indexes are selected.
int minIndex = lsm.getMinSelectionIndex();
int maxIndex = lsm.getMaxSelectionIndex();
for (int i = minIndex; i <= maxIndex; i++) {
if (lsm.isSelectedIndex(i)) {
output.append(" ");
output.append(i);
}
}
}
output.append(System.getProperty("line.separator"));
System.out.println(output.toString());
}
}
}