import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Test1{
JComboBox combo;
JTextField txt;
public static void main(String[] args) {
Test1 b = new Test1();
}
public Test1(){
String degrees[] = {"AAS1","AAS2","AAS1","AAS3"};
JFrame frame = new JFrame("Creating a JComboBox Component");
JPanel panel = new JPanel();
combo = new JComboBox(degrees);
combo.setEditable(true);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
txt = new JTextField(10);
txt.setText("1");
panel.add(combo);
panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
txt.setText(String.valueOf(combo.getSelectedIndex()+1));
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
} }
As you see from the code above. I have JComboBox with 4 items. If there are no items that are same everything is OK.
But in my example ("AAS1","AAS2","AAS1","AAS3") first and third items are same, and I have problems in this case.
When I select any item I want to get it's index in JTextField, but when I select third item I get index of first item.
Has any idea?
That's because JComboBox is using equals to check the item equality. In your case, those two String are equal so it returns the first index that match. If you really need to do that, you might need to define your own item class like this:
private static class MyItem {
private String value;
public MyItem(String value) {
this.value = value;
}
public String getValue() {
return value;
}
#Override
public String toString() {
return value; //this is what display in the JComboBox
}
}
And then add the item like this:
MyItem degrees[] = {new MyItem("AAS1"),new MyItem("AAS2"),new MyItem("AAS1"),new MyItem("AAS3")};
JComboBox combo = new JComboBox(degrees);
Create a class like that:
class ComboItem{
private String name;
public ComboItem(String name){
this.name = name;
}
public String toString() {
return name;
}
}
and create your combobox:
comboBox = new JComboBox(new ComboItem[]{
new ComboItem("AAS1"),
new ComboItem("AAS2"),
new ComboItem("AAS1"),
new ComboItem("AAS3")
});
You have to separate how the equals is calculated on the Strings items and the effective representation. I think that this can be done just by creating a specific class for your purpose and use it instead that String.
Since this could be homework I'm not going to give exact result, just think about how the JComboBox internally chooses the index specified.
try using combo.getSelectedItem() instead. Since its two different strings in the String Array, you should be able to do a reference comparison and tell a difference between the two.
Related
I'm trying to create a student registration system. In this system, students can see course name, course credit, and the instructor of the course by clicking the "Courses" button.For this purpose i have a Courses class, a database, a frame and a JList courslist.
ArrayList<Courses> aq = Database.allCourses();
//allCourses() is a static method in my Database class that returns fields from my Database as an ArrayList<Courses>
courselist.setListData(Driver.converToCoursesArray(aq));
//Driver.converttoCoursesArray() is a static method in my Driver class that takes a ArrayList<Courses> as a paramater and returns a Courses[] array.
Now, my problem is that in my frame, JList always seen like p1.Courses#4532
I've seen a similar problem when i was accidently trying to print an object with System.out.println(). But in this situation i convert the arraylist to an array and my JList holds objects(JList). So i'll be happy if you help me.
You need to override toString() in the Course class, such that it returns the name of the course you want to display.
Take a look at this example:
import javax.swing.*;
import java.awt.*;
public final class Example extends JFrame {
public Example() {
Course[] courses = {
new Course("Course 1"),
new Course("Course 2"),
new Course("Course 3")
};
JList<Course> courseJList = new JList<>(courses);
getContentPane().add(courseJList);
pack();
setMinimumSize(new Dimension(200, 200));
setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
final class Course {
private final String courseName;
public Course(final String courseName) {
this.courseName = courseName;
}
#Override
public String toString() {
return courseName;
}
}
This displays the following:
New to java and i am unable to see why my action listener is not working on the jcombobox. I think i have followed the other examples on the net to getSelectedItem, but nothing is happening.
FYI, my project is a unit converter (using MVC..hopefully, but that is not my priority).
Any assistance is greatly appreciated.
Thanks, Simon.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class UnitConverterView extends JFrame{
//variables and components
private static final long serialVersionUID = -4673040337179571462L;
private JComboBox<String> unitCategory;
private JTextField fromValue = new JTextField(7);
private JComboBox<String> convertFrom;
private JLabel equalsLabel = new JLabel(" = ");
private JTextField toValue = new JTextField(7);
private JComboBox<String> convertTo;
//constructor
UnitConverterView(){
//set up the view and components
JPanel unitPanel = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,300);
String[] categories = {"Length","Weight","Speed","Temperature"};
unitCategory = new JComboBox<>(categories);
String[] tofromValues = {" "};
convertFrom = new JComboBox<>(tofromValues);
convertTo = new JComboBox<>(tofromValues);
unitPanel.add(unitCategory);
unitPanel.add(fromValue);
unitPanel.add(convertFrom);
unitPanel.add(equalsLabel);
unitPanel.add(toValue);
unitPanel.add(convertTo);
this.add(unitPanel);
}
//get value to convert from
public int getMeasurement() {
return Integer.parseInt(fromValue.getText());
}
//listen for unitCategory to be selected
void addUnitCategoryListener(ActionListener listenForUnitCategory) {
unitCategory.addActionListener(listenForUnitCategory);
}
class UnitCatListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
/*String unitSelected = (String) unitCategory.getSelectedItem();
if (e.getSource() == unitCategory) {
String unitName = (String) unitCategory.getSelectedItem();
System.out.println("UnitName = " + unitName);
changeText(unitName);
}*/
JComboBox cb = (JComboBox)e.getSource();
String unitName = (String) cb.getSelectedItem();
System.out.println("UnitName = " + unitName);
}
void changeText(String name) {
toValue.setText(name);
}
}
}
You have declared a method addUnitCategoryListener() for registering listener to the combobox, but you never called this method. That's why the listener is never registered.
Add the below line at the end of your constructor, then you should be fine:
addUnitCategoryListener(new UnitCatListener());
To simply solve your problem, call the method you created to register the listener on the component. Add this to your constructor:
addUnitCategoryListener(new UnitCatListener());
However, there are a few things you'll want to know:
An ItemListener will usually do a better job than an ActionListener for a JComboBox. The previous one does not fire events if the user selects the already selected item (basically, does nothing). Usually there is nothing you need to do in these cases.
You don't need an extra method just to register the listener, you can directly add to your constructor the line
unitCategory.addActionListener(new UnitCatListener());
and remove your custom method.
The methods changeText and getMeasurement are never used.
Use parametrized types: instead of JComboBox use JComboBox<String>.
You don't need the equalsLabel as a field - a local variable will do - since you do not need to reference it anywhere later (unless you plan on changing a property of the label at runtime).
I am able to expose a private JTextField by doing this:
public void setTextField(String value) {
someTF.setText(value);
}
It would be a lot of work if I have a lot of JTextFields. I tried doing this but failed. No error it's just not setting the right value on specified JTextField.
public class SomeView {
private JTextField someTF = new JTextField(10);
...
public void initComponents() {
...
}
public void setTextField(JTextField jTF, String value) {
jTF.setText(value);
}
}
public class SomeViewTable implements ...{
public void mousePressed(MouseEvent e) {
if (e.getSource() == someButton) {
JTextField someTF = new JTextField(10);
String value = "Some Value";
SomeView sv = new SomeView();
sv.initComponents();
sv.setTextField(someTF, value);
}
}
}
Im expecting this to happen in SomeView class when I called method sameTextField
someTF.setText("Some Value");
Is this possible, what rules in java am I breaking here?
In your listener, you are creating a local variable:
JTextField someTF = new JTextField(10);
...
sv.setTextField(someTF, value);
But what you want is to set the text field of SomeView. So remove the first line, and replace the second with:
sv.setTextField(sv.someTF, value);
Now, to answer the more global question of how to expose many private JTextFields through one method, one possibility could be to assign a string ID to each of them, and store them all in a HashMap:
Map<String,JTextField> map = new HashMap<String,JTextField>();
map.put("field 1", textField1);
...
map.put("field n", textFieldn);
public void setTextField(String id, String value) {
map.get(id).setText(value);
}
Or you could simply generate getters automatically for all your fields (most IDE do that painlessly)...
I am designing a basic telephone directory for a project. It has three classes, Main (for the GUI), TelephoneDirectory (an object to store TelephoneRecords objects) and a TelephoneRecords class (where information for each record object is stored).
The requirements state: Extend your application by adding a list displaying the complete current contents of the telephone directory, ordered alphabetically by name. You will need to implement a ListModel. You may want to study class AbstractListModel before starting on your own implementation.
Only problem is, I have absolutely no idea how to extend my application to achieve this. I have searched online all night and haven't found a way to do this. I have tried storing the objects in an AbstractListModel rather than a HashMap but get errors. I don't exactly know what or why it is used and how I could use it. The next requirement (by the way) is to have the JList auto-update with new data when it's entered so I guess it has something to do with that?
Either way, if anyone could help it'd be great. My current working code for the previous that needs to be edited version is:
MAIN
public class Main extends JFrame implements ActionListener {
private static TelephoneDirectory directory = new TelephoneDirectory();
private JTextField nameField;
private JTextField numberField;
private JList contactList;
public Main() {
setTitle("Telephone Directory");
setLayout(new GridLayout(0,2));
JLabel nameLabel = new JLabel("Name of Contact:");
nameField = new JTextField(20);
add(nameLabel);
add(nameField);
JLabel numberLabel = new JLabel("Number of Contact:");
numberField = new JTextField(20);
add(numberLabel);
add(numberField);
JButton enterButton = new JButton("Enter");
JButton cancelButton = new JButton("Cancel");
enterButton.addActionListener(this);
cancelButton.addActionListener(this);
add(enterButton);
add(cancelButton);
JLabel contactsLabel = new JLabel("Current Contacts:");
contactList = new JList();
add(contactsLabel);
add(contactList);
setVisible(true);
pack();
}
public static void main(String[] args) {
new Main();
}
#Override
public void actionPerformed(ActionEvent arg0) {
JButton jb = (JButton) arg0.getSource();
if (jb.getText().equals("Cancel")) {
System.exit(0);
} else {
directory.addRecord(nameField.getText(), new TelephoneRecords(nameField.getText(), numberField.getText()));
System.out.println("Added record for " + nameField.getText() + ": number is " + numberField.getText() + ".");
}
}
}
TELEPHONEDIRECTORY
public class TelephoneDirectory implements Iterable<TelephoneRecords> {
private HashMap records;
public TelephoneDirectory() {
records = new HashMap<String, TelephoneRecords>();
}
public void addRecord(String name, TelephoneRecords newRecord) {
records.put(name, newRecord);
}
public TelephoneRecords getRecord(String name) {
return (TelephoneRecords) records.get(name);
}
public void getDirectory() {
System.out.println("Telephone Directory:");
records.values().iterator();
}
#Override
public Iterator<TelephoneRecords> iterator() {
return records.values().iterator();
}
}
TELEPHONERECORDS
public class TelephoneRecords {
private String name;
private String number;
public TelephoneRecords(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
#Override
public String toString() {
return "The phone number of " + name + " is " + number + ".";
}
}
You may be trying to do too much with inheritance. Rather than using an AbstractListModel in place of your HashMap, consider creating a class that extends AbstractListModel and that holds the TelephoneDirectory class with its HashMap as the nucleus of the AbstractListModel's data. This is called extending a class by composition rather than by inheritance.
Edit: Also consider using a TreeMap rather than a HashMap so as to be able to retrieve your names and telephone records in name order. You'll also need to give your TelephoneDirectory class a getElementAt(int index) and a getSize() method to allow it to be used within the AbstractListModel class.
Im using java enums to define how to render a modal window with buttons (Vaadin handles the rendering). My problem is that when I run the gui my buttons comes in a randomized order each time. So my question is this, since i use a enum set to hold my buttons, will that be unordered? whats the best way to make it into a ordered list?
My settings enum
public enum MODAL_SETTINGS {
NEW_MODAL_WINDOW("menu.context.new", "400", MODAL_BUTTON.SAVE, MODAL_BUTTON.CANCEL),
EDIT_MODAL_WINDOW("menu.context.modify","400", MODAL_BUTTON.UPDATE, MODAL_BUTTON.CANCEL),
DELETE_MODAL_WINDOW("menu.context.delete", "250", false, MODAL_BUTTON.DELETE, MODAL_BUTTON.CANCEL);
private EnumSet<MODAL_BUTTON> buttons;
private String caption;
private String width;
private boolean isResizable = true;
private MODAL_SETTINGS(String caption, String width, MODAL_BUTTON... buttons){
this.setCaption(caption);
this.setWidth(width);
this.buttons = EnumSet.copyOf(Arrays.asList(buttons));
}
private MODAL_SETTINGS(String caption, String width, boolean isResizable, MODAL_BUTTON... buttons){
this.setCaption(caption);
this.setWidth(width);
this.isResizable = isResizable;
this.buttons = EnumSet.copyOf(Arrays.asList(buttons));
}
public EnumSet<MODAL_BUTTON> getButtons(){
return buttons;
}
#Override
public String toString(){
String s = super.toString();
s=s.replaceAll("_", ".");
return s;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getCaption() {
return caption;
}
public void setWidth(String width) {
this.width = width;
}
public String getWidth() {
return width;
}
public boolean isResizable() {
return isResizable;
}
}
My buttons enum
public enum MODAL_BUTTON {
SAVE, UPDATE, CANCEL, DELETE;
}
Use Enum.values() instead of an EnumSet:
Note that each enum type has a static values method that returns an array containing all of the values of the enum type in the order they are declared. This method is commonly used in combination with the for-each loop to iterate over the values of an enumerated type.
Source: Enums in the Java 1.5 documentation
According to the documentation for Enumset, the iterator should return the Enum constants in the order in which they were declared.
The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared). The returned iterator is weakly consistent: it will never throw ConcurrentModificationException and it may or may not show the effects of any modifications to the set that occur while the iteration is in progress.
Although it can be something about with your UI thread accessing the Enums in different orders.
From EnumSet documentation:
The iterator returned by the iteratormethod traverses the elements in their natural order (the order in which the enum constants are declared).
So, your problem is somewhere else.
You must have something in the place where you are actually assiging the buttons, which changes their order. Try to give us a SSCCE.
Check out the sample code below where each time you run it the order of buttons will be the same for all rows.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class EnumeOrderButtonsTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JPanel p = new JPanel(new GridLayout(0, MODAL_BUTTON.values().length));
int noOfRows = 10;
for(int i = 0; i < noOfRows; i++)
for(MODAL_BUTTON mb : MODAL_BUTTON.values())
p.add(new JButton(mb.name()));
JFrame f = new JFrame();
f.setContentPane(p);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
public enum MODAL_BUTTON
{
SAVE, UPDATE, CANCEL, DELETE;
}
}