Is all in the title,
I do not understand the problem this time is a bit different, I used the same Object(List) for two different programs and it does not work in the second time, see :
private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {
init_creer_client();
List items = new ArrayList();
items.add("mawren");
items.add("blabla");
items.add("Bonjour");
CL.show(cartes,"creer_client");
}
screenshot about the error :
by cons here its work smoothly :
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
public class Test_swingx extends JFrame {
public Test_swingx(String title) throws HeadlessException {
this.setTitle(title);
JPanel pan=new JPanel();
JTextField jtf=new JTextField();
jtf.setColumns(20);
List items = new ArrayList();
items.add("hello");
items.add("marwen");
items.add("allooo");
AutoCompleteDecorator.decorate(jtf, items,false);
pan.add(jtf);
this.setContentPane(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(280, 150, 500, 200);
}
public static void main(String[] args) {
Test_swingx tsx=new Test_swingx("helloo swingx");
}
}
can anyone explain to me ?
You have a java.awt.List import should be java.util.List
It's because the List on the left-hand side is a java.awt.List instead of a java.util.List.
Try changing the line to:
java.util.List items = new ArrayList();
This is probably happening because you're importing java.awt.* and java.util.List. If you can change how you import these classes, you can avoid namespacing the type inline.
Nope, compiles fine:
package cruft;
import java.util.ArrayList;
import java.util.List;
/**
* ListExample description here
* #author Michael
* #link
* #since 2/11/12 7:27 PM
*/
public class ListExample {
public static void main(String[] args) {
List items = new ArrayList();
for (String arg : args) {
items.add(arg);
}
System.out.println(items);
}
}
Runs fine:
"C:\Program Files\Java\jdk1.7.0_02\bin\java" -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 111.255\bin" -Dfile.encoding=UTF-8 -classpath . com.intellij.rt.execution.application.AppMain cruft.ListExample foo bar baz bat
[foo, bar, baz, bat]
Process finished with exit code 0
Sanity check: Have you imported both import java.util.List and import java.util.ArrayList?
Check your imports, because java.awt.List is not the same as java.util.List.
I think the confusion comes from having two List types in different packages, as the error message says. You don't give all the code that generates the error, but I think a reasonable start to a fix would be to change the highlighted line to:
java.util.List items = new ArrayList();
and make sure you have imported java.util.*
Related
I already have the system library in my package and have tried resetting the metadata. What else can I do?
Errors with comments stating what errors they are.
package JFrameTest;
import java.awt.Dimension; //The package java.awt is not accessible
import javax.swing.JFrame; //The type javax.swing.JFrame is not accessible
public class Empty extends JFrame { //JFrame cannot be resolved to a type
public static void main (String[] args) {
new Main().setVisible(true); // Main cannot be resolved to a type
}
JavaFX was deleted from JDK, so you need to download JavaFX from here, here or use this guide. If you use maven just add the dependencies.
public static JComboBox[] ComboBox = new JComboBox[100];
String Array[] = { "Item1", "Item2", "Item3", "Item4" };
final DefaultComboBoxModel model = new DefaultComboBoxModel(Array); // this should assign the array and it does
ComboBox[1] = new JComboBox(model); // added this as the above also didn't help
ComboBox[1].setModel(model); // added this in because the above line didn't help
The issue I'm having with this code is its returning as part of the values, "[L]java,labg.String.....". I think this relates to the fact the array has been called (as a string not a true array).
I've tried every possible method i can think of to get rid of that random entry, I've tried true arrays, lists, nothing works. If I delete out Array..
ComboBox[1] = new JComboBox();
and just call the ComboBox naturally, its still there and I can't figure out why.
Updated 18/09/17 # 7.17am
Sorry I didn't really ask the question too well, it was really late and I'm actually experienced in VBA not so much in Java (which i'm still learning forgive me).
Yes I would like to improve my syntax approach, so please feel free to review my code.
Okay to start with, here is the design of the program. The purpose of the application is assist with capturing data in different aspects of their job, as they have templates they must fill out each time they preform an action (like changes address, leaves the country etc).
They select the questionset (example change of address), and then the template they need to fill out populates onto the userform.
The issue I was having was that when adding a dynamic combobox as an array, the combobox was displaying a strange item ([L]java,labg.String.....), even when all elements of the combobox are empty (or forced to be empty).
All I want is to be able to generate a dynamic combobox in an array format, and be able to retrieve the value the user has selected.
In writing the test code for you guys that you can compile....it just works fine...so I'm thinking I need to re-evaluate how the rest of my program is designed.
Here is the code for your reference, if you could help me figure out how to pass non static method's I'd appreciate that too!
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.BorderLayout;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
public class ExampleApplication {
public static JComboBox[] my_dynamic_combobox = new JComboBox[100]; // Declare my dyanmic combobox so its visible throughout the instance of this class (see my explanation as to why I use static objects)
public static JFrame userform1 = new JFrame("My UserForm"); // Creates new instance of JFrame with title 'My UserForm'
public static class my_functions { // This is what I use to hold my functions to avoid repitition in code
public static void add_dynamic_combobox(int my_combobox_array, String[] my_combobox_items) { // This is a function, it adds a new dynamic combobox based on the value passed form a loop, and assigns the relevant list to it.
my_dynamic_combobox[my_combobox_array] = new JComboBox(my_combobox_items); // This should populate the newly created combobox, with its relevant items ONLY
my_dynamic_combobox[my_combobox_array].setBounds(10, 10, 100, 20); // I'm only displaying one combobox at the moment, so positioning doesn't matter here.
userform1.add(my_dynamic_combobox[my_combobox_array]);
}
// End of my_functions class
}
public static void main(String[] args) {
// The static void won't let me pass in/out any arguments or run any class/methods that aren't static. I don't know how to get around this. Doesn't like non-static context
String the_list[] = { "Item1", "Item2", "Item3"};
userform1.setSize(900, 225); // Set 400 width and 500 height
userform1.setLayout(null); // Using no layout managers
userform1.setDefaultCloseOperation(userform1.EXIT_ON_CLOSE); // define exit behaviour
for (int i=0; i<10; i++){
my_functions.add_dynamic_combobox(i, the_list);
}
// Loop has finished display the form.
userform1.setVisible(true);
}
}
edit 2: 18/09/17 # 9.39pm - Still no luck is isolating the code that is causing the issue. It definitely seems to be an issue when referring between static and non-static methods though.
You can use some thing like this
public static JComboBox[] comboBox = new JComboBox[100];
String[] array = { "Item1", "Item2", "Item3", "Item4" };
comboBox[1] = new JComboBox(array);
to add single items use this
comboBox[1].addItem("Item goes here");
No need to declare a DefaultComboBoxModel, as JComboBox does this for you. So try something like this:
public static JComboBox<String>[] comboBox = new JComboBox[100];
String[] array = { "Item1", "Item2", "Item3", "Item4" };
comboBox[1] = new JComboBox<>(array);
Like the in-built Math class, there are a couple of methods that one can use without importing the Math class. e.g
int io = (int) Math.random();
and notice the import region: no MATH whatsoever
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
but seeing, Math set doesn't have everything i needed, i created mine in a new class, but i can't seem to figure out what to do so i can be able to use it.
Taking a hint from the Math.java file, I've made my class final and my methods static but no avail..
Here's an excerpt of my code
package customops.Sets;
/**
*
* #author Kbluue
*/
public final class SetOpz {
public SetOpz(){}
public static int setMax(int[] set){
int out = set[0];
for(int i=1; i<set.length; i++){
out = Math.max(out, set[i]);
}
return out;
}
how do i use just the import command without having to copy and paste the SetOpz class in the DTL package?
You don't need to import Math explicitly because it is included by default. To use your own code you will have to import it. If you're using IntelliJ or Eclipse or some other smart IDE it will offer to import it for you automatically. Otherwise add a import statement at the top:
import customops.Sets.SetOpz;
You can import your method wherever you want to use with the following import statement
import static customops.Sets.SetOpz.setMax;
I have the following question: I am trying to execute the usConstitution wordcram example (code follows) and if provided as is the code executes in eclipse, the applet starts and the word cloud is created. (code follows)
import processing.core.*;
//import processing.xml.*;
import wordcram.*;
import wordcram.text.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class usConstitution extends PApplet {
/*
US Constitution text from http://www.usconstitution.net/const.txt
Liberation Serif font from RedHat: https://www.redhat.com/promo/fonts/
*/
WordCram wordCram;
public void setup() {
size(800, 600);
background(255);
colorMode(HSB);
initWordCram();
}
public void initWordCram() {
wordCram = new WordCram(this)
.fromTextFile("http://www.usconstitution.net/const.txt")
.withFont(createFont("https://www.redhat.com/promo/fonts/", 1))
.sizedByWeight(10, 90)
.withColors(color(0, 250, 200), color(30), color(170, 230, 200));
}
public void draw() {
if (wordCram.hasMore()) {
wordCram.drawNext();
}
}
public void mouseClicked() {
background(255);
initWordCram();
}
static public void main(String args[]) {
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "usConstitution" });
}
}
My problem is the following:
I want to pass through main (which is the only static class) an argument so as to call the usConstitution.class from another class providing whichever valid filename I want in order to produce its word cloud. So how do I do that? I tried calling usConstitution.main providing some args but when I try to simply print the string I just passed to main (just to check if it is passed) I get nothing on the screen. So the question is How can I pass an argument to this code to customize .fromTextFile inside initWordCram ?
Thank you a lot!
from: https://wordcram.wordpress.com/2010/09/09/get-acquainted-with-wordcram/ :
Daniel Bernier says:
June 11, 2013 at 1:13 am
You can’t pass command-line args directly to WordCram, because it has no executable.
But you can make an executable wrapper (base it on the IDE examples that come with WordCram), and it can read command-line args & pass them to WordCram as needed.
FYI, it’ll still pop up an Applet somewhere – AFAIK, you can’t really run Processing “headless.” But that’s usually only a concern if you’re trying to run on a server.
I have a general question about java. Because I want to create StronaGlowna.java (class) where I have place all buttons, check box and other GUI component which I want to display in main class. The first question is this right way, it's correct ? or maybe is better way to do this thing. My code look this:
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = -4575271483481196192L;
Container pane;
CardLayout layout;
public Main() throws FileNotFoundException, IOException {
layout = new CardLayout();
setLayout(layout);
pane = this.getContentPane();
/*Page: Strona główna */
JPanel newPanel = new JPanel();
pane.add("New", newPanel);
JButton przycisk = new JButton("Przycisk");
newPanel.add(przycisk);
...
In "pane.add("New", newPanel);" I want to display elements from:
package aplikacja.glowna;
import javax.swing.JButton;
import javax.swing.JPanel;
public class StronaGlowna {
public void StronaGlownaDisplay() {
JPanel newPanel = new JPanel();
JButton przycisk2 = new JButton("Przycisk");
newPanel.add(przycisk2);
}
}
Can I import/display all class StronaGlowna in main() something like a include in PHP ? What do You thing about my idea, it's correct or I'm wrong ? Thanks for help and discussion.
It sounds like the way Netbeans handling GUI. You may view the article in http://netbeans.org/kb/docs/java/quickstart-gui.html, it may help you understand how the GUI works since Netbeans can generate code for you. You can always import class and create object to access methods (often public methods) . I think it is not like a include in PHP. PHP include is like to directly include the source code, but jave is not.
First - Never, never, never, code in Main class. Call a method from it and then start your staff in another class. And, of course, don't extend it. And the constructor is neither a good idea. All of these are bad practices. Now, going into your problem, my suggestion is that you make StronaGlowna extend JPanel, and then obtain an instance of it through a public constructor, and use that instance as the parameter for the constructor of JScrollPane. That will make the scrollPane act as a 'screen' inside which you can see the contents of StronaGlowna, which is what I understand you're after.