So far I have
Terminal terminal = new DefaultTerminalFactory().createTerminal();
TerminalScreen screen = new TerminalScreen(terminal);
MultiWindowTextGUI mwtg = new MultiWindowTextGUI(screen);
CheckBoxList checkBoxList = new CheckBoxList<String>();
checkBoxList.addItem("Check one");
checkBoxList.addItem("Check two");
What I can't figure out is how to add checkBoxList directly to mwtg
Many thanks for your help
ok, so after muddling blindly through the API for hours guessing here and there, I did this, which works but is probably clunky or smells, so please feel free to improve on my own answer.
private MultiWindowTextGUI mwtg;
private BasicWindow bw;
private CheckBoxList<String> checkBoxList;
private List<String> ckeckedItems;
Terminal terminal = new DefaultTerminalFactory().createTerminal();
TerminalScreen screen = new TerminalScreen(terminal);
MultiWindowTextGUI mwtg = new MultiWindowTextGUI(screen);
this.checkBoxList = new CheckBoxList<String>();
this.checkBoxList.addItem("item1");
this.checkBoxList.addItem("item2");
this.checkBoxList.addItem("item3");
this.checkBoxList.addListener((sel, prev) ->
{ this.ckeckedItems = this.checkBoxList.getCheckedItems(); }
);
Panel panel = new Panel();
panel.setLayoutManager(new GridLayout(4));
panel.addComponent(this.checkBoxList);
Button button = new Button("Done", () -> this.bw.close());
button.addTo(panel);
this.bw = new BasicWindow("Choices");
this.bw.setComponent(panel);
this.mwtg.addWindowAndWait(this.bw);
I hope this may be useful for someone...
Related
I want to use JLine to build a simple CLI. But I ran into a problem. File name completer doesn't work properly on Windows.
When I enter 8> first C:\ and press tab, completer do nothing although it should display all subfolders.
Here is my code:
ArgumentCompleter completer1 = new ArgumentCompleter(
new StringsCompleter("first"),
new FileNameCompleter(),
new NullCompleter()
);
ArgumentCompleter completer2 = new ArgumentCompleter(
new StringsCompleter("second"),
new NullCompleter()
);
AggregateCompleter completer3 = new AggregateCompleter(
completer1, completer2
);
ConsoleReader console = new ConsoleReader();
console.addCompleter(completer3);
while (true) {
String line = console.readLine("8> ");
if (line.isEmpty()){
console.println();
}
}
Any ideas how to fix this?
How do I convert string to JComboBox object model ?
Note: Im using NetBeans IDE 8.0.2
to change JComboBox from <String> to <Object>
right click on JComboBox and select properties
select code tab
click on ... button from type parameters
replace <String> to <Object> and click ok.
You don't convert a String to a ComboBoxModel. You add the String to the combo box or the ComboBoxModel.
For example:
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.add( "One" );
comboBox.add( "Two" );
comboBox.add( "Three" );
Read the section from the Swing tutorial on How to Use Combo Boxes for more information and other examples.
You can also search the forum or web for other examples.
I solved the problem.
I needed to be able to grab each line independent of the other so I can use just one Text file but break it out to different j combo boxes. Below was what I did. Is there a shorter way of doing this? I will have 20 JComboBox with each having around 7 select entries. The entries for the drop down boxes are around 50 lines of selections.
public void inputFile() throws IOException{
//File reader method
FileReader file = new FileReader("c:\\jcboEntries.dat");
try (BufferedReader br = new BufferedReader(file)) {
String[] lines = new String [6];
String [] jcbo = new String [6];
try {
int i =0;
lines[0] = br.readLine();
jcbo[0] = lines[0];
jcbo0 = jcbo[0];
jcboNUMONE.addItem(jcbo0);
System.out.println(jcbo0);
lines[1] = br.readLine();
jcbo[1] = lines[1];
jcbo1 = jcbo[1];
jcboNUMONE.addItem(jcbo1);
System.out.println(jcbo1);
lines[2] = br.readLine();
jcbo[2] = lines[2];
jcbo2 = jcbo[2];
jcboNUMONE.addItem(jcbo2);
System.out.println(jcbo2);
lines[3] = br.readLine();
jcbo[3] = lines[3];
jcbo3 = jcbo[3];
jcboNUMONE.addItem(jcbo3);
System.out.println(jcbo3);
lines[4] = br.readLine();
jcbo[4] = lines[4];
jcbo4 = jcbo[4];
jcboNUMONE.addItem(jcbo4);
System.out.println(jcbo4);
lines[5] = br.readLine();
jcbo[5] = lines[5];
jcbo5 = jcbo[5];
jcboNUMONE.addItem(jcbo5);
System.out.println(jcbo5);
} catch (IOException e){}
} catch (IOException e){}
}
I have a question that has been boring me for some days.
I'm implementing some swing interfaces that is supposed to load an image:
When i press a button it triggers an event that calls the following method:
private void GetDataPanelGeral()
{
ArrayList<String> Files = new ArrayList<>();
java.util.Date selectedDate = (java.util.Date) this.JDatePickerImpl_GERAL_MUDANÇAS_datePicker_START.getModel().getValue();
java.sql.Date sqlDateINICIO = new java.sql.Date(selectedDate.getTime());
selectedDate = (java.util.Date) this.JDatePickerImpl_GERAL_MUDANÇAS_datePicker_END.getModel().getValue();
java.sql.Date sqlDateFIM = new java.sql.Date(selectedDate.getTime());
//CALL MODULE 5
RunningLoop loop = null;
Thread t = new Thread
(
loop = new RunningLoop(50000,this.StringDBName,this.StringDBDriver,
this.StringDBUser,this.StringDBPass,this.PathToData,
sqlDateINICIO.toString(),
sqlDateFIM.toString(),
true)
);
t.start();
loop.StopLooping();
this.ImageLabelGERALMUNDANCAS.revalidate();
this.ImageLabelGERALMUNDANCAS.repaint();
this.ImageLabelGERALMUNDANCAS = new JLabel(new ImageIcon("this.PathToData+"/Results/Outputs/P5/0.png",""));
this.ImageLabelGERALMUNDANCAS.repaint();
JPanel Aggregator = new JPanel();
Aggregator.add(this.ImageLabelGERALMUNDANCAS);
Aggregator.setBackground(this.color);
//this.Aggregatorr.add(this.ImageLabelGERALMUNDANCAS);
this.JPanel_TABGeral.remove(1);
this.JPanel_TABGeral.add(Aggregator,1);
this.JPanel_TABGeral.revalidate();
this.JPanel_TABGeral.repaint();
Aggregator.revalidate();
Aggregator.repaint();
}
the image loaded in the line this.ImageLabelGERALMUNDANCAS = new JLabel(new ImageIcon("this.PathToData+"/Results/Outputs/P5/0.png","")); is created by a module called in the lines
//CALL MODULE 5
RunningLoop loop = null;
Thread t = new Thread
(
loop = new RunningLoop(50000,this.StringDBName,this.StringDBDriver,
this.StringDBUser,this.StringDBPass,this.PathToData,
sqlDateINICIO.toString(),
sqlDateFIM.toString(),
true)
);
t.start();
loop.StopLooping();
it seems to be all ok but it does not load the image.. If i try to load another image it works perfectly so i think the problem is the creation of the image in the thread.
Can somebody help me?
Ps: I'm still need to work in some code, currently i'm trying everything to get this work, then i'll worry about better code
Kind regards
Im new to java and just wondering how do i get rid of this error:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011)
at java.lang.Double.parseDouble(Double.java:540)
at fksdfs.test1.assign(test1.java:69)
at fksdfs.test1.main(test1.java:60)
I'm trying to get the Value from the textfield and convert it into a double to perform calculations on but it is not working. Can anyone help me?
Sorry for the messy coding.
public class test1 {
private JTextField tField;
public static void main(String[] args) {
JLabel Age = new JLabel("Age");
JTextField textField = new JTextField(10);
JLabel Overall = new JLabel("Overall: ");
JTextField age = new JTextField(10);
JLabel shoot = new JLabel("Shooting: ");
JTextField shooting = new JTextField(10);
JLabel dribble = new JLabel("Dribbling: ");
JTextField dribbling = new JTextField(10);
JLabel pass = new JLabel("Passing : ");
JTextField passing = new JTextField(10);
JLabel defend = new JLabel("defending : ");
JTextField defending = new JTextField(10);
JLabel head = new JLabel("Heading : ");
JTextField heading = new JTextField(10);
JLabel pace = new JLabel("Pace: ");
JTextField pacey = new JTextField(10);
JPanel panel = new JPanel();
panel.add(Age);
panel.add(textField);
panel.add(Overall);
panel.add(age);
panel.add(shoot);
panel.add(shooting);
panel.add(dribble);
panel.add(dribbling);
panel.add(pass);
panel.add(passing);
panel.add(defend);
panel.add(defending);
panel.add(head);
panel.add(heading);
panel.add(pace);
panel.add(pacey);
String age1 = textField.getText();
String overall1 = age.getText();
String shooting1 = shooting.getText();
String dribbling1 = dribbling.getText();
String passing1 = passing.getText();
String heading1 = heading.getText();
String defending1 = defending.getText();
String pace1 = pacey.getText();
JOptionPane.showMessageDialog(null, panel);
}
public static void assign(String s) {
Double.parseDouble(s);
}
}
The code that is causing your error is not shown in your code above. It is where you call the assign method. When you are calling it, the String is empty, and so you're trying to convert "" into a number which makes sense should throw this exception.
Note the exception message is telling you all this:
Exception in thread "main" java.lang.NumberFormatException: empty String:
The String is empty -- no number in it.
at fksdfs.test1.assign(test1.java:69)
It's occurring at line 69 of test1.java where you all assign.
The fix: don't do this, don't try to parse an empty String. Test if the String is empty first, or do your parsing in a try/catch block.
As an aside, please put in the effort to post well formatted code when asking questions here. We're volunteers and as such expect you to do put in the effort so that it isn't unduly hard to read and understand your code if you're asking us for help. Also, avoid doing all this stuff in the main method or using static methods such as your assign method. This method should be an instance method, and most all of your code should be in a proper OOP class. You will want to learn how to create classes and basics of Java before trying to create complex GUI's, and the Java Tutorials can help.
Just as a tip for your future code...
When using GUIs and inputing code it is often better to create a pre-made exception handler for incorrect inputs. My preference would be:
try{
double somethingDouble = Double.parseDouble(something.getText());
}catch(){
JOptionPane.showMessageDialog(frame,
"Check input - this is supposed to be a decimal value!");
something.setText("");
e.printStackTrace();
}
that way you could just add the try block into an actionPerformed method and determine if the user of your product - or in this case, your code - could determine if he/she entered in valid input.
Note: place the e.printStackTrace() line after the error message in the try block
I am trying to populate a JTable with information taken from a website (namely price and item name). I have a class that asks the user to input a URL and scans the page for the price and item name as well as the URL. Currently it takes all the parsed information and stores it in three different text files, one for price, one for item name, and one for the URL. I am trying to populate a JTable containing three columns (item name, price, and URL) with this information but every time I scan a new page the text files are overwritten and the previous information is lost. I don't necessarily need the JTable to be populated via the text file, I just need it to somehow get the information. Here is some of my code.
public BestBuy (JFrame frame){
super (frame, "Best Buy URL", true);
setLayout (new FlowLayout());
label = new JLabel ("Enter Best Buy URL");
add (label);
url = new JTextField ("Enter URL Here", 40);
add (url);
submit = new JButton ("Submit");
add (submit);
event b = new event ();
submit.addActionListener (b);
}
public class event implements ActionListener{
public void actionPerformed (ActionEvent b){
try {
String datab = url.getText(); //perform your operation
datab = datab.trim();
datab = datab.toLowerCase();
Document document = Jsoup.connect(datab).get();
String amountb = document.select(".amount").first().text();
String nameb = document.select(".product-title").first().text();
FileWriter stream = new FileWriter ("C:\\Users\\Daniel\\Desktop\\price.txt");
BufferedWriter out = new BufferedWriter (stream);
out.write(amountb + "\n");
out.newLine();
out.close();
FileWriter stream1 = new FileWriter ("C:\\Users\\Daniel\\Desktop\\itemName.txt");
BufferedWriter out1 = new BufferedWriter (stream1);
out1.write(nameb + "\n");
out1.newLine();
out1.close();
FileWriter stream2 = new FileWriter ("C:\\Users\\Daniel\\Desktop\\url.txt");
BufferedWriter out2 = new BufferedWriter (stream2);
out2.write(datab + "\n");
out2.newLine();
out2.close();
}
catch (Exception ex) {
}
setVisible (false);
}
This class asks the user for a Best Buy URL and parses the given page for item name, and price then writes it to files on my desktop.
public FirstGui (){
setLayout (new FlowLayout ());
String[] columnName = {"Item Name", "Price", "URL"};
Object [] [] data = {
};
table = new JTable (data, columnName);
table.setPreferredScrollableViewportSize(new Dimension (500, 300));
table.setFillsViewportHeight (true);
JScrollPane scrollpane = new JScrollPane (table);
add (scrollpane);
Now I am trying to get that parsed information onto my JTable but I have no idea how to do so. I tried to do
public getdatab() {
return datab;
}
public getnameb() {
return nameb;
}
public getamountb() {
return amountb;
}
but all these strings are within a void so that did not work. As you can probably see I am quite new to java and this might have an obvious solution but I have been stuck on this for a few days and cant figure it out. Thank you.
I'm not sure exactly how your getting your data, but you want to do something like this. Since you're trying to write the data to three different files, I will assume the data is coming in from three different streams. Here's the thing though. For this to work, all the data needs to be in parallel, Meaning that the first item, should correspond to the first price and first url, and so on. If this is the case, you can do something like this.
Have three separate lists.
List<String> names = new ArrayList<String>();
List<String> prices = new ArrayList<String>();
List<String> urls = new ArrayList<String>();
Then for each item you were going to add to a file, add to the list instead.
Use a DefaultTableModel as the model of your JTable
String[] columnName = {"Item Name", "Price", "URL"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
table = new JTable(model);
Now you can just add rows, using the data from the lists. Use the method model.addRow(row), where row is an array of Objects
for (int i = 0; i < names.size(); i++) {
String name = names.get(i);
String price = prices.get(i);
String url = urls.get(i);
model.addRow(new Object[] { name, price, url });
}
That's all there is to it. The model will update the table for you dynamically. But remember, like I said, the data in the lists must be in sync with one another for you to get the desired result.
If you're getting data in one row at a time, instead of one column at a time, that makes it even easier. For each set of data, that comes in, just add it as a row like it did in step 5.