Hey, so here's an example of what I want to do:
This "window" if you will, or simple gui, will accept an ArrayList<Strin g> and in turn spit out for every one of them a checkbox and a text field where you can rename it to whatever name you chose (later I'll add something to check and make sure none of the names are the same... but for now it doesn't matter).
I'm not very familiar with GUI in java so I don't know the best way of doing this. I'm not asking for code, but simply some help in the right direction. I've downloading all of java swing's tutorial demos and examples and have looked at just about every component and layout example in there. Still I don't feel like I know what I'm doing and you'll see in my code soon what I mean.
Before I do I want to show you guys what I want the gui to look like in a sense:
Here's what I've got so far with the code, I tried to put stuff into JPanels and then add them to the gui later:
[code=java]
import java.awt.*;
import java.awt.event. *;
import javax.swing.*;
import java.util.*;
public class TagChoosingWind ow implements ItemListener {
JTextField[] textFields;
JCheckBox[] checkBoxes;
JPanel[] panels;
short[] included;
ArrayList<Strin g> tags;
public TagChoosingWind ow(ArrayList<St ring> al) {
tags = al;
int size = tags.size();
included = new short[size];
textFields = new JTextField[size];
checkBoxes = new JCheckBox[size];
panels = new JPanel[size];
for (int i = 0; i < size; i++) {
checkBoxes[i] = new JCheckBox(tags. get(i) + " : ");
checkBoxes[i].setSelected(tr ue);
checkBoxes[i].addItemListene r(this);
textFields[i] = new JTextField(tags .get(i));
included[i] = 1;
panels[i] = new JPanel(new BoxLayout(panel s[i], BoxLayout.LINE_ AXIS));
panels[i].add(checkBoxes[i]);
panels[i].add(textFields[i]);
}
}
private void buildUI(Contain er container) {
container.setLa yout(new BoxLayout(conta iner, BoxLayout.PAGE_ AXIS));
for (int i = 0; i < panels.length; i++) {
container.add(p anels[i]);
}
}
public void itemStateChange d(ItemEvent e) {
Object source = e.getItemSelect able();
int index = -1;
for (int i = 0; i < checkBoxes.leng th; i++) {
if (source == checkBoxes[i]) {
index = i;
break;
}
}
if (e.getStateChan ge() == ItemEvent.SELEC TED) {
textFields[index].setEnabled(tru e);
included[index] = 1;
} else {
textFields[index].setEnabled(fal se);
included[index] = 0;
}
}
private void createAndShowGU I() {
//Create and set up the window.
JFrame frame = new JFrame("Choose What Data Tags You Want and Name Them");
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
//Set up the content pane.
TagChoosingWind ow tagchoosingwind ow = new TagChoosingWind ow(tags);
tagchoosingwind ow.buildUI(fram e.getContentPan e());
//Display the window.
frame.pack();
frame.setVisibl e(true);
}
public static void main(String args[]) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.Swi ngUtilities.inv okeLater(new Runnable() {
public void run() {
ArrayList<Strin g> temp = new ArrayList<Strin g>(2);
temp.add("First Tag\\");
temp.add("Secon d Tag\\");
TagChoosingWind ow tagchoosingwind ow = new TagChoosingWind ow(temp);
tagchoosingwind ow.createAndSho wGUI();
}
});
}
}
[/code]
Oh and thank you for all help, it's much appreciated,
-blazed
This "window" if you will, or simple gui, will accept an ArrayList<Strin g> and in turn spit out for every one of them a checkbox and a text field where you can rename it to whatever name you chose (later I'll add something to check and make sure none of the names are the same... but for now it doesn't matter).
I'm not very familiar with GUI in java so I don't know the best way of doing this. I'm not asking for code, but simply some help in the right direction. I've downloading all of java swing's tutorial demos and examples and have looked at just about every component and layout example in there. Still I don't feel like I know what I'm doing and you'll see in my code soon what I mean.
Before I do I want to show you guys what I want the gui to look like in a sense:
Code:
ArrayList<String> temp = new ArrayList<String>(2);
temp.add("First Tag\\");
temp.add("Second Tag\\");
If above is the arraylist I would want the gui to look something like this:
Note: ---- represents the border of the window, [] represents a checkbox, text represents non-editable text and "text" represents editable text
Well I didn't mention this earlier but the point of the checkboxes is to not include those strings, when I return an arraylist later, but that's not important now. The gui showing up is coming first...
----------------------------------------------
- []First Tag: "First Tag" -
- []Second Tag: "Second Tag" -
----------------------------------------------
Of course with a title and minimize/maximize/close buttons there too, but they come automatically...
[code=java]
import java.awt.*;
import java.awt.event. *;
import javax.swing.*;
import java.util.*;
public class TagChoosingWind ow implements ItemListener {
JTextField[] textFields;
JCheckBox[] checkBoxes;
JPanel[] panels;
short[] included;
ArrayList<Strin g> tags;
public TagChoosingWind ow(ArrayList<St ring> al) {
tags = al;
int size = tags.size();
included = new short[size];
textFields = new JTextField[size];
checkBoxes = new JCheckBox[size];
panels = new JPanel[size];
for (int i = 0; i < size; i++) {
checkBoxes[i] = new JCheckBox(tags. get(i) + " : ");
checkBoxes[i].setSelected(tr ue);
checkBoxes[i].addItemListene r(this);
textFields[i] = new JTextField(tags .get(i));
included[i] = 1;
panels[i] = new JPanel(new BoxLayout(panel s[i], BoxLayout.LINE_ AXIS));
panels[i].add(checkBoxes[i]);
panels[i].add(textFields[i]);
}
}
private void buildUI(Contain er container) {
container.setLa yout(new BoxLayout(conta iner, BoxLayout.PAGE_ AXIS));
for (int i = 0; i < panels.length; i++) {
container.add(p anels[i]);
}
}
public void itemStateChange d(ItemEvent e) {
Object source = e.getItemSelect able();
int index = -1;
for (int i = 0; i < checkBoxes.leng th; i++) {
if (source == checkBoxes[i]) {
index = i;
break;
}
}
if (e.getStateChan ge() == ItemEvent.SELEC TED) {
textFields[index].setEnabled(tru e);
included[index] = 1;
} else {
textFields[index].setEnabled(fal se);
included[index] = 0;
}
}
private void createAndShowGU I() {
//Create and set up the window.
JFrame frame = new JFrame("Choose What Data Tags You Want and Name Them");
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
//Set up the content pane.
TagChoosingWind ow tagchoosingwind ow = new TagChoosingWind ow(tags);
tagchoosingwind ow.buildUI(fram e.getContentPan e());
//Display the window.
frame.pack();
frame.setVisibl e(true);
}
public static void main(String args[]) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.Swi ngUtilities.inv okeLater(new Runnable() {
public void run() {
ArrayList<Strin g> temp = new ArrayList<Strin g>(2);
temp.add("First Tag\\");
temp.add("Secon d Tag\\");
TagChoosingWind ow tagchoosingwind ow = new TagChoosingWind ow(temp);
tagchoosingwind ow.createAndSho wGUI();
}
});
}
}
[/code]
Oh and thank you for all help, it's much appreciated,
-blazed
Comment