How to set jcombo box to what i want - java

I am doing a home work for my class in java, i am using NetBeans.
When the frame opened i want that my combo box load data that is exactly to my database column.
Exp... On my 7 column on my data base there's a column name that's name Color, and on the list of column there's yellow. I want that my jCombobox load with Yellow and get's all the others color on the model.
Here is my code
private void formWindowOpened(java.awt.event.WindowEvent evt) {
txt_id.setText(user);
SQLiteConnection DB = new SQLiteConnection ();
String question = DB.getQuestionUser();
DB.getUtilisateur(user);
cbx_question.addItem(question);
}
It keep on add item on the list of my model but do not show what is on the database column.
Hope that you will understand

The easiest way to populate a JComboBox is to supply the data when you call the constructor.
There are three different constructors you can call (supplying your data structure):
JComboBox(ComboBoxModel aModel)
Creates a JComboBox that takes its items from an existing ComboBoxModel.
JComboBox(E[] items)
Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector items)
Creates a JComboBox that contains the elements in the specified Vector.
You need to check what return type the database query has and convert it into one of the three previous data structures.
Later, if you wish to use the same JComboBox object to display other data (same type as before) you can do so by calling setModel( ComboBoxModel<E> model )
Source: Oracle documentation page

Related

javafx: sort TableView by default

I'm developing a small game that has a highscore list displayed by a javafx TableView. I've created a subclass HighscoreTableView extends TableView which is a TableView node that automatically creates the TableColumns I need and fills them with data on construction.
I want that table to be sorted by a default column on initialisation. I've added the following code lines:
duration.setSortType(TableColumn.SortType.DESCENDING);
this.getSortOrder().add(duration);
duration.setSortable(true);
this.sort();
where duration is the TableColumn that should define the sorting. Of course, it's added to the TableView. But when I create a new instance of that HighscoreTableView, it remains unsorted by default, until the user clicks on one of the column headers. This is unexpected, since this question, this question and this question say it should work that way. Any ideas?
Further information for reproduction:
The HighscoreTableView class is used by a HighscoreStage extends Stage class, which contains a TabPane with four Tabs. Each Tab contains a HighscoreTableView with different data taken from a static Data object. The data model is a class HighscoreEntry, an ObservableList of them gets added to the HighscoreTableViews. My full code is available here.
You are calling sort() before any data is loaded. Instead you should call it each time after adding/changing data to the table view.
Also you can use SortedList with appropriate comparator to wrap your original backing list. Than all changes will be automatically propagated to view.

Displaying just one value in JComboBox when one is selected

Hello :) I need help with changing a JComboBox in a JTable. I'm new to GUI-Programming and Swing and couldn't solve this problem: I have to change the behavior of a JComboBox.
You can see the ComboBox in the picture below. If "Ja" is selected there should just be "Nein" as an option and the other way around. It would also be cool if "Nein" is set per default. The code was written from one student from last semester and I have difficulties to adjust the combobox like I have to.
That's the code snippet where the ComboBox gets initialized.
optionsInteger = new JComboBox<String>();
optionsInteger.addItem("Ja");
optionsInteger.addItem("Nein");
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
optionsInteger.setSelectedIndex(1);
optionsInteger.setName("optionsInteger");
The ComboBox gets inserted to a JTable in this method:
public void repaintXTable(DefaultTableModel model,JTable table, int xAmount, JScrollPane scrollPane,
JComboBox<String> optionsInteger) {
model.setRowCount(xAmount);
th = table.getTableHeader();
tcm = th.getColumnModel();
tcs = tcm.getColumns();
tcs.nextElement().setHeaderValue("");
tcs.nextElement().setHeaderValue("Lower");
tcs.nextElement().setHeaderValue("Upper");
tc = tcs.nextElement();
tc.setHeaderValue("Integer");
tc.setCellEditor(new DefaultCellEditor(optionsInteger));
for(int i=0;i<xAmount;i++)
{
model.setValueAt("X"+(i+1), i, 0);
}
}
Thank you very much for your help.
In your code, this line
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
sets the default selection to the zeroth element (Ja). This line
optionsInteger.setSelectedIndex(1);
sets the default selection to the first element (Nein).
Either set the selected item or the selected index. There's no need to do both.
A JComboBox does not remove the selected element by default. If the selected element is removed, how would the selected element display in your JTable?
If you really want to do this, you'll have to create your own version of a JComboBox.
. If "Ja" is selected there should just be "Nein" as an option and the other way around.
So then you need two separate ComboBoxModels, one model containing "Nein" and the other containing "Ja". Then when you start to edit the cell you check the current value and use the model containing the other value.
Check out How to add unique JComboBoxes to a column in a JTable (Java). This example shows how you can dynamically change the model at the time the cell is about to be edited.
It would also be cool if "Nein" is set per default.
This has nothing to do with the editor. You just add "Nein" to the TableModel when you add the data to the model.

Is a custom TableCellEditor constructor only called once?

I have a JTable. One column in the JTable is assigned an extended TableCellEditor that displays an extended JComboBox.
There is a fixed list of 100 String objects that populates the comboboxes.
The challenge:
Design the JComboBoxes so that any selection is unique relative to other boxes? That is, if "A" is slected from the combobox in the first row, it is automatically removed from the list of each other combobox.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
The problem:
My comboboxes work beautifully. I can select items at will. I even have made some progress in eliminating already used items from the lists. But I can't figure out how to correctly auto-populate.
I am very confused because it appears that my combobox constructor is only called once when the table is created, not once for each row.
Is this the case? Is the constructor for a TableCellEditor only called once ever? If so, how do I modify the behavior of each combobox as it come into existence?
Thanks for your help!
If you would like specific code, please let me know. I don't know if you want me to paste in the whole classes.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
When you add a new row of data to the TableModel you must add the values of all columns in the row. This should not be a function of the editor. The editor allows you to change values in the cell.
I was able to get around my problem by creating an abstract superclass for my combobox that can be accessed from the tablemodel extension when it sets up its data.

Java Swing : get index of combobox inside table

I need to have combo boxes (JComboBox) in the first column of JTable.
JTable table = new JTable(5,10);
JCheckBox checkbox = new JCheckBox();
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(comboBox));
Now, how can I get selected index of combo box in a certain cell ?
table.getModel().getValueAt(i, 0) returns a String. That's not what I need.
Why don't you use checkbox.getSelectedIndex()? If you declare checkbox in different cells they will all contain a copy of the same. So if you want different comboboxes in different cells you should declare them all with different names. And then you get the selected index like I said before.
The way all this works under the covers is that the JComboBox is treated as a simple delegate editor and the value of the JComboBox is what's kept in the table, not the JComboBox instance (otherwise you'd need an instance per cell). (You can read the gory details in the source)
What this means is that all that's available to you from the JTable is the currently selected value, which will be of type of the parameterized type E in JComboBox<E>, in your case, evidently, a String.
If you want to get the index of a particular value, you need to pull it using the ComboBoxModel<E> backing your JComboBox<E> -- if you've implemented your own ComboBoxModel<E> you'll have to implement your own method, but assuming you've used the default (DefaultComboBoxModel<E>), you can do something like:
int index = ((DefaultComboBoxModel<String>) comboBox.getModel()).getIndexOf(value)

2 column jcombobox

I had quite a lot use for a multicolumn jcombobox, but have not yet found any nor managed to make my own. I have tried several approaches found in web, but they have not worked. Afterwards I read somewhere that those (old) does not work under current Java version.
I have managed to make my own so far, that the combobox has a table as dropdown list and I can select an item with mouse, but the goal is that when the user starts to type into the edit box, drop down list opens and cursor moves based on the text the user has written. It seems that the events from e.g. JTextField editor = (JTextField) comboBox.getEditor().getEditorComponent() does not work.
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
You are looking for autocomplete functionality (as I understand the question): it's supported in SwingX - and quite easy to use.
It boils down to implemneting a custom ObjectToStringConverter and configure the comboBox with an autoCompleteDecorator using that converter. Something like:
/**
* A converter which expects an item of an array type and returns
* a string representation of its first value.
*/
public static class ArrayToStringConverter extends ObjectToStringConverter {
#Override
public String getPreferredStringForItem(Object item) {
if (!(item instanceof Object[])) return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(item);
Object[] array = (Object[]) item;
return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(array[0]);
}
}
// usage
// assuming an model with items being arrays
JComboBox combo = new JComboBox(arrayModel);
// the renderer supporting multiple columns, f.i. a table
combo.setCellRenderer(new TabularListRenderer());
AutoCompleteDecorator.decorate(combo, new ArrayToStringConverter());
A complete working example (including the renderer and showing how-to force the width of the popup to be larger than the combo itself) is TableAsListRenderer in my incubator section
BTW: the autocomplete functionality is a standalone module, accessible via maven or manually downloadable from the maven rep at java.net, you would want the swingx-autocomplete-1.6.4.jar (plus the corresponding doc/sources, if interested)
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
you can to put JTable to the JComboBox, but by default you can to select only value from entire JTables row, not directly from JTables Cell (required additional workaround, not tried yet)
i still searching for this answer too
here's i try so far..
i create Jpopup and put the Jtable in there..
then i use jlabel not jcombobox,
when user click jlabel, then the popup(Jtable) will show in that jlabel location..
when user select value on jtable,
then the popup will dispose,
then the jlabel will show the result..
for your case, you can use jtextfield not jlabel
EDIT :
heres related question
enter link description here

Categories

Resources