This this the class of my dialog box:
public class Question extends DialogBox {
public Question(int row, FlexTable flex) {
super (true);
setText( "Choose Action");
String outputString = flex.getWidget(row, 0).toString();
VerticalPanel panel = new VerticalPanel();
panel.add(new Label ("" + outputString));
panel.add(new Button( "edit"));
this.setWidget(panel);
}
}
And this is the way I create an instance of my box after clicking at some row in flex table, but the problem is that I get nothing after, no impact after clicking any row.
flex.addClickHandler(new ClickHandler() {
public void onClick (ClickEvent event){
int rowlndex = flex.getCellForEvent(event).getRowIndex();
// Window.alŠµrt("Row Clicked " + rowlndex); --- Works Properly
Question que = new Question(rowlndex, flex);
que.center();
que.show();
}
});
Related
I am using 2 different classes: one holding a main JFrame with an edit JButton and one holding an edit JFrame that is called when the button is pressed.
First i select a row from a jtable for edit. After i press Edit button and a Jframe opens. If i press repeatedly the button, the same jframe are openning. So i want, after the first press of the button -> Jframe are openning and if i press again button I do not want to open the same frame again.
Here is a link with app image: https://ibb.co/gYfR9a
Here is my code for the Edit button:
JButton btnEdit = new JButton("Edit");
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < table.getRowCount(); i++) {
Boolean chkDel = Boolean.valueOf(table.getValueAt(i, 0).toString());
if (chkDel) {
String id = table.getValueAt(i, 1).toString();
String num = table.getValueAt(i, 2).toString();
String pre = table.getValueAt(i, 3).toString();
String name = table.getValueAt(i, 4).toString();
String email = table.getValueAt(i, 5).toString();
EditFrame f = new EditFrame(Integer.valueOf(id), num, pre, name, email);
f.initFrame(Integer.valueOf(id), num, pre, name, email);
}
}
}
});
btnEdit.setBounds(150, 250, 90, 23);
getContentPane().add(btnEdit);`
And here is the code for the Edit Frame:
public class EditFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField numField;
private JTextField preField;
private JTextField nameField;
private JTextField emailField;
private final JButton btnEdit = new JButton("Edit");
/**
* Launch the application.
*/
public void initFrame(int id, String num, String pre, String name, String email) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EditFrame eframe = new EditFrame(id, num, pre, name, email);
eframe.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
In your actionPerformed method of your button you should memorize if it was already clicked by using a boolean. You can then do something like if(!wasClickedAlready) { ... }. However the boolean needs to be kept in the correct scope (one above the method). For example as member variable of your ActionListener or in the wrapping class, or something like this. Else the state of the boolean can not be memorized between method-calls.
For example see this snippet:
btnEdit.addActionListener(new ActionListener() {
private boolean wasClicked = false;
#Override
public void actionPerformed(ActionEvent e) {
if (wasClicked) {
// Do nothing if clicked already
return;
} else {
// The button was clicked for the first time
wasClicked = true;
}
for (int i = 0; i < table.getRowCount(); i++) {
// Your stuff
...
}
}
});
If your question refers to not opening a frame for the same table row again, then you need to memorize the rows you have already clicked, for example by using a table of boolean like boolean[] or a Map mapping the row-index to a boolean like HashMap<Integer, Boolean>. However the scheme remains the same. If that is the case, just tell me in the comments and I will show you another snippet.
Edit: You commented that you maximally one frame should be shown for each row, not for the whole table. As stated above you can apply the same scheme than before:
btnEdit.addActionListener(new ActionListener() {
private boolean[] wasClickedTable = new boolean[table.getRowCount()];
#Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < table.getRowCount(); i++) {
Boolean chkDel = Boolean.valueOf(table.getValueAt(i, 0).toString());
// A row should be processed
if (chkDel) {
// Lookup if row was already clicked before
if(wasClickedTable[i]) {
// It was, skip the row and do not process it
continue;
}
// The row was not clicked before
// However it is now, set it
wasClickedTable[i] = true;
// Further process the row
// Your stuff
...
}
}
}
});
I'm not sure if I understood correctly. So you have a button that opens a JFrame, but you don't want the JFrame to be opened every time the button is clicked? What is the expected behavior?
I am currently facing a small issue that goes like this: I have a button which, when clicked, will trigger the display of a JPopupMenu under it ( I grab the location of the button, substract a bit from it's height and display the JPopupMenu). This is all made in NetBeans with the GUI builder.
The JPopupMenu is composed of several JPanels, which contain a JLabel, a Button, a TextField and a CheckBox. Those JPanels, I want them to be first initialized in a "minimized" mode( display only the label), but, when the user clicks on them, to expand and display the other components as well. On a second click, the JPanel will revert to the "minimized" state.
Now, I don't know exactly where the issue is, whether in the way the JPanel is resized or not, but although when I click the button the popup menu is displayed correctly and everything else, if I expand a JPanel inside it, the popup menu does not get resized -> it has the same width/height no matter what I do. I tried to change the Layout Manager used by JPopupMenu to FlowLayout,BoxLayout, but ultimately they had no effect. Now, a few snippets of points of interest in the code:
public class WifiItem extends javax.swing.JPanel {
private final Dimension maxDimension;
private final Dimension minDimension;
private boolean maximized;
public WifiItem() {
initComponents();
setBorder(BorderFactory.createLineBorder(Color.black, 1));
maxDimension = new Dimension(386, 62);
minDimension = new Dimension(386, 32);
maximized = false;
setSize(minDimension);
setPreferredSize(minDimension);
setMinimumSize(minDimension);
setMaximumSize(minDimension);
this.setPreferredSize(minDimension);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (maximized) {
setToMin();
} else {
setToMax();
}
}
});
}
public final void setToMax() {
setSize(maxDimension);
setPreferredSize(maxDimension);
setMinimumSize(maxDimension);
setMaximumSize(maxDimension);
maximized = true;
getParent().revalidate();
}
public final void setToMin() {
setSize(minDimension);
setPreferredSize(minDimension);
setMinimumSize(minDimension);
setMaximumSize(minDimension);
maximized = false;
getParent().revalidate();
}
This is where I build the JPopupMenu and set up the display.
public class WifiConnections {
private static final List<WifiItem> connectionsList = new ArrayList<>();
public static JPopupMenu displayPanel;
public WifiConnections() {
displayPanel = new JPopupMenu();
displayPanel.setLayout(new BoxLayout(displayPanel, BoxLayout.Y_AXIS));
WifiItem newItem = new WifiItem();
newItem.setConnectionId("Connection 1.");
WifiItem newItem2 = new WifiItem();
newItem2.setConnectionId("Connection 2.");
connectionsList.add(newItem);
connectionsList.add(newItem2);
connectionsList.forEach((item) -> {
displayPanel.add(item);
});
}
public void displayPopup(Point p) {
displayPanel.setLocation(p.x, p.y + 34);
displayPanel.setVisible(true);
}
public void hidePopup() {
displayPanel.setVisible(false);
}
}
As you can see, if I expand one item, the other one gets covered and the popup does not resize to fit the JPanels, and I am really at a loss at how to continue.
I want a drop-down compobox where the selected element is one-line when its not selected, but when I choose from the drop-down each cell is taller and shows some extra information
This is achieved by the following code:
import java.awt.Component;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
public class ComboJFrame extends javax.swing.JFrame {
private boolean popup;
private javax.swing.JComboBox jComboBox1;
public ComboJFrame() {
initComponents();
}
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
DefaultComboBoxModel model = new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3",
"Long Long Item 4" });
jComboBox1.setModel(model);
jComboBox1.setRenderer(new DefaultListCellRenderer() {
#Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
System.out.println("rend = " + value);
// if popup is showing we need to make elements tall
if (popup) this.setText("<html>" + value.toString().replaceAll(" ", "<br/>\n"));
return this;
}
;
});
jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
popup = false;
System.out.println("popup = " + popup);
}
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
popup = false;
System.out.println("popup = " + popup);
}
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
popup = true;
jComboBox1.invalidate();
System.out.println("popup = " + popup);
}
});
getContentPane().add(jComboBox1);
pack();
model.setSelectedItem("Item 3"); // THIS GIVES THE PROBLEM
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ComboJFrame().setVisible(true);
}
});
}
}
The problem is what if I programatically select an item, like
model.setSelectedItem("Item 3");
then the JCombobox gets the cell height wrong and displays
This is only happening on when selecting items programatically. If I select an item by manually clicking with the mouse and then opens the popup againd then it goes away.
Ive seen this on Ubuntu Linux 14.04 using Java 7 and Java 8.
I'd like to get it fixed, and to know whether Windows and Mac has the same problem.
Strange thing is, that if I add items to the model in a ways such that setSelectedItem is never called, like:
model.removeAllElements();
model.addElement("Item 3");
model.insertElementAt("Item 2",0);
model.insertElementAt("Item 1",0);
model.addElement("Long Item 4");
then the problem doesen't appear
Another workaround seems to be to select the item and then re-set the model:
model.setSelectedItem("Item 3");
jComboBox1.setModel(new DefaultComboBoxModel());
jComboBox1.setModel(model);
The above is test code, the real application is for Apertium machine translation, and the combobox looks like this:
When I create a table from an array, I fill ithe first column with the values present in the array, with the second column being ckeckBoxes with the values of the index of the array. I would need to retrieve the name of the selected checkBox. Below is my code, could anyone help me please?
Thanks for your help!
public class AddDoodlePart3 extends Composite {
MainView main = new MainView();
FlexTable table= new FlexTable();
VerticalPanel ab = new VerticalPanel();
HorizontalPanel hor = new HorizontalPanel();
InlineLabel lb = new InlineLabel("tette");
CheckBox ck ;
TextBox orario = new TextBox();
Button btn = new Button("Inserisci");
int culo;
public AddDoodlePart3(String det, ArrayList<String> listDate){
initWidget(this.ab);
this.ab.add(lb);
System.out.println(det+listDate.size());
table.setText(0, 0, " ");
table.setText(0, 1, "Opzione");
table.setText(0, 2, " ");
System.out.println("1");
for(int i=0;i<listDate.size();i++){
System.out.println(i);
this.ck = new CheckBox(""+i);
table.setWidget(i, 0, ck);
table.setText(i, 1, listDate.get(i));
ck.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
boolean checked = ((CheckBox) event.getSource()).getValue();
Window.alert("It is " + (checked ? "" : "not ") + "checked "+ culo);
}
});
}
this.ab.add(table);
this.hor.add(orario);
this.hor.add(btn);
this.ab.add(hor);
btn.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// System.out.println(culo);
}
});
}
}
You should use a DataGrid widget for these purposes. See GWT Showcase for an example. Click on a Source Code link to see how to implement this DataGrid.
You can set a SelectionModel on a DataGrid - in your case you need a MultiSelectionModel. Then, getting the list of selected items is as easy as:
selectionModel.getSelectedSet();
I'm creating ContentPanel and hide it:
final ContentPanel infoPanel = new ContentPanel();
final TabPanel infoTabPanel = new TabPanel();
...
//Adding TabItems with some forms
infoPanel.add(infoTabPanel);
infoPanel.hide();
add(infoPanel);
Then in some Event Listener I try to show this hidden panel:
infoPanel.show();
infoPanel.layout();
But this panel is shown without any data. Only if I click on tabs data appears.
So how to hide/show this panel correctly?
EDITED:
I'm using GXT 2.2.4.
I'm creating ContentPanel with TabPanel which contains FormPanel and hide ContentPanel.
Then in Event Listener I try to show this hidden panel, but it is shown without form. Only if I click on tabs form appears.
Here is code:
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
final ContentPanel infoPanel = new ContentPanel();
infoPanel.setAutoHeight(true);
final TabPanel infoTabPanel = new TabPanel();
infoTabPanel.setAutoHeight(true);
final FormPanel testForm = new FormPanel();
FieldSet fieldSet = new FieldSet();
fieldSet.setHeading("Information");
FormLayout fLayout = new FormLayout();
fieldSet.setLayout(fLayout);
LabelField field1 = new LabelField();
LabelField field2 = new LabelField();
field1.setFieldLabel("Field1:");
field1.setName("field1");
fieldSet.add(field1);
field2.setFieldLabel("Field2:");
field2.setName("field2");
fieldSet.add(field2);
testForm.add(fieldSet);
TabItem formTab = new TabItem("Form Tab");
formTab.add(testForm);
infoTabPanel.add(formTab);
TabItem longText = new TabItem("Long Text");
longText.addStyleName("pad-text");
longText.addText("Long Text" + "<br>" + "Long TextLong Text");
infoTabPanel.add(longText);
infoPanel.add(infoTabPanel);
infoPanel.hide();
Button buttonShow = new Button("show");
buttonShow.addSelectionListener(new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
infoPanel.show();
}
});
Button buttonHide = new Button("hide");
buttonHide.addSelectionListener(new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
infoPanel.hide();
}
});
add(infoPanel);
add(buttonShow);
add(buttonHide);
}
Which GXT version do you have? I can't reproduce your problem on GXT 3.x. I wrote a little example code which does exactly what you're asking for.
public void onModuleLoad() {
final ContentPanel infoPanel = new ContentPanel();
final TabPanel infoTabPanel = new TabPanel();
infoTabPanel.add(new TextButton("moo"), "moo");
infoTabPanel.add(new TextButton("boo"), "boo");
infoPanel.add(infoTabPanel);
infoPanel.hide();
VerticalLayoutContainer vlc = new VerticalLayoutContainer();
vlc.setPixelSize(300, 250);
TextButton buttonShow = new TextButton("show");
buttonShow.addSelectHandler(new SelectHandler() {
#Override
public void onSelect(SelectEvent event) {
infoPanel.show();
}
});
TextButton buttonHide = new TextButton("hide");
buttonHide.addSelectHandler(new SelectHandler() {
#Override
public void onSelect(SelectEvent event) {
infoPanel.hide();
}
});
vlc.add(infoPanel, new VerticalLayoutData(1, 1));
vlc.add(buttonShow);
vlc.add(buttonHide);
RootPanel.get().add(vlc);
}
OK I see.
OK I see, since clicking on the tab makes it appear, why don't you programmatically select the tab in your listener?
infoTabPanel.setSelection(formTab);
http://dev.sencha.com/deploy/gxt-2.2.5/docs/api/com/extjs/gxt/ui/client/widget/TabPanel.html#setSelection(com.extjs.gxt.ui.client.widget.TabItem)
---------below didn't work------------
so try infoPanel.repaint();