Can i do something like after i clicked the button then the combo box appear,the combo box only appear when i clicked the button.Is it even possible?
Here is my code:
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev) {
if(c3.getSelectedItem()=="First Floor"){
p1.add(c42);
}
});
The c3 is another combo box which contain from ground floor to fourth floor and c42 is combo box which contains room numbers for only First floor,i need something like when i clicked the search button then the c42 appear.
If c42 exists, and it's swing's ComboBox then You can simply use c42.setVisible(true).
Related
I'm making a simple calculator in GUI and I have all the code typed and ready, but I'm having trouble with making it so that when the user presses a number button, the respective number appears in the text box above. Do I need to use a radio button? Thanks in advance
I've tried action listeners but they didn't work (or I'm probably using them incorrectly). I've put the code for the 1 button.
JButton num1 = new JButton("1");
num1 = b1;
num1.setSize(50,50);
num1.setLocation(20,200);
num1.setBackground(Color.WHITE);
num1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Just append the number to the JTextField(using setText(String) and getText()) in the actionPerformed function.
I am having trouble with my code. What i'm trying to do is:
1. Create a Check Box, make it visible
2. When Check Box is selected display Combo Box, which will have few items for example ("1","2")
3. When 1 is selected from Combo Box then make 1 Text Field visible
4. When 2 is selected from Combo Box then make 2 Text Field's visible
What I am able to do is when Check Box is clicked, it displays the Combo Box with the items.
I am not able provide functionality to the items in the Combo Box, such as when Item1 is clicked then make 1 Text Field visible.
Please help needed.
My Code:
public void replacement_used(){
no_of_part_used_label.setVisible(false);
no_part_used_list.setVisible(false);
part_no_one_label.setVisible(false);
part_no_one_field.setVisible(false);
part_no_two_label.setVisible(false);
part_no_two_field.setVisible(false);
part_no_three_label.setVisible(false);
part_no_three_field.setVisible(false);
part_no_four_label.setVisible(false);
part_no_four_field.setVisible(false);
part_no_five_label.setVisible(false);
part_no_five_field.setVisible(false);
HandlerClass handler = new HandlerClass();
replacement_part_check_box.addItemListener(handler);
}
private class HandlerClass implements ItemListener{
public void itemStateChanged(ItemEvent event){
if (replacement_part_check_box.isSelected()){
no_of_part_used_label.setVisible(true);
no_part_used_list.setVisible(true);
}
x();
}
}
public void x(){
System.out.println("Start of x fucntion");
if( no_part_used_list.getSelectedItem().equals("1") ){
System.out.println("It is 1");
part_no_one_label.setVisible(true);
part_no_one_field.setVisible(true);
}
}
Well All you need to do is to add an ActionListener to your ComboBox.
May be first you should go through this link so that you can understand the basics of using combobox in swing http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
Also you need to learn coding conventions so that your code could become more readable and understandable.
1)I have GUI Menu below; From this menu, I should select "Add Job" menu item.
2)By selecting this menu item, I want to show the information belongs to a hashmap collection in a JTextFiled by pressing AddJob Button.
"peeskillet" already answered 2nd part. Thnks to him.
How can I do 1st part? How can I combine two parts?
Thanks,
Serb
i think in swing you cant combine two components but you can make one hidde
"• selecting the "Add Job" menu item, makes visible: • a text field which allows the user to enter the customer name • a set of checkboxes for selecting the features of the job: "On site", "Shorthand", "Translation" • the button labelled "Add Job"
If you just want to show the Add Frame
addMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// do something to make the frame visible like
addJobFrame.setVisible(true);
}
});
If you are wondering how to add a Job to the map
addJobButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
boolean selection1 = cbox1.isSelected();
boolean selection2 = cbox2.isSelected();
boolean selection3 = cbox3.isSelected();
String custName = textField.getText();
jobsMap.put(custName, new Job(custName, selection1, selection2, selection3));
}
});
I have editable JCombobox and I added keylistener for combobox editor component.
When user press 'Enter key' and if there is no text on the editable combobox I need to display message box using JOptinoPane. I have done necessary code in keyrelease event and it displays message as expected.
Problem is, when we get message box and if user press enter key on 'OK' button of JOptionPane, combobox editor keyevent fires again. Because of this, when user press Enter key on message box, JoptionPane displays continuously.
Any idea how to solve this?
Note that I can't use Action listener for this.
Please check if this code helps you!!!
JFrame frame = new JFrame("Welcome!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox cmb = new JComboBox();
cmb.setEditable(true);
cmb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent event) {
if (event.getKeyChar() == KeyEvent.VK_ENTER) {
if (((JTextComponent) ((JComboBox) ((Component) event
.getSource()).getParent()).getEditor()
.getEditorComponent()).getText().isEmpty())
System.out.println("please dont make me blank");
}
}
});
frame.add(cmb);
frame.setLocationRelativeTo(null);
frame.setSize(300, 50);
frame.setVisible(true);
Most people find it difficult because of this casting.
We need to add a key listener on the component that the combo box is using to service the editing.
JTextComponent editor = (JTextComponent) urCombo.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
// your code
}
});
Hope this code helps.
Note that I can't use Action listener for this.
this doesn't make me any sence, then to use ItemListener
Any idea how to solve this?
never to use KeyListener for Swing JComponents, use (Note that I can't use Action listener for this.) KeyBindings instead,
notice ENTER key is implemented for JComboBox in API by default, have to override this action from ENTER key pressed
One option would be to replace the KeySelectionManager interface with your own. You want to replace the JComboBox.KeySelectionManager as it is responsible for taking the inputted char and returns the row number (as an int) which should be selected.
Please check the event ascii code by ev.getkeycode() and check if it is a number or character. If it is neither a number nor a character do nothing.
If it is what you want then do the process.
If you are using Netbeans then right click on your combobox and select customize code.
add following lines of code
JTextComponent editor = (JTextComponent) Code.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
if(evt.getKeyCode()==10)
//do your coding here.
}
});
I have a combo box with a list of entries and when I choose an entry eg dog I want to display an image of a dog beside the combo box.
Would anybody have any examples of this in swt that I could take a look at?
Ann.
Add a SelectionListener to your combo box.
combo.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent event ) {
// ...
}
} );
On the widgetSelectedmethod, get the selection index - using combo.getSelectionIndex() -, map it to your image and display it wherever you want (e.g. on a Label: label.setImage(image)).