Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JButton & JComboBox 1

Status
Not open for further replies.

rbr19

Programmer
Jun 2, 2004
4
GB
hi all,

i'm new to this forum & also to java programming. i have a problem i.e. i'm not sure how to get the jbutton to obtain the necessary texts displayed on the jcombobox & 'paint' them. the texts are supposed to be 'painted' vertically i.e. rotated anticlockwise 90 degrees.
basically, i have a few editable jcomboboxes & jbuttons. the jcomboboxes are used to contain a list of materials where the user would choose. when the user presses the 'plot' jbutton, it is supposed to get the text from the jcombobox & 'paint' it inside the rectangle (the rectangles shouldn't be there initially, it will be 'painted' simultaneously with the texts). this process will go on & on till the user ends the programme by pressing the end jbutton.
below is a shortened version of my code:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Testing extends JFrame implements ActionListener
{

private String x[] ={ " ", "MaterialA", "MaterialB", "Material C"};
private String y[] = { " ", "+5", "+4", "+3", "+2", "+1", " 0", "-1", "-2", "-3", "-2", "-5" };



private JButton Back, Next , Plot, End;
private Container c;
private JPanel labelPanel, northPanel, southPanel;
private JComboBox designMaterialx, materialRatingy;


public Testing()
{
super( "Welcome" );
setSize( 800, 700 );

Container c = getContentPane();




northPanel = new JPanel();
northPanel.add(new Label( "Please choose accordingly:" ));
northPanel.setLayout (new FlowLayout(FlowLayout.CENTER, 20, 0));

designMaterialx = new JComboBox( x );
designMaterialx.setMaximumRowCount( 2 );
designMaterialx.setBorder(BorderFactory.createTitledBorder("Design Materials"));
northPanel.add( designMaterialx );
designMaterialx.setEditable(true);

materialRatingy = new JComboBox( y );
materialRatingy.setMaximumRowCount( 2 );
materialRatingy.setBorder(BorderFactory.createTitledBorder("Material Ratings"));
northPanel.add( materialRatingy );


c.add( northPanel, BorderLayout.NORTH );
setVisible( true );



southPanel = new JPanel();

Back = new JButton( "Back" );
Next = new JButton( "Next" );
Plot = new JButton( "Plot" );
End = new JButton( "End" );

southPanel.add( Back );
southPanel.add( Next );
southPanel.add( Plot );
southPanel.add( End );

c.add( southPanel, BorderLayout.SOUTH );
setVisible( true );

Back.addActionListener(this);
Next.addActionListener(this);
Plot.addActionListener(this);
End.addActionListener(this);


}



public void paint( Graphics g )
{
super.paint( g );


g.setColor(Color.black);
g.drawRect(120, 120, 20, 200);
g.drawString ("Material", 30, 220);
g.drawString ("Ratings", 30, 240);
g.drawString ("+5", 102, 130);
g.drawString ("0", 107, 220);
g.drawString ("-5", 102, 315);


g.setColor(Color.red);
g.drawRect(120, 321, 20, 200);
g.drawString ("Design", 30, 410);
g.drawString ("Materials", 30, 430);

}


public static void main( String args[] )
{
Testing app = new Testing();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}

public void actionPerformed(ActionEvent e)
{

if(e.getSource()==Back)
{

System.out.println("Back");
}

else if(e.getSource()==Plot)

{
System.out.println("Plot");
}

else if(e.getSource()==Next)
{

System.out.println("Next");
}

else if(e.getSource()==End)
{

System.exit(0);
}
}

}


any advice, feedback, tips or codes similar with what i intend to do is very much appreciated.

thanks in advance.
 
Code:
//CenterCanvas should be a square so that rotating 90 degree will be a square too.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JInternalFrame;
import java.awt.geom.AffineTransform;
import java.awt.Graphics2D;
import java.awt.geom.*;
import javax.swing.DesktopManager;
class WholeFrame extends JFrame implements ActionListener
      {
private String  x[] ={ " ", "MaterialA", "MaterialB",  "Material C"};
private String  y[] = { "                       ", "+5", "+4", "+3", "+2", "+1", " 0", "-1", "-2", "-3", "-2", "-5" };
private JPanel labelPanel, northPanel, southPanel;
private JComboBox designMaterialx, materialRatingy;  
    private JButton Back, Next , Plot, End;

       JPanel topPanel, leftPanel, bottomPanel;
       CenterCanvas cc;
       LeftCanvas lc;
       public WholeFrame() 
              {
               super("WholeFrame");
               getContentPane().setLayout(null);
               topPanel = new JPanel();
               topPanel.setBackground(Color.blue);
               topPanel.setBounds(0,0,800,100);
               getContentPane().add(topPanel);

               lc = new LeftCanvas();
               lc.setBounds(0,100,150,700);
               getContentPane().add(lc);

               cc = new CenterCanvas();
               cc.setBounds(150,100,500,500);
               getContentPane().add(cc);
               cc.setBackground(Color.red);

               bottomPanel = new JPanel();
               bottomPanel.setBounds(150,600,650,100);
               getContentPane().add(bottomPanel);
               bottomPanel.setBackground(Color.green);
               Back = new JButton("Back");
               Plot = new JButton("Plot");
               Back.addActionListener(this);
               Plot.addActionListener(this);
               bottomPanel.setLayout(new GridLayout(1,3));
               bottomPanel.add(Back);
               bottomPanel.add(Plot);

      Container c = getContentPane(); 
      topPanel.setLayout(new GridLayout(1,3));
      topPanel.add(new Label( "Please choose accordingly:" ));

designMaterialx = new JComboBox( x );
designMaterialx.setMaximumRowCount( 2 );
designMaterialx.setBorder(BorderFactory.createTitledBorder("Design Materials"));
topPanel.add( designMaterialx );
designMaterialx.setEditable(true);
               }

   public void actionPerformed(ActionEvent e)
   {
       
       if(e.getSource()==Back)
       {
           
           System.out.println("Back");
       }
       
       else if(e.getSource()==Plot)
 
       {
           System.out.println("Plot");
           cc.changeMessage( designMaterialx.getSelectedItem().toString() );
       }
       
       else if(e.getSource()==Next)
       {
       
           System.out.println("Next");
       }
       
       else if(e.getSource()==End)
       {
       
           System.exit(0);
       }
   }

       public static void main(String args[])
              {
              WholeFrame wholeFrameObj = new WholeFrame();              
              wholeFrameObj.setSize(800,700);
              wholeFrameObj.setVisible(true);
              wholeFrameObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              }
      }

class CenterCanvas extends Canvas
      {
       String messageToChange = "zzzzzz";
       boolean timeToChange = false;
       public void changeMessage(String temp)
              {
               messageToChange = temp;
               timeToChange = true;
               repaint();
              }
       public void paint(Graphics g)
              {
    Graphics2D g2 = (Graphics2D)g;
    //Update transform to provide rotation and display, 
    // the transform values.    
    System.out.println("Add Rotate Transform");
    //11.25 degrees about center
    g2.rotate(Math.PI/2.0,250.0,250.0);

g.setColor(Color.black);
g.drawRect(120, 120, 20, 200);
g.drawString ("Material", 30, 220);
g.drawString ("Ratings", 30, 240);
g.drawString ("+5", 102, 130);
g.drawString ("0", 107, 220);
g.drawString ("-5", 102, 315);

        
g.setColor(Color.black);
g.drawRect(120, 321, 20, 200);
g.drawString ("Design", 30, 410);
if (!timeToChange)
   g.drawString ("Materials", 30, 430);
else
    {
    g.drawString (messageToChange, 30, 430);
    timeToChange = false;
    }
              }

      
      }
class LeftCanvas extends Canvas
      {
       public void paint(Graphics g)
              {
g.setColor(Color.black);
g.drawRect(120, 120-100, 20, 200);
g.drawString ("Material", 30, 220);
g.drawString ("Ratings", 30, 240);
g.drawString ("+5", 102, 130-100);
g.drawString ("0", 107, 220-100);
g.drawString ("-5", 102, 315-100);

        
g.setColor(Color.red);
g.drawRect(120, 321-100, 20, 200);
g.drawString ("Design", 30, 410-100);
g.drawString ("Materials", 30, 430-100);
              }
      }
 
hi prosper,

thanks for replying. i really appreciate it. i've read your code & also ran it a few times to figure out what you have added & modified to my code. i've learned something new from it but the addition of the CenterCanvas is not what i planned to do actually.

the value selected in the design materials jcombo box is the one that is supposed to be rotated (90 degrees anti-clockwise) & painted in the g.drawRect(120, 321-100, 20, 200) rectangles upon the clicking of the plot jbutton. this rectangle & the new value of the jcombo box will keep 'adding up' i.e. new values & rectangles will be added next to the existing ones as long as the plot button is clicked. i assume i have to 'play around' with the coordinates to get the rectangles & those values painted again & again. the g.drawString ("Material", 30, 220);
g.drawString ("Ratings", 30, 240); g.drawString ("Design", 30, 410); g.drawString ("Materials", 30, 430); should only be painted once i.e. when the plot jbutton is initially pressed. subsequent pressing of the plot jbutton should not paint those strings anymore.


i hope you or anyone in this forum could tell or advise me on how to do that. any kind of help is appreciated.

thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top