Why two combobox appeares?

Hi, I am plotting a Gui that includes 2d shape, a JSlider and a JComobox.

But two Comobox appears.

Any help, thanks. The main code here.

import java.io.*;

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.Scanner;

import java.awt.Font;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.BasicStroke;

import java.awt.GradientPaint;

import java.awt.geom.GeneralPath;

import java.awt.AlphaComposite;

import java.awt.Composite;

import java.awt.geom.*;

import java.awt.*;

import java.awt.BorderLayout;

import javax.swing.*;

import java.util.Hashtable;

import java.awt.event.*;

import javax.swing.event.*;

publicclass Drawingextends JPanel

{

finalint TRANSITION = 100;// Transition x,y coordinates of cells.

privatedouble x1, y1, x2, y2;// to plot lines from (x1,y1) to (x2,y2)

privatedouble xCells[][] =newdouble[50000][];// Every cell's points's x coordinates array

privatedouble yCells[][] =newdouble[50000][];// Every cell's points's y coordinates array

privatedouble rgb[][]=newdouble[5000][];// Color red, green, blue for each point in each cell

privatedouble red[][]=newdouble[50000][];

privatedouble green[][] =newdouble[50000][];

privatedouble blue[][]=newdouble[50000][];

private String energyId[] =new String[500];

//private String fileName = "plotdata4.txt";

privateint sideId;

privateint sidesTotalNumber;// Total sides number for all cells

privateint rgbLength;// Total rgb color group number for all cells at each point

privateint cellsPointsNumber[] =newint[5000];// point numbers of each cell.(each cell has how many points)

privateint numberofCells;

privateint numberofsets;

privateint id;//PlotEnergyId

privatedouble lineWidth;

double scale = 1.0;

private String currentPattern;

private JSlider dJSlider;

private JComboBox comb;

publicvoid SliderFrame()

{

int min = 1, max = 16, inc = 5;

dJSlider =new JSlider(min, max, 6);

dJSlider.setMajorTickSpacing(5);

dJSlider.setMinorTickSpacing(1);

dJSlider.setSnapToTicks(true);

dJSlider.setLabelTable(getLabelTable(min, max, inc));

dJSlider.setPaintTicks(true);

dJSlider.setPaintLabels(true);

dJSlider.addChangeListener(

new ChangeListener()// anonymous inner class

// handle change in slider value

{

publicvoid stateChanged(ChangeEvent e)

{

int value = ((JSlider)e.getSource()).getValue();

scale = (value+4)/10.0;

}

}

);

add(dJSlider,BorderLayout.EAST);// ADD Slider

}

private Hashtable getLabelTable(int min,int max,int inc){

Hashtable<Integer, JLabel> table =new Hashtable<Integer, JLabel>();

for(int j = min; j <= max; j+=inc){

String s = String.format("%.1f", (j+4)/10.0);

table.put(Integer.valueOf(j),new JLabel(s));

}

return table;

}

publicvoid paintComponent(Graphics g)

{

super.paintComponent( g );// call superclass's paint method

this.setBackground( Color.WHITE );

Graphics2D g2d = (Graphics2D)g;// cast g to Graphics 2D

getDrawingData();

ComboBoxDemo();

repaint();

drawPolygon(g2d);

//Composite originalComposite = g2d.getComposite();

//g2d.setColor(Color.GRAY);

drawSides(g2d);

g2d.setComposite(makeComposite(1.0f));

// g2d.setComposite(originalComposite);

}

// end method paintComponent

private AlphaComposite makeComposite(float alpha)

{

int type = AlphaComposite.SRC_OVER;

return(AlphaComposite.getInstance(type, alpha));

}

publicvoid getDrawingData()

{

TestCell p =new TestCell();

p.cellData();

p.energyId();

p.rgbs();

xCells = p.getxCells();

yCells = p.getyCells();

sidesTotalNumber = p.getsidesTotalNumber();

rgb = p.getrgb();

red = p.getred();

green = p.getgreen();

blue = p.getblue();

energyId = p.getenergyId();

rgbLength = p.getrgbLength();

cellsPointsNumber = p.getcellsPointsNumber();//array: each cell has how many points

numberofCells = p.getnumberofCells();

numberofsets = p.getnumberofsets();

for(int i = 0; i < numberofCells; i++)

{

for(int j = 0; j<cellsPointsNumber[i]; j++)

{

xCells[i][j] = (xCells[i][j])*scale+TRANSITION;

yCells[i][j] = (yCells[i][j])*scale+TRANSITION;

}

}

}

publicvoid drawSides(Graphics2D g2d)

{

g2d.setColor(Color.GRAY);

g2d.setStroke(new BasicStroke((float)lineWidth));

for (int i = 0; i><numberofCells; i++)

{

for (int j = 0; j><cellsPointsNumber[i]-1; j++)

{

x1 = xCells[i][j];

y1 = yCells[i][j];

x2 = xCells[i][j+1];

y2 = yCells[i][j+1];

Line2D line =new Line2D.Double(x1,y1,x2,y2);

g2d.draw(line);// draw Sidess here

}

}

}

publicvoid drawPolygon(Graphics2D g2d)

{

for (int i = 0; i><numberofCells; i++)

{

Polygon poly =new Polygon();

float r = (float)red[i][id];

// red for ith cell,id is from 0 to numberofsets-1

// if id=0, means "Mixture"

float g = (float)green[i][id];// green for ith cell

float b = (float)blue[i][id];// blue for ith cell

List><Integer> xList =new ArrayList<Integer>();

List<Integer> yList =new ArrayList<Integer>();

for (int j = 0; j<cellsPointsNumber[i]; j++)

{

xList.add((int)(xCells[i][j]));

yList.add((int)(yCells[i][j]));

}

Integer[] x = xList.toArray(new Integer[xList.size()]);

Integer[] y = yList.toArray(new Integer[yList.size()]);

for (int s = 0; s><xList.size(); s++)

{

poly.addPoint(x[s],y[s]);

}

g2d.setColor(new Color(r, g, b));// transparent drawing

g2d.fillPolygon(poly);

}

}

publicvoid ComboBoxDemo(){

currentPattern = energyId[0];

JLabel patternLabel1 =new JLabel("Select one set");

comb =new JComboBox(energyId);

comb.setEditable(false);

//comb.addActionListener(this);

comb.setForeground (Color.red);

comb.setBackground (Color.yellow);

comb.add(patternLabel1);

comb.setAlignmentX(Component.LEFT_ALIGNMENT);

comb.addActionListener(

new ActionListener()// anonymous inner class

{

publicvoid actionPerformed(ActionEvent e)

{

JComboBox cb = (JComboBox)e.getSource();

id = cb.getSelectedIndex();

}

}

);

add(comb,BorderLayout.WEST);

setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

}

}// end class Drawing

import java.awt.*;

import javax.swing.JFrame;

publicclass TestMain

{

publicstaticvoid main(String args[])

{

JFrame frame =

new JFrame("SCALE 5 TRITON/NEWT" );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

Drawing drawing =new Drawing();

drawing.setLayout(new FlowLayout());

drawing.SliderFrame();

//drawing.ComboBoxDemo();

//add(drawing, BorderLayout.CENTER);

//drawing.paintComponent();

drawing.setBackground( Color.WHITE );

//comb.setLayout(new FlowLayout());

frame.add( drawing, BorderLayout.CENTER );// add panel to frame

frame.setSize( 680, 800 );// set frame size

frame.setVisible(true );// display

}

}

>

[15537 byte] By [ardmorea] at [2007-11-27 9:56:01]
# 1

Bad design.

paintComponent (which may be called many, many times) calls

ComboBoxDemo which creates a new combo box every time and

adds it to the panel.

Suggestion: separate painting from building the GUI.

When I write custom components that draw graphics,

I never add components to them, because it doesn't

look right. Instead I create a panel to hold the drawing

component and its assorted controls. But YMMV.

BigDaddyLoveHandlesa at 2007-7-13 0:26:05 > top of Java-index,Java Essentials,Java Programming...
# 2
Would you please provide more details for my design?Such as...I feel it is a little hard.
ardmorea at 2007-7-13 0:26:05 > top of Java-index,Java Essentials,Java Programming...
# 3

Setting aside whether or not components should be added to your Drawing class,

comment out code so that you have only this for paintComponent:

protected void paintComponent(Graphics g) {

super.paintComponent(g);

//other code commented out

}

Now, when you run your app, the drawing component isn't going to

draw a grid or whatever its task is, but you should still see your other

controls, like the elusive combo box. If you don't see a control, you

need to edit the code that lays out the components.

BigDaddyLoveHandlesa at 2007-7-13 0:26:05 > top of Java-index,Java Essentials,Java Programming...