We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › JComboBoxes inside ArrayList
Page Index Toggle Pages: 1
JComboBoxes inside ArrayList (Read 594 times)
JComboBoxes inside ArrayList
Nov 11th, 2009, 11:56pm
 
  Hello! Please, need help - can't tie arraylist of JComboBoxes to arraylist of graphic objects to choose color for each of objects with same index. More exactly - each JComboBox changes colors of all objects and not for separate, as I need. Maybe it is to tune inside void draw() function ???

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.UIManager;
import java.util.ArrayList.*;
import javax.swing.*;
import java.awt.BorderLayout.*;
import java.awt.*;
import java.awt.event.*;
PGraphics originalG;
PGraphics bufferG;
color combocolor;
int arcs=5;
int segheight=15;
int segwidth = 50;
String string1;
String string111 = "3";
JTextField segmentts;
ArrayList  parts, strings,labelperc,labelnames,fieldss,myarcs,combos;
int x = 25;
int index = 0;
String[] colors = { "Red",  "Green","Blue"};
Panel p1;
Myarc myarc;
float yyy;
void setup() {
 strings = new ArrayList(10);
 labelperc = new ArrayList(10);
 fieldss = new ArrayList(10);
 combos = new ArrayList(10);
 myarcs = new ArrayList(arcs);
 size(1100,700,P3D);
 bufferG = createGraphics(180,700,JAVA2D);
 p1 = new Panel();
 p1.setLocation(0,0);
 p1.setLayout(new BorderLayout(0,0));
 p1.setBounds(0,0,180,700);
 p1.setBackground(new Color(20,135,235));
 segmentts = new JTextField(string111);
 segmentts.setVisible(true );
 segmentts.setLocation(new Point(85, 10));
 segmentts.setSize(new Dimension(60, 20));
 segmentts.setText("3") ;
 JLabel quant = new JLabel("Quantity");
 quant.setVisible(true );
 quant.setSize(new Dimension(50, 20));
 quant.setLocation(new Point(20, 10));
 p1.add(segmentts);
 p1.add(quant);
 p1.show();
 for(int i =0; i<=10;i++){
  fieldss.add(new JTextField("20"));
   labelperc.add(new JLabel("%"));
   combos.add(new JComboBox(colors));
   strings.add(new String("si"));
   JTextField segment333 = (JTextField)fieldss.get(i);
   segment333.setLocation(new Point(90, 10+35+x*i));
   segment333.setSize(new Dimension(20, 20));
   segment333.setVisible(false );
   segment333.setEditable(false );
   JLabel labelii = (JLabel)labelperc.get(i);
   labelii.setSize(new Dimension(30, 10));
   labelii.setLocation(new Point(75, 45+x*i));
   labelii.setVisible(false );
   JComboBox tempcombo = (JComboBox)combos.get(i);
   tempcombo.setSize(new Dimension(40, 20));
   tempcombo.setLocation(new Point(130, 45+x*i));
   tempcombo.setVisible(false );
   p1.add(segment333); p1.add(labelii);p1.add(tempcombo);
   JComboBox ttempcombo = (JComboBox)combos.get(i);
   ttempcombo.addItemListener(
   new ItemListener() {
     public void itemStateChanged(ItemEvent e) {
       String s = (String)e.getItem();
       Object item = e.getItem();
       if (e.getStateChange() == ItemEvent.SELECTED) {}
       if (item.equals("Red")) {
         index = 0;
         combocolor= color(255,0,0);}
       else if (item.equals("Green")) {
         combocolor  = color(0,255,0);}
       else if (item.equals("Blue")) {
         combocolor = color(0,0,255);} }});}
 segmentts.addActionListener(
 new ActionListener() {
   public void actionPerformed(ActionEvent ee) {
     if( segmentts!=null && !segmentts.getText().equals("") &&
       int(Float.parseFloat(segmentts.getText())) >0 &&
       int(Float.parseFloat(segmentts.getText()))<=11) {
       try{
         arcs = int(Float.parseFloat(segmentts.getText()));
         myarcs = new ArrayList(arcs);
         for(int i =0; i<arcs;i++){
           JTextField tempfiled1 = (JTextField)fieldss.get(i);
           tempfiled1.setVisible(true );
           tempfiled1.setEditable(true );
          JLabel labelii = (JLabel)labelperc.get(i);
           labelii.setVisible(true );  
           JComboBox tempcombo = (JComboBox)combos.get(i);
           tempcombo.setVisible(true );
         }
         for(int i =arcs; i<11;i++){
           JTextField tempfiled1 = (JTextField)fieldss.get(i);
           tempfiled1.setVisible(false );
           tempfiled1.setEditable(true );
          JLabel labeli = (JLabel)labelnames.get(i);
           labeli.setVisible(false );
           JComboBox tempcombo = (JComboBox)combos.get(i);
           tempcombo.setVisible(false);}}
       catch(Exception e) {}}}});
 setLayout(new BorderLayout(0,0));
 add(p1);originalG = g;   }
void useBufferG() {g.endDraw();g = bufferG; g.beginDraw();}
void useOriginalG() {g.endDraw();g = originalG;g.beginDraw();}
void draw() {
 useOriginalG();
 background(235,235,235);
 stroke(0,0,255);
 useBufferG();
 useOriginalG();
 image(bufferG,180,0);
 background(255,255,255);
 for(int i = arcs-1 ; i>=0;i--){
   JTextField tempfiled1 = (JTextField)fieldss.get(i);
   JTextField tempfiled1plus1 = (JTextField)fieldss.get(i+1);
   if( tempfiled1!=null && !tempfiled1.getText().equals("")){
     try{
       String t1 =  tempfiled1.getText();
       String plus1 =  tempfiled1plus1.getText();
       float yyy = Float.parseFloat(t1);
       float plus11 = Float.parseFloat(t1);
       ellipseMode(CENTER);
       translate(180,100,0);
       Myarc  myarc= new Myarc(0, 0, segwidth, segwidth, radians(0), radians(3.6*yyy),combocolor);
       myarcs.add(myarc);
       myarc.drawarc();
       myarc.updarc();
     }
     catch(Exception e) {}}}}
class Myarc{
 float x=0;float y=0;
 float d=0;float a=0;float b;float v;color arccol = color(0,255,0);
 Myarc(float x, float y, float d, float a, float b, float v, color arccol){
   this.x=x;this.y=y;this.d=d;this.a=a;this.b=b;this.v=v;this.arccol=arccol;
 }
 void drawarc(){
   fill(arccol);  arc(x, y, d, a, b, v );pushMatrix();translate(0,0,-10);arc(x, y, d, a, b, v );popMatrix();}
 void updarc(){
   fill(arccol);  arc(x, y, d, a, b, v );pushMatrix();translate(0,0,-10);arc(x, y, d, a, b, v ); popMatrix();}
}


Page Index Toggle Pages: 1