I am developing an Eclipse plug in and try to put a JComboBox on an IToolBarManager and add ActionListener to it, so I can handle the JComboBox selection.
Can anyone please help me with that?
There may be a better solution, but I have used the following method:
IToolBarManager mgr = this.getViewSite().getActionBars().getToolBarManager();
IContributionItem comboCI = new ControlContribution("test") {
protected Control createControl(Composite parent) {
final Combo c = new Combo(parent, SWT.READ_ONLY);
c.add("one");
c.add("two");
c.add("three");
c.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
c.add("four");
}
});
return c;
}
};
mgr.add(comboCI);
}
Related
JComboBox displays a List on click. Instead of the list, I want to display a JPopupMenu.
In the following code the event is triggered but the popup doesnt show up. Why?
JComboBox box = new JComboBox();
box.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
popupMenu.show(box, 0, box.getHeight());
}
...
});
Alternatively one can use a mouseListener. Due to a JDK-bug
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4144505
one has to add the mouseListener to all Descendants like that:
MouseAdapter comboPopupAdapter = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
popupMenu.show(box, 0, box.getHeight());
}
};
box.addMouseListener(comboPopupAdapter);
for (Component c : box.getComponents()) {
c.addMouseListener(comboPopupAdapter);
}
I'm getting stuck while building a forum like application which has a vote button.
I have vote up and vote down button for each content which are automatically generated. I want this button to only display the up and down arrow but not any text or label.. how can i find out which button is pressed?
Automated content..
ImageIcon upvote = new ImageIcon(getClass().getResource("vote_up.png"));
ImageIcon downvote = new ImageIcon(getClass().getResource("vote_down.png"));
JButton vote_up = new JButton(upvote);
JButton vote_down = new JButton(downvote);
vote_up.addActionListener(voting);
vote_down.addActionListener(voting);
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
//What to do here to find out which button is pressed?
}
};
any help is appreciated.
public void a(){
int crt_cnt = 0;
for(ClassA temp : listofClassA)
{
b(crt_cnt);
crt_cnt++;
}
}
public void b(crt_cnt){
//draw button
}
As from above, I have multiple vote_up and vote_down button created by the b function, how can i differentiate which crt_cnt is the button from?
There are multiple ways you might achieve this
You could...
Simply use the source of the ActionEvent
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == vote_up) {
//...
} else if (...) {
//...
}
}
};
This might be okay if you have a reference to the original buttons
You could...
Assign a actionCommand to each button
JButton vote_up = new JButton(upvote);
vote_up.setActionCommand("vote.up");
JButton vote_down = new JButton(downvote);
vote_down .setActionCommand("vote.down");
//...
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
if ("vote.up".equals(e.getActionCommand())) {
//...
} else if (...) {
//...
}
}
};
You could...
Take full advantage of the Action API and make indiviual, self contained actions for each button...
public class VoteUpAction extends AbstractAction {
public VoteUpAction() {
putValue(SMALL_ICON, new ImageIcon(getClass().getResource("vote_up.png")));
}
#Override
public void actionPerformed(ActionEvent e) {
// Specific action for up vote
}
}
Then you could simply use
JButton vote_up = new JButton(new VoteUpAction());
//...
Which will configure the button according to the properties of the Action and will trigger it's actionPerformed method when the button is triggered. This way, you know 100% what you should/need to do when the actionPerformed method is called, without any doubts.
Have a closer look at How to Use Actions for more details
You can detect by using the method getSource() of your EventAction
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == vote_up ) {
// vote up clicked
} else if (e.getSource() == vote_down){
// vote down clicked
}
}
};
hey thanks for all the help and assistance! I've finally got it! I solved it by
assigning a text on the button, +/- for vote up or down, followed by the content id which i required, then change the font size to 0
vote.setText("+"+thistopic.content.get(crt_cnt).get_id());
vote.setFont(heading.getFont().deriveFont(0.0f));
after that i could easily trace which button is pressed by comparing to the
actionEvent.getActionCommand()
which return the text on the button!
I would wrap the JButton similar to this:
JButton createMyButton(final JPanel panel, final String text,
final boolean upOrDown, final int gridx, final int gridy) {
final JButton button = new JButton();
button.setPreferredSize(new Dimension(80, 50));
final GridBagConstraints gbc = Factories.createGridBagConstraints(gridx,
gridy);
panel.add(button, gbc);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
myActionPerformed(text, upOrDown);
}
});
return button;
}
You could use an int instead of the text, if more convenient.
I want to create a jTree which when i right click on a node, should give me the options of "rename","add Region(parent)","add City(child)".
the name of my jTree is branches
As i am new to swing,Could any one help with code. Thanks in Advance.
Regards, Sarkwa
I recommend you to use setComponentPopupMenu method of JTree with MouseListener. In mouseListener determine Node for menu and generate popupMenu once. I write a simple example which can help you to do your work.
public class Main extends javax.swing.JFrame {
private JTree t;
private DefaultTreeModel model;
private DefaultMutableTreeNode selectedNode;
public Main() {
DefaultMutableTreeNode n = new DefaultMutableTreeNode("test");
n.add(new DefaultMutableTreeNode("test2"));
model = new DefaultTreeModel(n);
t = new JTree(model);
t.setEditable(true);
t.setComponentPopupMenu(getPopUpMenu());
t.addMouseListener(getMouseListener());
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(t);
pack();
setVisible(true);
}
private MouseListener getMouseListener() {
return new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
if(arg0.getButton() == MouseEvent.BUTTON3){
TreePath pathForLocation = t.getPathForLocation(arg0.getPoint().x, arg0.getPoint().y);
if(pathForLocation != null){
selectedNode = (DefaultMutableTreeNode) pathForLocation.getLastPathComponent();
} else{
selectedNode = null;
}
}
super.mousePressed(arg0);
}
};
}
private JPopupMenu getPopUpMenu() {
JPopupMenu menu = new JPopupMenu();
JMenuItem item = new JMenuItem("edit");
item.addActionListener(getEditActionListener());
menu.add(item);
JMenuItem item2 = new JMenuItem("add");
item2.addActionListener(getAddActionListener());
menu.add(item2);
return menu;
}
private ActionListener getAddActionListener() {
return new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if(selectedNode != null){
System.out.println("pressed" + selectedNode);
DefaultMutableTreeNode n = new DefaultMutableTreeNode("added");
selectedNode.add(n);
t.repaint();
t.updateUI();
}
}
};
}
private ActionListener getEditActionListener() {
return new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if(selectedNode != null){
//edit here
System.out.println("pressed" + selectedNode);
}
}
};
}
public static void main(String... s){
new Main();
}
}
getPopUpMenu method generate your popUp. For all items in popUp I add Listener for actions. For renaming nodes I recommend you to use CellEditor instead of menu, i write simple example of using it here.
And read this tutorial for JTree
Steps:
Add a MouseListner to the JTree
Have the mouse listener only respond to events from Button3 (right click).
Have the listner's action display a JPopupMenu.
In the menu add your options
Your options will have actions that will need to have a reference back to the JTree for the appropriate modifications to happen.
Thanks for your attention!
PLease help a newbie out :)
Current Problem:
Need to change a color of the line when clicking on a MenuItem with the name of the color.
Here is my code for changing the color of the line.
When i create the menuItems i also crete the actionListener for them:
private void CreateMenu()
{
menuBar = new MenuBar();
menu = new Menu("File");
mSave = new MenuItem("Save");
colorSubMenu = new Menu("Choose Color...");
String[] colors = {"red","yellow","green","blue","purple","black"};
for(int i=0;i<colors.length;i++)
{
final int ii = i;
MenuItem m=new MenuItem(colors[i]);
colorSubMenu.add(m);
colorSubMenu.addActionListener(
new ActionListener()
{
#Override public void actionPerformed(ActionEvent e)
{
THIS LINE DOESN'T WORK ===>> color = Color.getColor(colorSubMenu.getItem(ii)));
}
}
);
}
menu.add(mSave);
menu.add(colorSubMenu);
menuBar.add(menu);
setMenuBar(menuBar);
}
But... it doesn't work!
please give an advise. i am running out of ideas.
Will be glad to hear anything:) thanks once again!
UPDATE:
want to change this part to something more elegant and that actually works:
colorSubMenu.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
THIS LINE DOESN'T WORK ===>> color = Color.getColor(colorSubMenu.getItem(ii)));
}
}
);
Color#getColor relys on using colors from the System properties. These probably will not match the colors from the Color array in the question. You can use reflection instead
#Override
public void actionPerformed(ActionEvent e) {
Field field = Class.forName ("java.awt.Color").getField (e.getActionCommand());
Color color = (Color) field.get (null);
// use color...
}
How can I make the comboBox available when the checkBox was uncheck (vice versa)
Why the comboBox is still disable after I unChecked the checkBox?
choice [] = {"A","B","C"};
JComboBox a = new JComboBox(choice);
JCheckBox chk = new JCheckBox("choice");
...
a.addActionListener(this);
chk.addActionListener(this);
...
public void actionPerformed(ActionEvent e) {
//disable the a comboBox when the checkBox chk was checked
if(e.getSource()==chk)
a.setEnabled(false);
//enable the a comboBox when the checkBox chk was unchecked
else if(e.getSource()!=chk)
a.setEnabled(true);
}
If I understand you correctly I think all that you need to do is to change the enabled state of the combo box based on the current value of the checkbox:
public void actionPerformed(ActionEvent e) {
if (e.getSource()==chk) {
a.setEnabled(chk.isSelected());
}
}
I have a similar set up, and I use an Item Listener, like so:
CheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED){
ComboBox.setEnabled(true);
}else if(e.getStateChange()==ItemEvent.DESELECTED){
ComboBox.setSelectedIndex(-1);
ComboBox.setEnabled(false);
}
}
});
This way the behaviour is different when selected and deselected.
I treid this and worked..
public class JF extends JFrame implements ActionListener {
String choice [] = {"A","B","C"};
JComboBox a = new JComboBox(choice);
JCheckBox chk = new JCheckBox("choice");
JF()
{
this.add(a, BorderLayout.NORTH);
this.add(chk, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
a.addActionListener(this);
chk.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//NOTE THE FOLLOWING LINE!!!!
if(e.getSource()==chk)
a.setEnabled(chk.isSelected());
}
public static void main(String[] args) {
new JF().setVisible(true);
}
}
Your old code didn't work because, even unchecking a checkbox triggers the event. The source of the trigger is the checkbox.. so both while checking and unchecking the event source was chk
if (e.getSource() == chckbxModificar) {
if (chckbxModificar.isSelected()) {
cbxImpuesto.setEnabled(true);
cbxMoneda.setEnabled(true);
txtPorcentaje.setEditable(true);
txtSimbolo.setEditable(true);
} else {
cbxImpuesto.setEnabled(false);
cbxMoneda.setEnabled(false);
txtPorcentaje.setEditable(false);
txtSimbolo.setEditable(false);
}
}