I'm having difficulty changing unselected options in a JList table to set them to setEnable(false). The method that is receiving the values is an ActionListener button method that, once pressed, receives the selected values from the JList. Here is the method and the buildEnemySelectionPanel() method is creating the JList with the appropriate JPanel for later placement:
private String[] enemies = {"Goblin", "Skeleton"};
private void buildEnemySelectionPanel()
{
enemyPanel = new JPanel();
enemyListPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
enemyListPanel.setPreferredSize(new Dimension(180, 85));
enemyListPanel.setBackground(Color.WHITE);
enemyPanel.setLayout(new BoxLayout(enemyPanel, BoxLayout.Y_AXIS));
enemyList = new JList(enemies);
enemyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
enemyList.addListSelectionListener(new EnemyListListener());
enemyListPanel.add(enemyList);
enemyPanel.add(enemyListPanel);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Select Enemy"))
{
indexEnemy = enemyList.getSelectedIndex();
indexEnemyWeapon = weaponList.getSelectedIndex();
/*
here is where I'm having problems
*/
}
}
So far I've tried to take all of the items from the JList and, matching them with the references from the original String[] list items that I sent to the JList, parsed the indexes and if they didn't match set to false. Unfortunately as you are all probably well aware, compilation errors came up as result due to the fact that the JList is not actually a list. Here is a sample of my for loop that I tried to use in my method above:
for(int x = 1; x < enemyList.length(); x++)
{
if (!(enemies[x] == indexEnemy))
{
enemyList[x].setEnable(false);
}
}
I've read the http://docs.oracle.com/javase/8/docs/api/ , (tried to link 'setEnable') among some examples but don't seem to be making the connection.
Ideally, what I wish to happen is that when the ActionEvent of my button is triggered, all non-selected options in my JList will be disabled. I understand that the end-user will still be able to change his/her mind and make a different selection. But I'd like to still receive some help on how I can set the non-selected items in my JList to false if they are not the indexEnemy from my method above.
Related
this code below creates a checkbox which I want to turn synchronized scrolling on my two panels on/off. Initially when the program runs the scrollbars are independent. When the checkbox is checked and the "if" statement is reached, scroller2's scrollbar is set to the model of scroller1's scrollbar and they are connected, i.e. they move together.
However the problem is when the checkbox is unchecked, meaning I want the scrollbars to be independent again. Nothing I put in the else statement seems to work.
JCheckBox check = new JCheckBox("Global");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println(selected);
if (selected){
scroller2.getHorizontalScrollBar().setModel(scroller.getHorizontalScrollBar().getModel());
} else {
scroller = new JScrollPane(plotPanel);
scroller2 = new JScrollPane(plotPanel2);
}
}
};
check.addActionListener(actionListener);
This:
scroller = new JScrollPane(plotPanel);
scroller2 = new JScrollPane(plotPanel2);
creates two new JScrollPanes but you do nothing with them. Understand that changing the object a variable refers to, here the scroller and scroller2 vairables will have no effect on the object that is displayed, and this issue gets to the core distinction of the difference between a reference variable and a reference or object. If you wanted to change the displayed JScrollPane you'd have to remove the old one, and add the new one to the GUI.
But you don't want to do this. Simply give one of the horizontal JScrollBars a new model, specifically a new DefaultBoundedRangeModel. I would use the constructor that takes 4 ints, and would scrape the value, extent, min, max parameters from the current model.
e.g., something like:
else {
JScrollBar horizBar = scroller2.getHorizontalScrollBar();
DefaultBoundedRangeModel currentModel = (DefaultBoundedRangeModel) horizBar.getModel();
int extent = currentModel.getExtent();
int min = currentModel.getMin();
int max = currentModel.getMax();
int value = currentModel.getValue();
DefaultBoundedRangeModel newModel = new DefaultBoundedRangeModel(value, extent, min, max);
horizBar.setModel(newModel);
}
Note code not tested
So I have this project,
the source code is here.
When you run the project and goto Processing, there is a jcombobox there that is suppose to have an addActionListener.
p_customer_list = new JComboBox<>(customers_name);
pp_customer_list.setPreferredSize(new Dimension(360, 35));
panel_processing_header.add(pp_customer_list);
//pp_customer_list.addActionListener(this);
pp_customer_list.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
JComboBox tmpBox = (JComboBox) e.getSource();
int selected = tmpBox.getSelectedIndex();
pp_refresh_data(selected);
}
});
This is what I have so far, its suppose to find the selected index when the value of the combobox changes and pass it to pp_refresh_data() but for some reason it does not run (I tried putting a JOptionPane to see when the code is executed, and its only executed once when the program runs.)
Hard to tell from just a partial code snippet, but do you have 2 combos, one named "p_customer_list" and another named "pp_customer_list"?
This could be your problem. You may be adding the listener to the wrong combo, or you may be adding the wrong combo to your panel, or maybe you don't need two, or maybe...
Again, it's hard to tell from just a snippet.
Ok I have this table added to a JPanel added to a JFrame.
In the mousePressed Event there it instates a new class tData and calls a method which returns data from the database as a string contained in a vector element.
What I want to happen, every time the btn is pressed it updates the table,
What does happen, every time the btn is pressed it updates the table but adds the same data to the end of the table?
What would cause this to happen?
Object columnHeaders[] = {"Fname", "Mname", "Lname", "Age"};
Object data[][] = {{}};
final DefaultTableModel dtm = new DefaultTableModel(data, columnHeaders);
JTable tb = new JTable(dtm);
JScrollPane scrollPane = new JScrollPane(tb);
tableWrap.add(scrollPane);
btn.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
tData h = new tData(10));
Vector<String> v = h.getInfo();
for(int i = 0; i < v.size() ; i++) {
dtm.insertRow(i, new Object[]{v.get(i).split(",")[0], v.get(i).split(",")[1], v.get(i).split(",")[2], v.get(i).split(",")[3]});
}
dtm.fireTableDataChanged();
frame.repaint();
}
});
Several issues here:
It looks like you need to update the entire table whenever that button is pressed. You have three options here: update the DefaultTableModel using setDataVector, recreate a new model from scratch and set it on the JTable with setModel, implement your own TableModel based on AbstractTableModel and firing appropriate TableModel events. You could also update the model using addRow and removeRow, but that would be very uneffective as it would trigger many events.
To listen for "button pressed" events, rather user an ActionListener (and add it using addActionListener) or an Action (and set it with setAction).
In DefaultTableModel, when you modify the structure, it is not needed to manuall call fireTableDataChanged();. It automatically fires appropriate TableModel events whenever you modify the underlying data. Don't call frame.repaint() either, it is useless and can be time consuming.
Unrelated: Consider following Java coding conventions (Class name starts with a capital letter) and meaningful variable names.
There are quite a few issues with the code you've written here. First, let's address the issue you are having with the code in your mousePressed method:
You are getting a Vector from your data providing code and then setting rows 0...n of the table with your new data. The way to do this is to write a custom extension of AbstractTableModel that returns the data you need when the button is pressed.
Another issue is the way you are handling the button press. Adding a mouse listener to the button is absolutely not the correct way of handling that. You want to add an ActionListener to the button. That listener will be fired whenever the button is pressed and released.
I have JList that grows in size along with the JFrame. I have the following code:
defaultListModel = new DefaultListModel<SData>();
for (String string: listOfStrings) {
defaultListModel.addElement(string);
}
jList = new JList<String>(defaultListModel);
jList.setCellRenderer(jListCellRenderer);
jList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
//codes to go
}
});
jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
jList.setLayoutOrientation(JList.VERTICAL_WRAP);
scrollPane = new JScrollPane(jList);
If I set some value for setVisibleRowCount method, the row number becomes fixed and If I don't set value, default value of 8 comes to play. I want to have this value dynamically changing.
I just found that jList.setVisibleRowCount(0) makes it self adjustable, when resizing JList.
Echoing #kleopatras's comment, it's not clear what controls setVisibleRowCount(). This example grows the enclosing Window as rows are added, up to a predefined limit, then the scrollbar takes over. It might give you some ideas, or you can use it as the basis of your sscce as #Andrew suggests.
Addendum: If the size of JList will control the count, I'd start with half of the model's size(). Then add one visible row for every n added to the model, in a fixed ratio that is pinned to a predefined limit. To maintain a reliable count, you'll have to implement your own ListModel or override the mutators in DefaultListModel.
I have several components, all inheriting JCheckBox without overwriting anything from it, stored in a vector, which is then traversed and each of the components is added to a dialog
CreateLists(); // initialises the checkbox vector
for(int i = 0; i < checkBoxes.size() ; i++){
myPanel.add(checkBoxes.elementAt(i));
}
Some of these checkboxes are already selected.
My problem now is: When I open the dialog and select or unselect any checkbox, the value of the checkboxes in the vector doesn't change. Selected stays selected and unselected stays unselected.
I also tryed to get the new values by using JPanels getCompoents(), but the values of these are wrong, too.
An ItemListener in the checkbox inheritors confirmes that the changes do happen, but whenever I try to get the new values, there just the same as those with which the checkboxes were initialised.
Here is a console output I used to keep track of the changes:
create lists
print values:
checkBox1 = true
checkBox2 = true
checkBox3 = false
checkBox2 clicked new value = false
checkBox3 clicked new value = true
print values:
checkBox1 = true
checkBox2 = true
checkBox3 = false
Here is some more code and information:
CreateList() compares a watchlist with the watchable things, creates the checkboxes accordingly (true = watched etc) and adds them to the new initalised vector.
To read the values i use this:
Component[] components = pnlGesamt.getComponents();
for(int i = 0; i < components.length; i++){
if(components[i] instanceof WLElementCheckBox){
WLElementCheckBox cb = (WLElementCheckBox) components[i];
System.out.println(cb.WlElement().Name() + " = " + cb.isSelected());
}
}
The JCheckBox inheritor:
private WatchListElement wlElement;
public WLElementCheckBox (WatchListElement wl, boolean selected)
{
super();
WlElement(wl);
setSelected(selected);
setText(wlElement.Name());
addItemListener(new MIL());
}
public WatchListElement WlElement ()
{
return wlElement;
}
public void WlElement (WatchListElement wlElement)
{
this.wlElement = wlElement;
}
public class MIL implements ItemListener{
public void itemStateChanged(ItemEvent arg0) {
System.out.println("Ckb " + wlElement.Name() +" geklickt Item neu = " + isSelected());
}
}
There is a small possibility that the changes to your checkboxes are not visible because you're querying them from another thread (such as main) instead of the Event Dispatch Thread (EDT). Swing is not thread-safe. When you check the checkboxes, the values are set on the EDT, but other threads might have the old state saved in memory and don't check to see if it's been updated. So, the first thing you need to do is ensure that when you're checking the states of your checkboxes, you're doing it from the EDT, using a call to SwingUtilities.invokeAndWait() or SwingUtilities.invokeLater().
For more information on why this happens, search for Thread Safety in Java.
(Also, don't be tempted to use synchronization to solve the problem or you might end up with a deadlock in your GUI).
One final pro tip: using new Java for-loop syntax makes code easier to read and write, as well as more efficient in some cases. Your code...
for(int i = 0; i < checkBoxes.size() ; i++){
myPanel.add(checkBoxes.elementAt(i));
}
... can turn into...
for(JCheckBox c : checkBoxes) {
myPanel.add(c);
}
This works on arrays and anything that is Iterable (which includes Collection).