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));
}
});
Related
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.
Let 'x' be an item in the JList. When I click it for the first time, the event fires, when I click it again, the event does not fire. I have to click some other item and then come back to 'x'.
How can I fire the event repeatedly from 'x' without having to deal with other items.
This is my code:
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {} else {
String clicked = (String)list.getSelectedValue();
//method to fire is here
}
}
updateDisplays();
}
The ListSelectionListener reflects changes to the lists selection, you could use a MouseListener instead...
For example...
MouseListener ml = new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent evt) {
if (SwingUtilities.isLeftMouseButton(evt) && evt.getClickCount() == 1) {
if (list.getSelectedIndex() != -1) {
int index = list.locationToIndex(evt.getPoint());
System.out.println("You clicked item # " + index);
}
}
}
}
list.addMouseListener(ml);
You can add a MouseListener and watch for clicks. Note that a click that changes the selection will fire both the MouseListener and your ListSelectionListener.
Another option is to immediately clear the selection from your ListSelectionListener; that way the next click will reselect and retrigger, although you will lose the ability to navigate through items with the keyboard.
It seems like sort of an unusual UX decision, though, to assign significance to a click on an already selected item in a list.
Adding based on your question comments: If you go the MouseListener route, I recommend looking for double-clicks instead of single-clicks if the click is going to execute an action (especially if the action changes data and is not undoable). Also note that your ListSelectionListener will execute actions as you navigate through the list with the keyboard, which may not be what you intend.
If your commands in your history list are typed, you could also consider using a drop-down combo box for both command entry and the history list, where a selection from history fills in the command text but does not execute. You'd also have an opportunity to add auto-complete from command history.
I'm new in Java swing, and I have a problem. I made for-loop for creating buttons and now I want automatically give them names or some kind of marks for future recognition (I will need name of clicked button to make it a variable).
How can I give them names in my loop? Thank you.
Here is code of my for-loop:
for (int aa=1; aa<65; aa++)
{
JButton button = new SquareButton("");
gui.add(button);
button.addActionListener((ActionListener) button);
}
I will need name of clicked button to make it a variable).
You don't need a variable to work with the clicked button. Instead you get a reference to the button that was clicked from the ActionListener code:
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
// do processing on the clicked button.
}
In my Java Swing application, I show a list of options to users using a JOptionPane with a JList, using the code below:
List<Object> options = getOptions();
JList list = new JList(options.toArray());
JScrollPane scrollpane = new JScrollPane();
JPanel panel = new JPanel();
panel.add(scrollpane);
scrollpane.getViewport().add(list);
JOptionPane.showMessageDialog(null, scrollpane,
"Please select an object", JOptionPane.PLAIN_MESSAGE);
How can I let the user select an option by double-clicking it?
JList doesn't provide any special handling of double or triple (or N) mouse clicks, but it's easy to add a MouseListener if you wish to take action on these events. Use the locationToIndex method to determine what cell was clicked. For example:
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
}
}
});
I just need to know how to close the dialog after the user
double-clicks the item
In this mouse event, you can make use of SwingUtilities.windowForComponent(list) to get the window and dispose it using window.dispose() function.
See List Action for a solution that will allow you to select an Item from the list with the mouse or the keyboard.
In the Action that you create you can use:
Window window = SwingUtilities.windowForComponent(...);
to get the window that you need to dispose();
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).