I have a jList (named JList1) created using the Desing mode from NetBeans IDE, and I want to add items to that list using a secondary class which parses a big xml list and gets the data from it. My problem is that I dont really understand how to do this, I already tried a lot of different codes, tried with a model too, but cant get it right. I am new to java (and programming too), and I dont understand if I do something like
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);
this created a new jList instead of using my already created one, no ?
created using the Desing mode from NetBeans IDE,
maybe not good idea to be prisonier of code generated by
add a new Item to DefaultListModel
and I want to add items to that list using a secondary class which
parses a big xml list and gets the data from it.
sounds like as you have an issue with Concurency in Swing, updates to the already visible Swing GUI must be done on EDT
use SwingWorker#publish() for long and hard job (which parses a big xml list and gets the data from it.)
for example, add a new Item to DefaultListModel
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Testing extends JFrame {
private static final long serialVersionUID = 1L;
private DefaultListModel listModel = new DefaultListModel();
private JList list = new JList(listModel);
private int currentSelectedRow = 0;
private int xX = 0;
public Testing() {
setLocation(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int x = 0; x < 9; x++) {
listModel.addElement("" + x);
xX++;
}
JScrollPane sp = new JScrollPane(list);
add(sp, BorderLayout.CENTER);
JButton btn1 = new JButton("Reset Model CastingModel");
add(btn1, BorderLayout.NORTH);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
list.clearSelection();
DefaultListModel model = (DefaultListModel) list.getModel();
model.removeAllElements();
// note Swing GUI by default to freeze if is removed more that
// 999 elemets from the JList or its underlaying XxxListModel,
// to use repeatly Actions from SwingTimer on short period
for (int x = 0; x < 9; x++) {
model.addElement("" + (x + xX));
xX++;
}
list.setModel(model);
}
});
JButton btn2 = new JButton("Reset Model directly from Model");
add(btn2, BorderLayout.SOUTH);
btn2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
list.clearSelection();
listModel.removeAllElements();
for (int x = 0; x < 9; x++) {
listModel.addElement("" + (x + xX));
xX++;
}
}
});
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Testing().setVisible(true);
}
});
}
}
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);
The constructor you are using is as follows
/**
* Constructs a <code>JList</code> that displays the elements in
* the specified array. This constructor creates a read-only model
* for the given array, and then delegates to the constructor that
* takes a {#code ListModel}.
* <p>
* Attempts to pass a {#code null} value to this method results in
* undefined behavior and, most likely, exceptions. The created model
* references the given array directly. Attempts to modify the array
* after constructing the list results in undefined behavior.
*
* #param listData the array of Objects to be loaded into the data model,
* {#code non-null}
*/
public JList(final E[] listData)
{
this (
new AbstractListModel<E>() {
public int getSize() { return listData.length; }
public E getElementAt(int i) { return listData[i]; }
}
);
}
So you need to have your array which you are passing as an argument to the constructor final. Also do make use of generics.
final String[] ar = {"one", "two", "three"};
JList<String> Jlist1 = new JList<String>(ar);
Lastly since you are using new keyword it is bound to create new object. Just make your original list point to this new JList object created using your array. Mind you have to make it final and cannot be changed later.
Related
i'm struggeling to make my arraylist into an 2D array and then adding it on a table to show the data.
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class Planettabell
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame vindu = new Test();
vindu.setVisible(true);
}
});
}
}
class Test extends JFrame
{
private String[] name = {"Name", "grade"};
Object[][] cell = {{"nameHer", "GradeHer"}};
Object[][] cell2 = {{"nameHer2", "gradeHer2"}};
Object[][] cell3 = {{"nameHer3", "gradeHer3"} };
public Test()
{
setTitle("Planettabell");
setSize(500, 210);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
List<Object[]> list = new ArrayList<Object[]>();
list.add(cell);
list.add(cell2);
list.add(cell3);
Object[][]array = list.toArray(new Object[list.size()][]);
JTable tabell = new JTable(array, name);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JScrollPane(tabell), BorderLayout.CENTER);
}
}
i will get this message if i run it
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
this code is working if i add 'cell' instead of 'array' on JTable, but i need the entire array from list to work.
i have also tried:
int number = list.size()/2;
Object[][] ArrayNew = new Object[number][2];
for(int x = 0; x< number; x++)
{
for(int z = 0; z < 2; z++)
{
int y = 2 * x;
ArrayNew [x][z] = list.get(y+z);
}
}
JTable tabell = new JTable(ArrayNew, name);
instead of list.toarray. But then i only gett [[Ljava.lang.Object;#28864ae7 and [[Ljava.lang.Object;#49214a13 where the text in the table supposed to be.
would appreicate any answer :)
Your list is effectively a 3D data structure (a list of 2D arrays), it should be only 2D (a list of arrays):
Object[] information = {"nameHer", "GradeHer"};
List<Object[]> list = new ArrayList<Object[]>();
list.add(information); // more data here
Object[][]array = list.toArray(new Object[list.size()][]);
In your code, Object[][] cell = {{"nameHer", "GradeHer"}}; is a 2D array, then you add it into a list (making your list 3 dimensionnal in the process).
Your cells shouldn't be 2D, they represent your rows and must be1D arrays.
Replace by Object[] cell = {"nameHer", "GradeHer"}; and it will work
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class Planettabell
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame vindu = new Test();
vindu.setVisible(true);
}
});
}
}
class Test extends JFrame
{
private String[] name = {"Name", "grade"};
Object[][] cells = {
{"nameHer", "GradeHer"},
{"nameHer2", "gradeHer2"},
{"nameHer3", "gradeHer3"}
};
public Test()
{
setTitle("Planettabell");
setSize(500, 210);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable tabell = new JTable(cells, name);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JScrollPane(tabell), BorderLayout.CENTER);
}
}
You're approaching this problem entirely wrong.
a DefaultTableModel takes a 2d array and displays everything for you including column headers. So, without seeing the code to your KarakterTabell I can't imagine what more you're trying to achieve.
To do a table model correctly all you need to do is have a means to access your data in an x,y fashion. Then, pass in the data stream into this new model it will run when the table comes up:
public class KarakterTableModel implements TableModel {
List<String[]> data = new ArrayList<String[]>();
public KarakterTableModel(BufferedReader reader) {
while(reader.ready()) {
String columnData = reader.readLine();
String[] columns = columnData.split(" ");
data.add(columns);
}
}
public Object getValueAt(int x, int y) {
String[] row = data.get(x);
return row[y];
}
}
JTable table = new Jtable(new KarakterMode(new BufferedReader(System.in));
Also remember: it's public Object getValueAt() -- the JTable will put the "toString()" call of whatever is returned from this call into the cell.
I'm creating a program that reads data from a file, displays it on a GUI that has a JList and JButtons. I am trying to write it with CardLayout so the appropriate JPanel can be displayed when an item is selected from the JList or a JButton is clicked (i.e. next, previous, first and last). I am able to successfully read from the file and display data to the GUI. I've run into 2 problems and I've tried searching online for answers but cant seem to figure it out:
1) How do I get the JPanels to switch using CardLayout?
2) How do I get the data to be displayed in the GUI in text fields when a user clicks an item from the JList? The JList does appear and my ListSelectionListener is working because when I click on a particular item, it will print to the console (as a test).
If I comment out all of the JPanels except for 1, then it is correctly displayed but when I place all of them, then it does not switch.
So far, I have this for my ListSelectionListener (as an inner class):
public class CancerSelectionListener implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent e) {
Integer selection = (Integer)(((JList) e.getSource()).getSelectedIndex());
if(selection == 0) {
System.out.println("blah"); // works
// switch to the corresponding JPanel in CardLayout
}
}
}
String[] tester;
String teste;
listModel = new DefaultListModel();
for(int i = 0; i < 36; i++) {
tester = _controller.readCancer(i); // reads from the file, this part works!
teste = tester[0];
listModel.addElement(teste);
}
cancerList = new JList(listModel);
cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cancerList.setSelectedIndex(-1);
cancerList.setVisibleRowCount(5);
cancerListScroller = new JScrollPane(cancerList);
CardLayout myCardLayout;
myCardLayout = new CardLayout();
mainPanel2.setLayout(myCardLayout);
myCardLayout.show(mainPanel2, "test");
CancerPanels.aplPanel apl = new CancerPanels.aplPanel();
CancerPanels.fcPanels fc = new CancerPanels.fcPanels();
CancerPanels.vhlsPanels vhls = new CancerPanels.vhlsPanels();
CancerPanels.pdgPanels pdg = new CancerPanels.pdgPanels();
CancerPanels.cebpaPanels cebpa = new CancerPanels.cebpaPanels();
mainPanel2.add(apl.aplReturn(), "test");
mainPanel2.add(fc.fcReturn());
mainPanel2.add(vhls.vhlsReturn());
mainPanel2.add(pdg.pdgReturn());
mainPanel2.add(cebpa.cebpaReturn());
// I have 37 JPanels that are placed in the JPanel that uses CardLayout but I didn't post all of them as it would take up lots of space
The data for each JPanel is populated from static inner classes in the CancerPanels class (only showing 1 as each is very long!)
public class CancerPanels extends CancerGUI {
static JPanel cards;
static CancerController _cons = new CancerController();
static String[] cancerData;
static JScrollPane treatmentsScroller = new JScrollPane(txtTreatments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane causesScroller = new JScrollPane(txtCauses, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane symptomsScroller = new JScrollPane(txtSymptoms, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public static class aplPanel extends JPanel {
public JPanel aplReturn() {
treatmentsScroller.setViewportView(txtTreatments);
txtTreatments.setEditable(false);
causesScroller.setViewportView(txtCauses);
txtCauses.setEditable(false);
symptomsScroller.setViewportView(txtSymptoms);
txtSymptoms.setEditable(false);
cards = new JPanel(new GridLayout(6,1));
cancerData = _cons.readCancer(0);
resultName.setText(cancerData[0]);
txtSymptoms.setText(cancerData[1]);
txtCauses.setText(cancerData[2]);
txtTreatments.setText(cancerData[3]);
resultRate.setText(cancerData[4]);
resultPrognosis.setText(cancerData[5]);
cards.add(resultName);
cards.add(symptomsScroller);
cards.add(causesScroller);
cards.add(treatmentsScroller);
cards.add(resultRate);
cards.add(resultPrognosis);
return cards;
}
}
Edit:
Here is my most recent attempt. I can scroll through the JList but it doesn't properly display the correct corresponding JPanel (in fact it doesn't display anything, except whenever I click the last button, I don't know why that button works). I successfully managed to place an ItemListener on a JComboBox but ultimately, I want the CardLayout to work. Our instructor provided us with sample code to use but when I try it, the JPanels do not switch (or if they do they're hidden, not sure why).
Each of my listeners are public inner classes in the overall CancerGUI class.
public CancerGUI() {
CancerPanels.aplPanel apl = new CancerPanels.aplPanel();
CancerPanels.fcPanels fc = new CancerPanels.fcPanels();
CancerPanels.vhlsPanels vhls = new CancerPanels.vhlsPanels();
// more than 30 JPanels that I add to the JPanel that uses CardLayout, so I only posted 3
// each of them uses the GridLayout
mainPanel2 = new JPanel(new CardLayout());
mainPanel2.add(apl.aplReturn(), "1");
mainPanel2.add(fc.fcReturn(), "2");
mainPanel2.add(vhls.vhlsReturn(), "3");
CancerActionButtons _cab = new CancerActionButtons();
btnNext = new JButton("Next");
btnPrevious = new JButton("Previous");
btnFirst = new JButton("First");
btnLast = new JButton("Last");
btnClear = new JButton("Clear");
btnNext.addActionListener(_cab);
btnPrevious.addActionListener(_cab);
btnFirst.addActionListener(_cab);
btnLast.addActionListener(_cab);
CancerItemListener _item = new CancerItemListener(); // this listener works!
renalC.addItemListener(_item);
skinC.addItemListener(_item);
brainC.addItemListener(_item);
bladderC.addItemListener(_item);
ovarianC.addItemListener(_item);
pancC.addItemListener(_item);
breastC.addItemListener(_item);
String[] tester;
String teste;
listModel = new DefaultListModel();
for(int i = 0; i < 36; i++) {
tester = _controller.readCancer(i);
teste = tester[0];
listModel.addElement(teste);
}
cancerList = new JList(listModel);
cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cancerList.setSelectedIndex(-1);
cancerList.setVisibleRowCount(5);
cancerListScroller = new JScrollPane(cancerList);
ListSelection _list = new ListSelection();
cancerList.addListSelectionListener(_list);
JScrollPane treatmentsScroller = new JScrollPane(txtTreatments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
treatmentsScroller.setViewportView(txtTreatments);
JScrollPane causesScroller = new JScrollPane(txtCauses, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
causesScroller.setViewportView(txtCauses);
JScrollPane symptomsScroller = new JScrollPane(txtSymptoms, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
symptomsScroller.setViewportView(txtSymptoms);
public class ListSelection implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent e) {
String selection = (String)(((JList)e.getSource()).getSelectedValue());
((CardLayout) mainPanel2.getLayout()).show(mainPanel2, selection);
((CardLayout) mainPanel2.getLayout()).show(mainPanel2, selection);
}
}
public class CancerActionButtons implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "First":
((CardLayout) mainPanel2.getLayout()).first(mainPanel2);
cancerCount = 1;
break;
case "Last":
((CardLayout) mainPanel2.getLayout()).last(mainPanel2);
cancerCount = 11;
break;
case "Previous":
((CardLayout) mainPanel2.getLayout()).previous(mainPanel2);
cancerCount--;
cancerCount = cancerCount < 1 ? 11 : cancerCount;
break;
case "Next":
((CardLayout) mainPanel2.getLayout()).next(mainPanel2);
cancerCount++;
cancerCount = cancerCount > 11 ? 1 : cancerCount; //
break;
}
cancerList.setSelectedIndex(cancerCount-1);
}
}
/**
* Inner class that responds to any user interaction with a JComboBox for
* general types of cancers.
*/
public class CancerItemListener implements ItemListener {
#Override
public void itemStateChanged(ItemEvent e) {
JPanel showPanel = new JPanel();
if(e.getStateChange() == ItemEvent.SELECTED) {
String selection = (String) e.getItem();
if(selection.equalsIgnoreCase("skin cancer")) {
CancerPanels.skin skin = new CancerPanels.skin();
showPanel = skin.skinReturn();
} else if (selection.equalsIgnoreCase("bladder cancer")) {
CancerPanels.bladder bladder = new CancerPanels.bladder();
showPanel = bladder.bladderReturn();
} else if (selection.equalsIgnoreCase("pancreatic cancer")) {
CancerPanels.pancreatic pancreatic = new CancerPanels.pancreatic();
showPanel = pancreatic.returnPancreatic();
} else if (selection.equalsIgnoreCase("renal cancer")) {
CancerPanels.renal renal = new CancerPanels.renal();
showPanel = renal.returnRenal();
} else if (selection.equalsIgnoreCase("ovarian cancer")) {
CancerPanels.ovarian ovarian = new CancerPanels.ovarian();
showPanel = ovarian.ovarianReturn();
} else if (selection.equalsIgnoreCase("breast cancer")) {
CancerPanels.breast breast = new CancerPanels.breast();
showPanel = breast.returnBreast();
} else if (selection.equalsIgnoreCase("brain cancer")) {
CancerPanels.brain brain = new CancerPanels.brain();
showPanel = brain.returnBrain();
} else if (selection.equalsIgnoreCase("von hippel-lindau syndrome")) {
CancerPanels.vhlsPanels vhls = new CancerPanels.vhlsPanels();
showPanel = vhls.vhlsReturn();
}
JOptionPane.showMessageDialog(null, showPanel);
}
}
}
Seperate class where the JPanels are made before being added to CardLayout:
public class CancerPanels extends CancerGUI {
static String name;
static JPanel cards;
static CancerController _cons = new CancerController();
static String[] cancerData;
static JScrollPane treatmentsScroller = new JScrollPane(txtTreatments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane causesScroller = new JScrollPane(txtCauses, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
static JScrollPane symptomsScroller = new JScrollPane(txtSymptoms, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public static class aplPanel extends JPanel {
public JPanel aplReturn() {
treatmentsScroller.setViewportView(txtTreatments);
txtTreatments.setEditable(false);
causesScroller.setViewportView(txtCauses);
txtCauses.setEditable(false);
symptomsScroller.setViewportView(txtSymptoms);
txtSymptoms.setEditable(false);
cards = new JPanel(new GridLayout(6,1));
cancerData = _cons.readCancer(0);
resultName.setText(cancerData[0]);
txtSymptoms.setText(cancerData[1]);
txtCauses.setText(cancerData[2]);
txtTreatments.setText(cancerData[3]);
resultRate.setText(cancerData[4]);
resultPrognosis.setText(cancerData[5]);
cards.add(resultName);
cards.add(symptomsScroller);
cards.add(causesScroller);
cards.add(treatmentsScroller);
cards.add(resultRate);
cards.add(resultPrognosis);
return cards;
}
In essence what you are trying to do is to change the state of one class from another.
How this is done with Swing GUI's is no different for how it is done for non-GUI programs: one class calls the public methods of another class.
One key is to have wiring to allow this to occur which means references for one class needs to be available to the other class so that appropriate methods can be called on appropriate references. The devil as they say is in the details.
"1) How do I get the JPanels to switch using CardLayout?" -- So the class that holds the CardLayout could for instance have the public methods, next(), previous(), and perhaps show(SOME_STRING_CONSTANT) or some other swapView(...) method.
"2) How do I get the data to be displayed in the GUI in text fields when a user clicks an item from the JList?" -- This will involve the use of listeners -- the class holding the JTextFields will listen for notification from the class that holds the JList, and when notified gets the necessary information from the list-displaying class. A PropertyChangeListener could work well here.
e.g.,
public class CancerSelectionListener implements ListSelectionListener {
private CardDisplayingView cardDisplayingView = null;
public CancerSelectionListener(CardDisplayingView cardDisplayingView) {
this.cardDisplayingView = cardDisplayingView;
}
#Override
public void valueChanged(ListSelectionEvent e) {
int selection = ((JList) e.getSource()).getSelectedIndex();
if(selection == 0) {
if (cardDisplayingView != null) {
cardDisplayingView.swapView(...);
}
}
}
}
I'm trying to implement drag & drop in java with two JList instances.
The basic flow works fine. However, when I'm dragging a string from one list, I want to restrict the drop target only for the second list.
I noticed that when I'm dragging a string from one list to my desktop so it creates a file containing this string.
Is there any way to avoid such situations?
public class SampleDnD extends JFrame{
public static void main(String[] args) {
new SampleDnD();
}
/**
* new form of frame sample contain 2 JLists with drag enabled.
*/
public SampleDnD(){
JList l1 = new JList();
JList l2 = new JList();
JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
DefaultListModel listModel1 = new DefaultListModel();
DefaultListModel listModel2 = new DefaultListModel();
String[] list1 = new String[]{"1","3","5","7","9"};
String [] list2 = new String[]{"0","2","4","6","8"};
for(int index=0;index<list1.length;index++){
listModel1.add(index, list1[index]);
listModel2.add(index, list2[index]);
}
l1.setModel(listModel1);
l2.setModel(listModel2);
l1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l1.setDropMode(DropMode.INSERT);
l1.setDragEnabled(true);
l2.setDragEnabled(true);
l1.setTransferHandler(new ListTransferHandler());
l2.setDropMode(DropMode.INSERT);
l2.setTransferHandler(new ListTransferHandler());
jScrollPane1.setViewportView(l1);
jScrollPane2.setViewportView(l2);
Container cp = getContentPane();
cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));
setPreferredSize(new Dimension(800,500));
getContentPane().add(jScrollPane1);
getContentPane().add(jScrollPane2);
setVisible(true);
pack();
}
public class ListTransferHandler extends TransferHandler {
/**
* We only support importing strings.
*/
public boolean canImport(TransferHandler.TransferSupport info) {
// Check for String flavor
if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
return true;
}
/**
* Bundle up the selected items in a single list for export.
*
*/
protected Transferable createTransferable(JComponent c) {
JList list = (JList)c;
String value = (String)list.getSelectedValue();
return new StringSelection(value);
}
/**
* We support only move actions.
*/
public int getSourceActions(JComponent c) {
return TransferHandler.MOVE;
}
/**
* Perform the actual import. This demo only supports drag and drop.
*/
public boolean importData(TransferHandler.TransferSupport info) {
if (!info.isDrop()) {
return false;
}
JList list = (JList)info.getComponent();
DefaultListModel listModel = (DefaultListModel)list.getModel();
JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
int index = dl.getIndex();
boolean insert = dl.isInsert();
// Get the string that is being dropped.
Transferable t = info.getTransferable();
String data;
try {
data = (String)t.getTransferData(DataFlavor.stringFlavor);
}
catch (Exception e) { return false; }
if (insert) {
listModel.add(index++, data);
} else {
// If the items go beyond the end of the current
// list, add them in.
if (index < listModel.getSize()) {
listModel.set(index++, data);
} else {
listModel.add(index++, data);
}
}
return true;
}
/**
* Remove the items moved from the list.
*/
protected void exportDone(JComponent c, Transferable data, int action) {
JList source = (JList)c;
DefaultListModel listModel = (DefaultListModel)source.getModel();
if(action == TransferHandler.MOVE)
{
listModel.remove(source.getSelectedIndex());
}
}
}
}
A little late, but maybe helpful for somebody else:
You need to create your own DatFlavor, that will only be accepted by your own application.
DataFlavor myObjectDataFlavor = new DataFlavor(MyObject.class, "java/MyObject");
Pass this one instead of a default DataFlavor.javaFileListFlavor or DataFlavor.stringFlavor. Your class MyObject is a Pojo containing only a class variable of type String or an integer or whatever you need.
I'm making a swing based application where I have a JList that periodically get's updated with different orderings of the same data, gets infrequently updated with new items, and also infrequently updated with less items. I'm trying to figure out the best way to make this look good. Right now, I just call
JList.setListData(String [] data);
Which doesn't look too great, and it clears the selected items.
I want a way to update it that only clears the selected items if it was removed from the list, but otherwise keeps the same items selected, even if their index changes. I was looking into keeping track of which indexs are selected and then setting the selected items after changing the data but that sounds horrible. I also looked at ListModels and keeping track of my own but it seems that which items are selected is kept in the JList itself so that wouldn't work perfectly either.
I'd really appreciate any suggestions.
I made you a working example.
I've used the JList like you requested, added buttons that resort, add, and delete from the list.
Anyways, hope this helps!
Cheers.
public class Test {
private static String selectedValue = null;
private static JList jList = new JList();
public static void main(String args[]) {
JFrame jFrame = new JFrame();
jFrame.setSize(500, 500);
jFrame.setLocationRelativeTo(null);
jFrame.setLayout(new GridLayout(4, 1));
jList = new JList(new String[]{"1", "2", "3", "4", "5"});
jFrame.add(jList);
JButton rearrangeButton = new JButton("rearrange");
rearrangeButton.addActionListener(new Test().new SelectedListener());
jFrame.add(rearrangeButton);
JButton addButton = new JButton("add");
addButton.addActionListener(new Test().new SelectedListener());
jFrame.add(addButton);
JButton deleteButton = new JButton("delete");
deleteButton.addActionListener(new Test().new SelectedListener());
jFrame.add(deleteButton);
jFrame.setVisible(true);
}
private class SelectedListener implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
storeSelected();
if (actionEvent.getActionCommand().equalsIgnoreCase("rearrange")) {
jList.setListData(new String[]{"5", "4", "3", "2", "1"});
} else if (actionEvent.getActionCommand().equalsIgnoreCase("add")) {
List< String> items = new ArrayList< String>();
for (int item = 0; item < jList.getModel().getSize(); item++) {
items.add(jList.getModel().getElementAt(item).toString());
}
items.add("new");
jList.setListData(items.toArray());
} else if (actionEvent.getActionCommand().equalsIgnoreCase("delete")) {
List< String> items = new ArrayList< String>();
for (int item = 0; item < jList.getModel().getSize(); item++) {
items.add(jList.getModel().getElementAt(item).toString());
}
items.remove(0);
jList.setListData(items.toArray());
}
reSelect();
}
}
private static void storeSelected() {
if (jList.getSelectedIndex() > -1) {
selectedValue = jList.getSelectedValue().toString();
} else {
selectedValue = null;
}
}
private static void reSelect() {
if (selectedValue != null) {
jList.setSelectedValue(selectedValue, true);
}
}
}
It seems the standard JList has no methods to adjust the sorting (why on earth did they only added that to a JTable ...), but there is a tutorial for sorted lists
I wnt that when i enter some letter in the textfield then the related items should be picked up from my database and should appear as a drop down list.
For Example:
I typed 'J' in text Field, in my database is having names such as {"Juby','Jaz','Jasmine','Joggy'....}
Theses names should appear as a list. So that i could select one from them.and so on for other leters as well.
Is there any predefined control in awt??
Thnx
Why not just use a JComboBox? By default, when the user types a keystroke in a read-only combobox and an item in the combobox starts with the typed keystroke, the combobox will select that item.
Or you could set the JComboBox to be editable using setEditable(true), and use a KeySelectionManager. The link explains selecting an item in a JComboBox component with multiple keystrokes.
This is a small example implementing what ( i think) you asked for.. the database in this example is a vector of strings.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Answer extends JFrame {
public static final int MAXITEMS = 100;
JPanel panel = new JPanel();
JTextField textField = new JTextField(10);
String[] myDataBase = { "Juby", "Jaz", "Jasmine", "Joggy", "one", "dog","cat", "parot" };
String[] listItems;
JList theList = new JList();
public Answer() {
this.add(panel);
panel.setPreferredSize(new Dimension(500, 300));
panel.add(textField);
panel.add(theList);
textField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
String compareString = ("" + textField.getText() + ke.getKeyChar());
listItems = new String[MAXITEMS];
if (compareString.trim().length() > 0 ){
int counter = 0;
for (int i = 0; i < myDataBase.length; i++) {
if (counter < MAXITEMS) {
if (myDataBase[i].length() >= compareString.length() &&
myDataBase[i].substring(0, compareString.length()).equalsIgnoreCase(compareString)) {
listItems[counter] = myDataBase[i];
counter++;
}
}
}
}
theList.setListData(listItems);
}
});
}
public static void main(String[] args) {
final Answer answer = new Answer();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
answer.pack();
answer.setVisible(true);
}
});
}
}
One option is to use GlazedLists, as it has some support for auto-completion.