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.
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.