Reputation: 19
A couple of questions about certain GUI based functions.
Question 1:
Displaying data from the jComboBoxes (Manufacturers and Vehicles) into a jTextArea. In the code below I used insert function which sets the text of the first line of the textarea. But I want it to display the manufacturer at the top and then the vehicle chosen by the user.
private void ManufacturersActionPerformed(java.awt.event.ActionEvent evt) {
String str1 = (String) Manufacturers.getSelectedItem();
jTextArea1.insert(str1, WIDTH);
}
private void VehiclesActionPerformed(java.awt.event.ActionEvent evt) {
String str2 = (String) Vehicles.getSelectedItem();
jTextArea1.insert(str2, WIDTH);
I guess I could change it to setText function or something else.
Question 2:
I have 2 jlists that I want to display multiple data selected from the user into the jTextArea but I don't know what event handler to use for it that is selected from NetBeans functions.
Code for program below:
package rlc.building.program;
/*
*
* @author Jacob
*/
public class GUI extends javax.swing.JFrame {
/**
* Creates new form GUI
*/
public GUI() {
initComponents();
}
/**
* 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() {
jScrollPane2 = new javax.swing.JScrollPane();
Modifications = new javax.swing.JList();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Parts = new javax.swing.JList();
Reset = new javax.swing.JButton();
Manufacturers = new javax.swing.JComboBox();
Vehicles = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Modifications.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "LED Light Bar £250", "Rally Light Hood Mount £150", "Rally Lights x2 £100", "Limo Tinted Windows £1500", "Custom Exo-Skelton Roll Cage £3000", "Custom Roll-Cage £1000", "Thick White Wall Tires £1450", "Front and Rear Strut Bars £400", " ", " " };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane2.setViewportView(Modifications);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane3.setViewportView(jTextArea1);
jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
jLabel1.setText("Real Life Car Building Program");
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel2.setText("Modifications");
jLabel2.setToolTipText("This area below is a list of all the modifications you can choose from for any vehicle.");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel3.setText("Manufacturers");
jLabel3.setToolTipText("The car companys that you can choose from.");
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel4.setText("Vehicles");
jLabel4.setToolTipText("The vehicles you can choose from.");
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jLabel5.setText("Parts");
jLabel5.setToolTipText("The parts that you can install on the vehicle straight from the manufacturer.");
Parts.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Leather Seats £1000", "New Stainless Steel Exhaust £500", "Alloy Wheels £1200", "Checker Plated Steel Panels £400", " " };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(Parts);
Reset.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
Reset.setText("Reset");
Reset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResetActionPerformed(evt);
}
});
Manufacturers.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chevrolet", "Dodge", "Ford", "Honda", "Infiniti", "Land Rover", "Mazda", "Mitsubishi", "Nissan", "Pontiac", "Scion", "Subaru", "Toyota", "Volkswagen" }));
Manufacturers.setToolTipText("This is the drop down menu that lists the manufacturers chosen. Click the down arrow to see the selection.");
Manufacturers.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ManufacturersActionPerformed(evt);
}
});
Vehicles.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1957 Bel Air", "1970 El Camino SS 454", "2014 Corvette Stingray", "1970 Camaro Z28", "1996 Impala SS", "2014 Super Sport", "2015 Chevrolet Corvette Z06", " ", "2015 Hellcat Charger R/T ", "2015 Hellcat Challenger ", "2015 Dodge Viper ", "2015 Dodge Dart ", "1969 Dodge Charger R/T ", "1969 Dodge Charger Daytona Hemi", " ", "1956 Ford F100 ", "1987 Ford Sierra Cosworth RS500 ", "1973 Ford XB Falcon GT ", "1977 Ford Escort RS1800 ", "2015 Ford Mustang GT ", "1973 Ford Capri RS3100 ", "1975 Ford Bronco ", "2003 Ford Focus RS ", "2014 Ford Fiesta ST", " ", "1997 Honda Civic Type R ", "2009 Honda S2000 CR ", "1994 Honda Civic 1.5 VTi ", "1999 Honda Civic Si Coupe ", "1991 Honda CR-X SiR ", "2000 Honda Integra Type-R ", "1986 Honda Civic Si", " ", "2003 Infiniti G35 ", "2006 Infiniti G37 ", "2015 Infiniti Q60 Coupe ", "2015 Infiniti Q70 ", "2015 Infiniti QX50", " ", "2015 Land Rover The Defender ", "2015 Land Rover Discovery ", "2015 Land Rover Range Rover", " ", "1990 Mazda MX-5 Miata ", "2016 Mazda MX-5", "1997 Mazda RX-7 ", "1994 Mazda MX-5 Miata ", "2007 Mazda MX-5 Roadster Coupe ", "1990 Mazda Savanna RX-7 ", "2011 Mazda RX-8 R3 ", "1985 Mazda RX-7 GSL-SE ", "1973 Mazda RX-3", " ", "2008 Mitsubishi Lancer Evolution X GSR ", "1999 Mitsubishi Lancer Evolution VI GSR ", "2006 Mitsubishi Lancer Evolution IX MR ", "2010 Mitsubishi Colt Ralliart ", "1988 Mitsubishi Starion ESI-R ", "2004 Mitsubishi Lancer Evo VIII MR ", "1992 Mitsubishi Galant VR-4 ", "2015 Mitsubishi Shogun ", "2015 Mitsubishi L200", " ", "2010 Nissan 370Z ", "1994 Nissan 240SX SE ", "1971 Nissan Skyline 2000GT-R", "2002 Nissan Skyline GT-R V-Spec II ", "2010 Nissan GT-R SpecV ", "2000 Nissan Silvia Spec-R ", "2012 Nissan GT-R Black Edition ", "1992 Nissan Silvia CLUB K's ", "2015 Nissan IDx NISMO", " ", "1969 Pontiac GTO Judge ", "1973 Pontiac Firebird Trans Am SD-455 ", "1977 Pontiac Firebird Trans Am ", "1969 Pontiac Firebird Trans Am ", "1965 Pontiac GTO ", "2006 Pontiac GTO", " ", "2013 Scion FR-S ", "2012 Scion tC ", "2014 xB ", "2015 Scion FR-S ", "2015 Scion tC", " ", "2011 Subaru WRX STI ", "1990 Subaru Legacy RS ", "1998 Subaru Impreza 22B STi ", "2004 Subaru Impreza WRX STi ", "2005 Subaru Impreza WRX STI ", "2008 Subaru Impreza WRX STi ", "2013 Subaru BRZ ", "1980 Subaru BRAT GL ", "2015 Forester", " ", "2015 Toyota Land Cruiser V8 ", "2015 Toyota Hilux ", "2015 Toyota Prius ", "1985 Toyota Sprinter Trueno GT Apex ", "1969 Toyota 2000GT ", "1974 Toyota Celica GT ", "1998 Toyota Supra RZ ", "2013 Toyota GT86", " ", "1963 Volkswagen Beetle ", "1963 Volkswagen Type 2 De Luxe", "2003 Volkswagen Golf R32 ", "2010 Volkswagen Golf R ", "2011 Volkswagen Polo GTI ", "2015 VolksWagen Up! ", "2015 VolksWagen New Scirocco ", "2015 VolksWagen e-up!", " ", " " }));
Vehicles.setToolTipText("");
Vehicles.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
VehiclesActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(208, 208, 208)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(59, 59, 59)
.addComponent(jLabel3)
.addGap(163, 163, 163)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(93, 93, 93))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3)
.addGap(18, 18, 18)
.addComponent(Reset)
.addGap(26, 26, 26))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(71, 71, 71)
.addComponent(jLabel4))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Manufacturers, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Vehicles, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jLabel3)))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(Manufacturers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Vehicles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(Reset)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
.addContainerGap())))
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))))
);
Manufacturers.getAccessibleContext().setAccessibleName("Manufacturers");
Vehicles.getAccessibleContext().setAccessibleName("Vehicles");
pack();
}// </editor-fold>
private void ResetActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText("");
}
private void ManufacturersActionPerformed(java.awt.event.ActionEvent evt) {
String str1 = (String) Manufacturers.getSelectedItem();
jTextArea1.insert(str1, WIDTH);
}
private void VehiclesActionPerformed(java.awt.event.ActionEvent evt) {
String str2 = (String) Vehicles.getSelectedItem();
jTextArea1.insert(str2, WIDTH);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox Manufacturers;
private javax.swing.JList Modifications;
private javax.swing.JList Parts;
private javax.swing.JButton Reset;
private javax.swing.JComboBox Vehicles;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
Thanks for the help, Jacob.
Upvotes: 0
Views: 1780
Reputation: 4277
1) setText()
will set the whole text at once. In your situation, it would be more convenient to use append()
which "Appends the given text to the end of the document."
2) You should use addListSelectionListener()
on the JList
, in order "to be notified each time a change to the selection occurs". Then you can update the text in the JTextArea
.
e.g.
myJList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
myTextArea.setText(""); // Reset the text area, to avoid appending multiples times the same values
for(Object o : myJList.getSelectedValuesList()){
myTextArea.append(o.toString()+"\n");
}
}
});
EDIT: As you have various sources writing data to the text area, you should centralize all the writing in one method:
public void updateTextArea(){
myTextArea.setText(""); // Reset the text area, to avoid appending multiples times the same values
// Write from comboboxes
myTextArea.append( (String) Manufacturers.getSelectedItem() );
myTextArea.append( (String) Vehicles.getSelectedItem());
// Write from the JList
for(Object o : myJList.getSelectedValuesList()){
myTextArea.append(o.toString()+"\n");
}
}
And call the method from all the listeners:
private void ManufacturersActionPerformed(java.awt.event.ActionEvent evt) {
updateTextArea();
}
private void VehiclesActionPerformed(java.awt.event.ActionEvent evt) {
updateTextArea();
}
...
myJList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
updateTextArea();
}
});
Upvotes: 2