I have GUI screen which lets you set the privacy of a Contact from a selection being made through RadioButton. Although I can add the selection to the database like this...
private void addContactButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
ContactDAO cDao = new ContactDAO();
final ContactDTO cdto = new ContactDTO();
String privacy = "";
String alumni = "";
if (all.isSelected()) {
privacy = all.getText();
}
if (bio.isSelected()) {
privacy = bio.getText();
}
if (none.isSelected()) {
privacy = none.getText();
}
if (yes.isSelected()) {
alumni = yes.getText();
}
if (no.isSelected()) {
alumni = no.getText();
}
cdto.setAlumni(alumni);
cdto.setStatus(privacy);
cDao.add(cdto);
}
I am stuck on retrieving the previously selected item for edit mode. Each radiobutton option belong to a buttongroup.
private void editContact() {
txtID1.setText(String.valueOf(cDTO.getID()));
txtTitle1.setText(cDTO.getTitle());
txtFn1.setText(cDTO.getForename());
txtSn1.setText(cDTO.getSurname());
//get status from cDTO.getStaus and adjust appropriately to the radio button
}
in the above method I would like to set the selected item of the radio button. Just as you would do getSelectedItem() for JComboBox, I am trying to achieve the same for radio button. note cDTO contains the data string, cDTO.getStatus which gets the value from database. But how do I set it to the 3 radio buttons that i have, named allButton bioButton noneButton
Assuming that cDTO.getStatus() returns a String that matches a radio button's name: For each button in the ButtonGroup, b, do something like this:
b.setSelected(cDTO.getStatus().equals(b.getText()));
Related
I want to update JComboBox when I type in the JComboBox editor. It will get the text of JComboBox editor in real time and add some items based on the text. I have tried like this:
final JTextField tfListText = (JTextField) comboBox1.getEditor().getEditorComponent();
tfListText.addCaretListener(new CaretListener() {
private String lastText;
#Override
public void caretUpdate(CaretEvent e) {
String text = tfListText.getText();
if (!text.equals(lastText)) {
lastText = text;
comboBox1.removeAllItems();
// get some items based on text
comboBox1.addItem(someItems1);
...
}
}
});
However, the selected item of JComboBox was changed when using method removeAllItems() or addItem(), and it caused an endless loop triggering the CaretListener again.
So how to update JComboBox without selected item being changed, or is there any other way to add items based on the input content in real time
I have a combo viewer based on a CCombo:
public static ComboViewer createComboViewer(Composite parent) {
CCombo combo = new CCombo(parent, SWT.BORDER);
combo.setTextLimit(2);
combo.addVerifyListener(new UpperCaseKeyListener());
ComboViewer viewer = new ComboViewer(combo);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setLabelProvider(new CustomLabelProvider());
String[] strings = {"AB","CD","EF","GH","IJ"};
viewer.getCCombo().addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent keyEvent) {
String key = viewer.getCCombo().getText();
System.out.println(key);
String[] items = viewer.getCCombo().getItems();
if (!key.equals("") && key.length()==2) {
for (int i=0;i<strings.length;i++) {
if (strings[i].contains(key)) {
final ISelection selection = new StructuredSelection(strings[i]);
viewer.setSelection(selection);
}
}
}
}
});
I have a list of strings : {"AB","CD","EF","GH","IJ"} in this combo viewer.
When I type for example "AB" my item is selected from the drop-down list , but it is not highlighted with blue.
How can I make this happen?
I want that when I type an item in the combo and it is found in the list , to be highlighted with blue when I open the drop down list.
You must call setInput on the viewer to tell it about your content:
String[] strings = {"AB","CD","EF","GH","IJ"};
viewer.setInput(string);
In general a viewer requires both the content provider and label provider to be set and then setInput to be called.
i am making an App where i have to save dynamically created Android.Widget.Button-Objects and its Attributes, like ID, when the App is closed and opened again.
These buttons are saved in an ArrayList.
My idea was to convert my Button-Objects into JSON and save them in the SharedPreference.
My Problem now is that i cant convert the Buttons into JSON, i am using following code for this, if found on stackoverflow:
(For Tryouts i am using a new Button-Object)
Button btn = new Button(this);
Gson gson = new Gson();
String json = gson.toJson(btn);
Its working with String-Object or Integer-Object but not with Button-Object.
Can someone help me?
If you create your buttons dynamically it means you probably set a color, a text, ... to them.
So when you want to save them you only need to know how many buttons you had and what custom attributes you've set to each of them.
So you can do something like that:
You manage 2 lists, one with the buttons and one with their custom attributes.
To make it easier you can use a custom ButtonBuilder to manage the attributes.
Each time you want a new button, you create a new ButtonBuilder, you set the attributes, you generate the button and you store both the builder AND the button in 2 separated lists. Then you can store the list of builders in the SharedPrefs and regenerate the buttons from this list next time you open the app.
List<ButtonBuilder> mBuilders = new ArrayList<>();
List<Button> mButtons = new ArrayList<>();
/**
* Display a new button
*/
public void addButton(/* List of parameters*/) {
ButtonBuilder builder = new ButtonBuilder()
.setBgColor(myColor)
.setText(myText);
Button button = builder.build(context);
mBuilders.add(builder);
mButtons.add(button);
// ... Display the button
}
/**
* Call this method when you need to regenerate the buttons
*/
public void regenerateButtonsOnStart() {
// Get from shared preferences
mBuilders = getBuildersFromSharedPrefs();
Button btn;
for (ButtonBuilder builder : mBuilders) {
btn = builder.build(context);
mButtons.add(btn);
// ... Display the button
}
}
/**
* Custom button builder
*/
public class ButtonBuilder {
private int mBgColor;
private String mText;
// ... whatever you want
public ButtonBuilder() {
}
public ButtonBuilder setBgColor(int bgColor) {
this.mBgColor = bgColor;
return this;
}
public ButtonBuilder setText(String text) {
this.mText = text;
return this;
}
public Button build(Context context) {
Button btn = new Button(context);
btn.setText(mText);
btn.setBackgroundColor(mBgColor);
return btn;
}
}
I have a class that extends BasicTabbedPaneUI and does some paint component overriding.
I want to be able to add a addMouseListener to the class I use it in to check when the user selects a tab the current tab index and the previous tab index.
NOTE: The user is able to navigate to tabs via the keyboard and not just clicking on a tab and I want to be able to make sure the previous index tracks this. So in the example below preIndex would equal the previous index regardless to whether the user navigated to it via the keyboard or mouse.
Any ideas please?
tabbedPane.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
JTabbedPane tabP = (JTabbedPane) e.getSource();
int currIndex = tabP.indexAtLocation(e.getX(), e.getY());
int prevIndex = ?????
}
});
Many thanks!!!!
I would use the change listener instead of mouse listener (it's called in both cases: for mouse and key event triggered tab change). If you cannot determine previously selected tab you can use following approach: save currently selected tab index as client property of the tabbed pane.
private static final String OLD_TAB_INDEX_PROPERTY = "oldTabIdx";
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JTabbedPane tabP = (JTabbedPane) e.getSource();
int currIndex = tabP.getSelectedIndex();
int oldIdx = 0;
Object old = tabP.getClientProperty(OLD_TAB_INDEX_PROPERTY);
if (old instanceof Integer) {
oldIdx = (Integer) old;
}
tabP.putClientProperty(OLD_TAB_INDEX_PROPERTY, currIndex);
// now we can use old and current index
}
});
I want to create listbox.and when user clicks on that i want to display datagrid into dropdown.
private DataGrid objDataGrid;
public CallDataGrid() {
// TODO Auto-generated constructor stub
}
public CallDataGrid(ArrayList<Student> objArrayList) {
System.out.println("Datagrid is now going to set.");
objDataGrid = new DataGrid<Student>();
objLayoutPanel = new SimpleLayoutPanel();
objScrollPanel = new ScrollPanel();
objScrollPanel.add(objDataGrid);
objLayoutPanel.add(objScrollPanel);
objDataGrid.setEmptyTableWidget(new Label("There is no data to display."));
final TextColumn<Student> nameColumn = new TextColumn<Student>() {
#Override
public String getValue(Student object) {
return object.getStrName();
}
};
objDataGrid.addColumn(nameColumn, "User Name");
objDataGrid.setColumnWidth(nameColumn,100,Unit.PX);
final TextColumn<Student> passwordColumn = new TextColumn<Student>() {
#Override
public String getValue(Student object) {
return object.getStrPassword();
}
};
objDataGrid.addColumn(passwordColumn, "Password");
objDataGrid.setColumnWidth(passwordColumn,90,Unit.PX);
objDataGrid.setWidth("190px");
objDataGrid.setHeight("100px");
objDataGrid.setRowData(0, objArrayList);
objDataGrid.setPageSize(5);
// now how can i set this datagrid into Listbox or suggestion box??
No direct way to do what you want. Instead of a ListBox widget you can use a TextBox with a button on the right. When clicking the button put your DataGrid into a PopupPanel and display it by setting the its position on the bottom of the TextBox.