I am searching an example that use JList as a Desktop application (to replace a Xojo program).
Hi, in this code you can see how to fill and how to remove data from JList
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JPanel.java to edit this template
*/
package javaapplication4;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.DefaultTableModel;
/**
*
* @author thorstenstueker
*/
public class NewJPanel extends javax.swing.JPanel {
public static DefaultListModel<String> list;
/**
* Creates new form NewJPanel
*/
public NewJPanel() {
initComponents();
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame frame = new JFrame();
frame.add(new NewJPanel());
frame.setSize(1280,720);
frame.pack();
frame.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList<>();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jList1.setModel(new javax.swing.AbstractListModel<String>() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(jList1);
jButton1.setText("fill");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("empty");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 469, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(705, 705, 705)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 964, Short.MAX_VALUE))
.addContainerGap())
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultListModel<String> list=new DefaultListModel<>();
list.addElement("Test1");
list.addElement("Test2");
list.addElement("Test3");
list.addElement("TET");
jList1.setModel(list);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultListModel<String> list=new DefaultListModel<>();
jList1.setModel(list); // TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JList<String> jList1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
Thank You.
Like you can see: simple. Also important: in Xojo you are developing a table from the List. Here you have many options to work with Tables with JTable. Following also it’s model!
If you need more informations: I’ll try to help
Hi Thorsten,
I tried to load the model (I downloaded the zip, unzip it, then asked Netbeans to load the project) and get this error:
This is called in the file “project.properties” from “nbproject” folder.
PS: I use (following your advice):
openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Zulu21.32+17-CA (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Zulu21.32+17-CA (build 21.0.2+13-LTS, mixed mode, sharing)
What can I do ?
In the pSettings you need to setup the project as Java21. So open the project properties. There you can setup the Source/Binary format. Please set this to your jdk. If your jdk is not listed go to Libraries. There you’ll find Manage Platforms. Add the platform. Then select the Java platform. After it will be available also for Sources to select the platform. That will end your Errormessage. While I have other JDK installed with my IDE you get this Errormessage.
Thanks for your answer (I have a friend who visit and now I’m back).
Here’s my Project Properties screen:
openjdk version “21.0.2” 2024-01-16 LTS
OpenJDK Runtime Environment Zulu21.32+17-CA (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Zulu21.32+17-CA (build 21.0.2+13-LTS, mixed mode, sharing)
As you can see, I really followed your advice (jdk source/version) with Java/Netbean:
When Netbean issues the error, I set the plane mode to OFF (MacBook Pro m1/No Internet) because my SSD is short of free space.
Can’t I change the version to JDK_21 ? (or simply remove it ?
The user computer is running Windows 10 (or 11). I can control that before install time (when it will be time).
Regards
You can change to 21 or further without problems even 22 will run
Thanks, I will do that right now !
I was able to run the code far above (entry #2) in the Terminal:
I had to comment the line “package javaapplication4;” to achieve that.
Now, the next challenge is to add more columns…
BTW: it seems I was in the wrong way. JTable is what I have to use to show the 13 columns I need to display.
https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html
The TABLEs (service and data) and the TABLE whose contents is displayed on screen.
Then use JTable. Bad news: you have to setup. Good news: works also over TableModel. When placing the table in Designer search in the properties Window for Model and edit the model for your needs. Sounds simple? It is.
You are correct. But you have hard time before you can tell that.
But, I had some exceptions (Frontier, Xojo)… then I had to move away from these…
I can understand that. Definitely.
I found this book:
The definitive guide to Java Swing by John Zukowski
It have a chapter on Tables, with examples. I will read it / try the examples and pray.