Please help me in JTextArea
Hi,
Is there any way of constructing a JTextArea with the specified number of rows and columns(say 4 rows and 30 columns).
I tried with JTextArea(4,30). But in this case the visible area only is 4 rows. Is there any way to construct a JTextArea which can limit my entry to 4 rows in all occasions(irrespective of whatever may the capacity)
e.g.
The Text can contain
Line no.1
no.2
no.3
no. 4
If I press enter after this should restrict my entry
Well there is no real way to predict what will happen when the next character is added to the text area. Also you requirement isn't complete. For example you don't state whether you are using word or character wrapping on the text area. A couple of suggestions.
If you use a monospaced font, then the maximum number of characters that can be displayed is rows * columns or 120 in your example. The Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter]General Text Component Features show you how to use a DocumentFilter to limit the number of characters added to a Document,
Or maybe you can use a DocumentListerner. An event gets fired after every addition to the Document. You could then use the modelToView(...) method of the text area to determine the view positon of the last character in the Document. If the Y position is greater than 4 * the row height then you know it is painted on the 5th line and you would need to undo the last insertion.
Similar Messages
-
Really need help in printing JTextArea. Please help me
Hi,
i want to print the content of JTextarea which span more than 1 page.i have tried all suggestion in the forums but still have the same result.
it only print the first page.
Please help me.
attached is the code for printing:
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
/* get component width and table height */
Dimension dimension = componentToBePrinted.getSize();
double compWidth = dimension.width;
double compHeight = dimension.height;
/* get page width and page height */
double pageWidth = pageFormat.getImageableWidth();
double pageHeight = pageFormat.getImageableHeight();
double scale = pageWidth / compWidth;
/* calculate the no. of pages to print */
final int totalNumPages= (int) Math.ceil( ( scale * compHeight ) / pageHeight );
if (pageIndex > totalNumPages) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.translate( 0f, 0f );
g2.translate( 0f, -pageIndex * pageHeight );
g2.scale( scale, scale );
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
/** Re-enables double buffering globally. */
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
THANKSUse freely downloadable smart jprint class AtDocumentPrinter from http://www.activetree.com. This package alos allows you to print contents of any kind of JTextComponent such as JTextField, JTextArea, JEditorPane, and JTextPane.
It breaks the text, images and forms into multiple pages by breaking horizontally and vertically. JTable printing is specially interesting. -
Please help me in creating a form using Swing.
(I m sorry if I've posted similar post in some other thread.I am new to this forum..:))
There are several problems which I am facing while coding a form.
1. In the form I have one JComboBox
of Country and other of State. Now I want it in such a way that when
I select a country from the Country JComboBox ,the corresponding states
automatically appears in the State JComboBox.
2. I need to add picture frame in the 6th tab so that the picture shows up when clicked browse and open.
3.I need to add tables in 1st and 5th tab which shows up the details through database when added through several textboxes and combo boxes.
so please help me!
Here's my code
==========================================================
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.Box;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Design extends JFrame
public Design()
super("Tenant Form");
setLayout(new BorderLayout());
JTabbedPane tabbedPane=new JTabbedPane(); //creating tabbed buttons
//<--------coding for first tab--------->
JPanel panel1=new JPanel();
tabbedPane.addTab("Detail of Landlord",null,panel1,"first tab"); //here panel1 is created for tab1
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(2,2,2,2);
gbc.anchor=GridBagConstraints.WEST;
JPanel jp11 = new JPanel(); //1st panel in 1st tab for top labels and buttons
jp11.setLayout(new GridBagLayout());
JLabel l241 = new JLabel("Name Of LandLord");
jp11.add(l241,gbc);
JComboBox jc01=new JComboBox();
jc01.addItem("Select");
jc01.addItem("Mr.");
jc01.addItem("Mrs.");
gbc.gridx=1;
jp11.add(jc01,gbc);
JTextField f01=new JTextField(10);
gbc.gridx=2;
jp11.add(f01,gbc);
JLabel l251 = new JLabel("Sex");
gbc.gridx=5;
gbc.insets=new Insets(2,20,2,2);
jp11.add(l251,gbc);
JComboBox jc11=new JComboBox();
jc11.addItem("Select");
jc11.addItem("Male");
jc11.addItem("Female");
gbc.gridx=6;
gbc.insets=new Insets(2,2,2,2);
jp11.add(jc11,gbc);
JLabel l261 = new JLabel("Age(Yrs)");
gbc.gridx=8;
gbc.insets=new Insets(2,20,2,2);
jp11.add(l261,gbc);
JTextField f11=new JTextField(3);
gbc.gridx=9;
gbc.insets=new Insets(2,2,2,2);
jp11.add(f11,gbc);
JLabel l271 = new JLabel("Occupation");
gbc.gridx= 11;
gbc.insets=new Insets(2,20,2,2);
jp11.add(l271,gbc);
JComboBox jc21=new JComboBox();
jc21.addItem("Select");
jc21.addItem("Engineer");
jc21.addItem("Business");
gbc.gridx=12;
gbc.insets=new Insets(2,2,2,2);
jp11.add(jc21,gbc);
JButton ab1=new JButton("ADD");
gbc.gridx=14;
gbc.insets=new Insets(2,20,2,2);
jp11.add(ab1,gbc);
panel1.add(jp11);
//<--coding for adding table with scroll pane #yet to be coded#-->
JTable jtab1=new JTable();
//start of p21 panel. 1st of two titledborder panels in tab 1
JPanel jp21=new JPanel();
jp21.setBorder(new TitledBorder("Address Of Landlord Property"));
jp21.setLayout(new GridBagLayout());
JLabel l11=new JLabel("Property/House/Building Address");
gbc.gridx=0;
gbc.gridy=0;
jp21.add(l11,gbc);
JTextArea ta11=new JTextArea(3,15);
ta11.setLineWrap(true);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
JScrollPane scroll11=new JScrollPane(ta11,v,h);
gbc.gridx=1;
jp21.add(scroll11);
jp21.add(ta11,gbc);
JLabel l21=new JLabel("Land Mark");
gbc.gridx=0;
gbc.gridy=1;
gbc.ipady=0;
gbc.gridheight=1;
jp21.add(l21,gbc);
JTextField tf21=new JTextField(15);
gbc.gridx=1;
gbc.gridy=1;
jp21.add(tf21,gbc);
JLabel l31=new JLabel("Country");
gbc.gridx=0;
gbc.gridy=2;
jp21.add(l31,gbc);
JComboBox c11=new JComboBox();
c11.addItem("India");
c11.addItem("US");
c11.addItem("Australia");
gbc.gridx=1;
gbc.gridy=2;
jp21.add(c11,gbc);
JLabel l41=new JLabel("State");
gbc.gridx=0;
gbc.gridy=3;
jp21.add(l41,gbc);
JComboBox c21=new JComboBox();
c21.addItem("Rajasthan");
c21.addItem("Delhi");
c21.addItem("Maharastra");
gbc.gridx=1;
gbc.gridy=3;
jp21.add(c21,gbc);
JLabel l51=new JLabel("District");
gbc.gridx=0;
gbc.gridy=4;
jp21.add(l51,gbc);
JComboBox c31=new JComboBox();
c31.addItem("jaipur");
c31.addItem("ajmer");
c31.addItem("alwar");
gbc.gridx=1;
gbc.gridy=4;
jp21.add(c31,gbc);
JLabel l61=new JLabel("City/Town");
gbc.gridx=0;
gbc.gridy=5;
jp21.add(l61,gbc);
JTextField tf31=new JTextField(15);
gbc.gridx=1;
gbc.gridy=5;
jp21.add(tf31,gbc);
JLabel l71=new JLabel("Police District");
gbc.gridx=0;
gbc.gridy=6;
jp21.add(l71,gbc);
JComboBox c41=new JComboBox();
c41.addItem("Jaipur");
c41.addItem("Alwar");
c41.addItem("Ajmer");
gbc.gridx=1;
gbc.gridy=6;
jp21.add(c41,gbc);
JLabel l81=new JLabel("Police Circle");
gbc.gridx=0;
gbc.gridy=7;
jp21.add(l81,gbc);
JComboBox c51=new JComboBox();
c51.addItem("India");
c51.addItem("US");
c51.addItem("Australia");
gbc.gridx=1;
gbc.gridy=7;
jp21.add(c51,gbc);
JLabel l91=new JLabel("Police station");
gbc.gridx=0;
gbc.gridy=8;
jp21.add(l91,gbc);
JComboBox c61=new JComboBox();
c61.addItem("Bani Park");
c61.addItem("Raja Park");
c61.addItem("Malviya Nagar");
gbc.gridx=1;
gbc.gridy=8;
jp21.add(c61,gbc);
JLabel l101=new JLabel("Pin No.");
gbc.gridx=0;
gbc.gridy=9;
jp21.add(l101,gbc);
JTextField tf41=new JTextField(15);
gbc.gridx=1;
gbc.gridy=9;
jp21.add(tf41,gbc);
JLabel l111=new JLabel("Phone No.(R)");
gbc.gridx=0;
gbc.gridy=10;
jp21.add(l111,gbc);
JTextField tf51=new JTextField(15);
gbc.gridx=1;
gbc.gridy=10;
jp21.add(tf51,gbc);
//start of p31.2nd of two titledborder panels in tab 1
JPanel jp31=new JPanel();
jp31.setBorder(new TitledBorder("Address Of Landlord Office"));
jp31.setLayout(new GridBagLayout());
JLabel l121=new JLabel("Office Address");
gbc.gridx=0;
gbc.gridy=0;
jp31.add(l121,gbc);
JTextArea ta61=new JTextArea(3,15);
ta61.setLineWrap(true);
JScrollPane scroll2=new JScrollPane(ta61);
gbc.gridx=1;
gbc.gridy=0;
jp31.add(ta61,gbc);
JLabel l131=new JLabel("Country");
gbc.gridx=0;
gbc.gridy=1;
gbc.gridheight=1;
jp31.add(l131,gbc);
JComboBox c12=new JComboBox();
c12.addItem("India");
c12.addItem("US");
c12.addItem("Australia");
gbc.gridx=1;
gbc.gridy=1;
jp31.add(c12,gbc);
JLabel l141=new JLabel("State");
gbc.gridx=0;
gbc.gridy=2;
jp31.add(l141,gbc);
JComboBox c22=new JComboBox();
c22.addItem("Rajasthan");
c22.addItem("Delhi");
c22.addItem("Maharastra");
gbc.gridx=1;
gbc.gridy=2;
jp31.add(c22,gbc);
JLabel l151=new JLabel("District");
gbc.gridx=0;
gbc.gridy=3;
jp31.add(l151,gbc);
JComboBox c32=new JComboBox();
c32.addItem("jaipur");
c32.addItem("ajmer");
c32.addItem("alwar");
gbc.gridx=1;
gbc.gridy=3;
jp31.add(c32,gbc);
JLabel l161=new JLabel("City/Town");
gbc.gridx=0;
gbc.gridy=4;
jp31.add(l161,gbc);
JTextField tf71=new JTextField(15);
gbc.gridx=1;
gbc.gridy=4;
jp31.add(tf71,gbc);
JLabel l171=new JLabel("Police District");
gbc.gridx=0;
gbc.gridy=5;
jp31.add(l171,gbc);
JComboBox c42=new JComboBox();
c42.addItem("Jaipur");
c42.addItem("Alwar");
c42.addItem("Ajmer");
gbc.gridx=1;
gbc.gridy=5;
jp31.add(c42,gbc);
JLabel l181=new JLabel("Police Circle");
gbc.gridx=0;
gbc.gridy=6;
jp31.add(l181,gbc);
JComboBox c52=new JComboBox();
c52.addItem("India");
c52.addItem("US");
c52.addItem("Australia");
gbc.gridx=1;
gbc.gridy=6;
jp31.add(c52,gbc);
JLabel l191=new JLabel("Police station");
gbc.gridx=0;
gbc.gridy=7;
jp31.add(l191,gbc);
JComboBox c62=new JComboBox();
c62.addItem("Bani Park");
c62.addItem("Raja Park");
c62.addItem("Malviya Nagar");
gbc.gridx=1;
gbc.gridy=7;
jp31.add(c62,gbc);
JLabel l201=new JLabel("Pin No.");
gbc.gridx=0;
gbc.gridy=8;
jp31.add(l201,gbc);
JTextField tf81=new JTextField(15);
gbc.gridx=1;
gbc.gridy=8;
jp31.add(tf81,gbc);
JLabel l211=new JLabel("Phone No.(O)");
gbc.gridx=0;
gbc.gridy=9;
jp31.add(l211,gbc);
JTextField tf91=new JTextField(15);
gbc.gridx=1;
gbc.gridy=9;
jp31.add(tf91,gbc);
JLabel l221=new JLabel("Phone No.(M)");
gbc.gridx=0;
gbc.gridy=10;
gbc.gridheight=1;
jp31.add(l221,gbc);
JTextField tf101=new JTextField(15);
gbc.gridx=1;
gbc.gridy=10;
jp31.add(tf101,gbc);
JLabel l231=new JLabel("E-mail");
gbc.gridx=0;
gbc.gridy=11;
gbc.gridheight=1;
jp31.add(l231,gbc);
JTextField tf111=new JTextField(15);
gbc.gridx=1;
gbc.gridy=11;
jp31.add(tf111,gbc);
JPanel jp41=new JPanel(); //adding above two panels p21 and p31 to p41 panel.
jp41.setLayout(new BoxLayout(jp41,BoxLayout.X_AXIS));
jp41.add(jp21);
jp41.add(Box.createHorizontalStrut(50));
jp41.add(jp31);
panel1.add(jp41); //adding p41 panel to panel1
//<--------coding for second tab--------->
JPanel panel2 = new JPanel();
tabbedPane.addTab("Id of Landlord",null,panel2,"second tab");
panel2.setLayout(new FlowLayout());
//adding radiobutton above TitledBorder panel
JPanel jp02=new JPanel();
JLabel l12=new JLabel("Identity Known");
JRadioButton jrb12=new JRadioButton("Yes");
JRadioButton jrb22=new JRadioButton("No");
ButtonGroup bg12=new ButtonGroup();
bg12.add(jrb12);
bg12.add(jrb22);
jp02.add(l12);
jp02.add(jrb12);
jp02.add(jrb22);
//adding TitledBorder panel
JPanel jp12=new JPanel();
jp12.setBorder(new TitledBorder("Identity Detail"));
jp12.setLayout(new GridBagLayout());
gbc.insets=new Insets(5,5,5,5);
JLabel l22=new JLabel("Identity Card");
gbc.gridx=0;
gbc.gridy=0;
jp12.add(l22,gbc);
JTextField jtf12=new JTextField(10);
gbc.gridx=1;
jp12.add(jtf12,gbc);
JLabel l32=new JLabel("Date of Issue");
gbc.gridx=0;
gbc.gridy=1;
jp12.add(l32,gbc);
JTextField jtf22=new JTextField(10);
gbc.insets=new Insets(5,5,5,25);
gbc.gridx=1;
jp12.add(jtf22,gbc);
JLabel l42=new JLabel("Identity Number");
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=2;
jp12.add(l42,gbc);
JTextField jtf32=new JTextField(10);
gbc.gridx=3;
jp12.add(jtf32,gbc);
JLabel l52=new JLabel("Name Of Issuer");
gbc.gridx=0;
gbc.gridy=2;
jp12.add(l52,gbc);
JTextField jtf42=new JTextField(10);
gbc.insets=new Insets(5,5,5,25);
gbc.gridx=1;
jp12.add(jtf42,gbc);
JLabel l62= new JLabel("Place Of Issuer");
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=2;
jp12.add(l62,gbc);
JTextField jtf52=new JTextField(10);
gbc.gridx=3;
jp12.add(jtf52,gbc);
gbc.gridx=0;
gbc.gridy=2;
Box b12 =Box.createVerticalBox(); //adding both panels to vertica box and adding it to panel2
b12.add(jp02);
b12.add(jp12);
panel2.add(b12);
//<--------coding for third tab--------->
JPanel panel3 = new JPanel();
tabbedPane.addTab("Detail of Tenant",null,panel3,"third tab");
//adding panel for top data
JPanel jp13=new JPanel(); //jp13 panel for details above 3 TitledBorder panels
jp13.setLayout(new GridBagLayout());
JLabel l13=new JLabel("Name");
gbc.insets=new Insets(2,2,2,2);
gbc.gridx=0;
gbc.gridy=0;
jp13.add(l13,gbc);
JComboBox c13=new JComboBox();
c13.addItem("Select");
c13.addItem("Mr.");
c13.addItem("Mrs.");
gbc.gridx=1;
jp13.add(c13,gbc);
JTextField jtf13=new JTextField(16);
gbc.gridx=2;
gbc.gridwidth=2;
jp13.add(jtf13,gbc);
JLabel l23=new JLabel("Sex");
gbc.insets=new Insets(2,20,2,2);
gbc.gridx=4;
gbc.gridwidth=1;
jp13.add(l23,gbc);
JComboBox c23=new JComboBox();
c23.addItem("Select");
c23.addItem("Male");
c23.addItem("Female");
gbc.insets=new Insets(2,2,2,2);
gbc.gridx=5;
jp13.add(c23,gbc);
JLabel l33=new JLabel("Father/Mother/Husband's Name");
gbc.gridy=1;
gbc.gridx=0;
jp13.add(l33,gbc);
JComboBox c33=new JComboBox();
c33.addItem("Select");
c33.addItem("Mr.");
c33.addItem("Mrs.");
gbc.gridx=1;
jp13.add(c33,gbc);
JComboBox c43=new JComboBox();
c43.addItem("Select");
c43.addItem("Mr.");
c43.addItem("Mrs.");
gbc.gridx=2;
jp13.add(c43,gbc);
JTextField jtf23=new JTextField(10);
gbc.gridx=3;
jp13.add(jtf23,gbc);
JLabel l43=new JLabel("Age(Yrs)");
gbc.insets=new Insets(2,20,2,2);
gbc.gridx=4;
jp13.add(l43,gbc);
JTextField jtf33=new JTextField(3);
gbc.insets=new Insets(2,2,2,2);
gbc.gridx=5;
jp13.add(jtf33,gbc);
JLabel l53=new JLabel("Citizenship");
gbc.gridy=2;
gbc.gridx=0;
jp13.add(l53,gbc);
JComboBox c53=new JComboBox();
c53.addItem("Select");
c53.addItem("Indian");
c53.addItem("Australian");
gbc.gridx=1;
gbc.gridwidth=2;
jp13.add(c53,gbc);
JLabel l63=new JLabel("Occupation");
gbc.insets=new Insets(2,20,2,2);
gbc.gridx=4;
gbc.gridwidth=1;
jp13.add(l63,gbc);
JComboBox c63=new JComboBox();
c63.addItem("Select");
c63.addItem("Engineer");
c63.addItem("Doctor");
gbc.insets=new Insets(2,2,2,2);
gbc.gridx=5;
jp13.add(c63,gbc);
panel3.add(jp13);
JPanel jp23=new JPanel(); //first of 3 TitledBorder panels
jp23.setBorder(new TitledBorder("Local Address"));
jp23.setLayout(new GridBagLayout());
JLabel l73=new JLabel("Local Address");
gbc.gridx=0;
gbc.gridy=0;
jp23.add(l73,gbc);
JTextArea ta43=new JTextArea(3,10);
ta43.setLineWrap(true);
JScrollPane scroll13=new JScrollPane(ta43);
gbc.gridx=1;
gbc.gridy=0;
jp23.add(ta43,gbc);
JLabel l83=new JLabel("Country");
gbc.gridx=0;
gbc.gridy=1;
gbc.gridheight=1;
jp23.add(l83,gbc);
JComboBox c73=new JComboBox();
c73.addItem("India");
c73.addItem("US");
c73.addItem("Australia");
gbc.gridx=1;
gbc.gridy=1;
jp23.add(c73,gbc);
JLabel l93=new JLabel("State");
gbc.gridx=0;
gbc.gridy=2;
jp23.add(l93,gbc);
JComboBox c83=new JComboBox();
c83.addItem("Rajasthan");
c83.addItem("Delhi");
c83.addItem("Maharastra");
gbc.gridx=1;
gbc.gridy=2;
jp23.add(c83,gbc);
JLabel l103=new JLabel("District");
gbc.gridx=0;
gbc.gridy=3;
jp23.add(l103,gbc);
JComboBox c93=new JComboBox();
c93.addItem("jaipur");
c93.addItem("ajmer");
c93.addItem("alwar");
gbc.gridx=1;
gbc.gridy=3;
jp23.add(c93,gbc);
JLabel l113=new JLabel("City/Town");
gbc.gridx=0;
gbc.gridy=4;
jp23.add(l113,gbc);
JTextField tf53=new JTextField(10);
gbc.gridx=1;
gbc.gridy=4;
jp23.add(tf53,gbc);
JLabel l123=new JLabel("Police District");
gbc.gridx=0;
gbc.gridy=5;
jp23.add(l123,gbc);
JComboBox c103=new JComboBox();
c103.addItem("Jaipur");
c103.addItem("Alwar");
c103.addItem("Ajmer");
gbc.gridx=1;
gbc.gridy=5;
jp23.add(c103,gbc);
JLabel l133=new JLabel("Police Circle");
gbc.gridx=0;
gbc.gridy=6;
jp23.add(l133,gbc);
JComboBox c113=new JComboBox();
c113.addItem("India");
c113.addItem("US");
c113.addItem("Australia");
gbc.gridx=1;
gbc.gridy=6;
jp23.add(c113,gbc);
JLabel l143=new JLabel("Police station");
gbc.gridx=0;
gbc.gridy=7;
jp23.add(l143,gbc);
JComboBox c123=new JComboBox();
c123.addItem("Bani Park");
c123.addItem("Raja Park");
c123.addItem("Malviya Nagar");
gbc.gridx=1;
gbc.gridy=7;
jp23.add(c123,gbc);
JLabel l153=new JLabel("Pin No.");
gbc.gridx=0;
gbc.gridy=8;
jp23.add(l153,gbc);
JTextField tf63=new JTextField(10);
gbc.gridx=1;
gbc.gridy=8;
jp23.add(tf63,gbc);
JLabel l163=new JLabel("Phone No.(R)");
gbc.gridx=0;
gbc.gridy=9;
jp23.add(l163,gbc);
JTextField tf73=new JTextField(10);
gbc.gridx=1;
gbc.gridy=9;
jp23.add(tf73,gbc);
JLabel l173=new JLabel("E-mail");
gbc.gridx=0;
gbc.gridy=10;
gbc.gridheight=1;
jp23.add(l173,gbc);
JTextField tf83=new JTextField(10);
gbc.gridx=1;
gbc.gridy=10;
jp23.add(tf83,gbc);
JPanel jp33=new JPanel(); //second of 3 TitledBorder panels
jp33.setBorder(new TitledBorder("Permanent Address"));
jp33.setLayout(new GridBagLayout());
JLabel l183=new JLabel("Perm. Address");
gbc.gridx=0;
gbc.gridy=0;
jp33.add(l183,gbc);
JTextArea ta93=new JTextArea(5,10);
JScrollPane scroll23=new JScrollPane(ta43);
ta93.setLineWrap(true);
gbc.gridx=1;
gbc.gridy=0;
jp33.add(ta43,gbc);
JLabel l193=new JLabel("Country");
gbc.gridx=0;
gbc.gridy=1;
gbc.gridheight=1;
jp33.add(l193,gbc);
JComboBox c133=new JComboBox();
c133.addItem("India");
c133.addItem("US");
c133.addItem("Australia");
gbc.gridx=1;
gbc.gridy=1;
jp33.add(c133,gbc);
JLabel l203=new JLabel("State");
gbc.gridx=0;
gbc.gridy=2;
jp33.add(l203,gbc);
JComboBox c143=new JComboBox();
c143.addItem("Rajasthan");
c143.addItem("Delhi");
c143.addItem("Maharastra");
gbc.gridx=1;
gbc.gridy=2;
jp33.add(c143,gbc);
JLabel l213=new JLabel("District");
gbc.gridx=0;
gbc.gridy=3;
jp33.add(l213,gbc);
JComboBox c153=new JComboBox();
c153.addItem("jaipur");
c153.addItem("ajmer");
c153.addItem("alwar");
gbc.gridx=1;
gbc.gridy=3;
jp33.add(c153,gbc);
JLabel l223=new JLabel("City/Town");
gbc.gridx=0;
gbc.gridy=4;
jp33.add(l223,gbc);
JTextField tf103=new JTextField(10);
gbc.gridx=1;
gbc.gridy=4;
jp33.add(tf103,gbc);
JLabel l233=new JLabel("Police District");
gbc.gridx=0;
gbc.gridy=5;
jp33.add(l233,gbc);
JComboBox c163=new JComboBox();
c163.addItem("Jaipur");
c163.addItem("Alwar");
c163.addItem("Ajmer");
gbc.gridx=1;
gbc.gridy=5;
jp33.add(c163,gbc);
JLabel l243=new JLabel("Police Circle");
gbc.gridx=0;
gbc.gridy=6;
jp33.add(l243,gbc);
JComboBox c173=new JComboBox();
c173.addItem("India");
c173.addItem("US");
c173.addItem("Australia");
gbc.gridx=1;
gbc.gridy=6;
jp33.add(c173,gbc);
JLabel l253=new JLabel("Police station");
gbc.gridx=0;
gbc.gridy=7;
jp33.add(l253,gbc);
JComboBox c183=new JComboBox();
c183.addItem("Bani Park");
c183.addItem("Raja Park");
c183.addItem("Malviya Nagar");
gbc.gridx=1;
gbc.gridy=7;
jp33.add(c183,gbc);
JLabel l263=new JLabel("Pin No.");
gbc.gridx=0;
gbc.gridy=8;
jp33.add(l263,gbc);
JTextField tf113=new JTextField(10);
gbc.gridx=1;
gbc.gridy=8;
jp33.add(tf113,gbc);
JLabel l273=new JLabel("Phone No.(R)");
gbc.gridx=0;
gbc.gridy=9;
jp33.add(l273,gbc);
JTextField tf123=new JTextField(10);
gbc.gridx=1;
gbc.gridy=9;
jp33.add(tf123,gbc);
JLabel l283=new JLabel("phone No.(M)");
gbc.gridx=0;
gbc.gridy=10;
gbc.gridheight=1;
jp33.add(l283,gbc);
JTextField tf133=new JTextField(10);
gbc.gridx=1;
gbc.gridy=10;
jp33.add(tf133,gbc);
JPanel jp43=new JPanel(); //third of 3 TitledBorder panels
jp43.setBorder(new TitledBorder("Ex-Home Address"));
jp43.setLayout(new GridBagLayout());
JLabel l293=new JLabel("Address");
gbc.gridx=0;
gbc.gridy=0;
jp43.add(l293,gbc);
JTextArea ta143=new JTextArea(3,10);
ta143.setLineWrap(true);
JScrollPane scroll33=new JScrollPane(ta143);
gbc.gridx=1;
gbc.gridy=0;
jp43.add(ta143,gbc);
JLabel l303=new JLabel("Country");
gbc.gridx=0;
gbc.gridy=1;
gbc.gridheight=1;
jp43.add(l303,gbc);
JComboBox c193=new JComboBox();
c193.addItem("India");
c193.addItem("US");
c193.addItem("Australia");
gbc.gridx=1;
gbc.gridy=1;
jp43.add(c193,gbc);
JLabel l313=new JLabel("State");
gbc.gridx=0;
gbc.gridy=2;
jp43.add(l313,gbc);
JComboBox c203=new JComboBox();
c203.addItem("Rajasthan");
c203.addItem("Delhi");
c203.addItem("Maharastra");
gbc.gridx=1;
gbc.gridy=2;
jp43.add(c203,gbc);
JLabel l323=new JLabel("District");
gbc.gridx=0;
gbc.gridy=3;
jp43.add(l323,gbc);
JComboBox c213=new JComboBox();
c213.addItem("jaipur");
c213.addItem("ajmer");
c213.addItem("alwar");
gbc.gridx=1;
gbc.gridy=3;
jp43.add(c213,gbc);
JLabel l333=new JLabel("City/Town");
gbc.gridx=0;
gbc.gridy=4;
jp43.add(l333,gbc);
JTextField tf153=new JTextField(10);
gbc.gridx=1;
gbc.gridy=4;
jp43.add(tf153,gbc);
JLabel l343=new JLabel("Police District");
gbc.gridx=0;
gbc.gridy=5;
jp43.add(l343,gbc);
JComboBox c223=new JComboBox();
c223.addItem("Jaipur");
c223.addItem("Alwar");
c223.addItem("Ajmer");
gbc.gridx=1;
gbc.gridy=5;
jp43.add(c223,gbc);
JLabel l353=new JLabel("Police Circle");
gbc.gridx=0;
gbc.gridy=6;
jp43.add(l353,gbc);
JComboBox c233=new JComboBox();
c233.addItem("India");
c233.addItem("US");
c233.addItem("Australia");
gbc.gridx=1;
gbc.gridy=6;
jp43.add(c233,gbc);
JLabel l363=new JLabel("Police station");
gbc.gridx=0;
gbc.gridy=7;
jp43.add(l363,gbc);
JComboBox c243=new JComboBox();
c243.addItem("Bani Park");
c243.addItem("Raja Park");
c243.addItem("Malviya Nagar");
gbc.gridx=1;
gbc.gridy=7;
jp43.add(c123,gbc);
JLabel l373=new JLabel("Pin No.");
gbc.gridx=0;
gbc.gridy=8;
jp43.add(l373,gbc);
JTextField tf163=new JTextField(10);
gbc.gridx=1;
gbc.gridy=8;
jp43.add(tf163,gbc);
JLabel l383=new JLabel("Phone");
gbc.gridx=0;
gbc.gridy=9;
jp43.add(l383,gbc);
JTextField tf173=new JTextField(10);
gbc.gridx=1;
gbc.gridy=9;
jp43.add(tf173,gbc);
JLabel l393=new JLabel("Leaving Date");
gbc.gridx=0;
gbc.gridy=10;
gbc.gridheight=1;
jp43.add(l393,gbc);
JTextField tf183=new JTextField(10);
gbc.gridx=1;
gbc.gridy=10;
jp43.add(tf183,gbc);
/*JCheckBox cb13=new JCheckBox("Approx");
gbc.gridx=2;
gbc.gridy=10;
jp43.add(cb13);*/
JPanel jp53=new JPanel(); //panel for addin all 3 TitledBorder panels
jp53.setLayout(new BoxLayout(jp53,BoxLayout.X_AXIS));
jp53.add(jp23);
jp53.add(Box.createHorizontalStrut(30)); //giving space between each TitledBorder panel
jp53.add(jp33);
jp53.add(Box.createHorizontalStrut(30));
jp53.add(jp43);
panel3.add(jp53); //adding panel which contains all 3 TitledBorder panels to panel3
JPanel jp63=new JPanel(); //jp63 panel for bottom data
jp63.setBorder(new TitledBorder("Tennancy Start/End"));
JLabel l403=new JLabel("From Date");
jp63.add(l403);
JTextField jtf193=new JTextField(12);
jp63.add(jtf193);
JCheckBox jcb23=new JCheckBox("Approx");
jp63.add(jcb23);
JLabel l413=new JLabel("To Date");
jp63.add(l413);
JTextField jtf203=new JTextField(12);
jp63.add(jtf203);
JCheckBox jcb33=new JCheckBox("Approx");
jp63.add(jcb33);
panel3.add(jp63); //adding bottom details panel jp63 to panel3
//<--------coding for fourth tab--------->
JPanel panel4 = new JPanel();
tabbedPane.addTab("Id of Tenant",null,panel4,"fourth tab");
panel4.setLayout(new FlowLayout());
//adding radiobutton above TitledBorder panel jp14
JPanel jp04=new JPanel();
JLabel l14=new JLabel("Identity Known");
JRadioButton jrb14=new JRadioButton("Yes");
JRadioButton jrb24=new JRadioButton("No");
ButtonGroup bg14=new ButtonGroup();
bg14.add(jrb14);
bg14.add(jrb24);
jp04.add(l14);
jp04.add(jrb14);
jp04.add(jrb24);
//adding TitledBorder panel jp14
JPanel jp14=new JPanel();
jp14.setBorder(new TitledBorder("Identity Detail"));
jp14.setLayout(new GridBagLayout());
gbc.insets=new Insets(5,5,5,5);
JLabel l24=new JLabel("Identity Card");
gbc.gridx=0;
gbc.gridy=0;
jp14.add(l24,gbc);
JTextField jtf14=new JTextField(10);
gbc.gridx=1;
jp14.add(jtf14,gbc);
JLabel l34=new JLabel("Date of Issue");
gbc.gridx=0;
gbc.gridy=1;
jp14.add(l34,gbc);
JTextField jtf24=new JTextField(10);
gbc.insets=new Insets(5,5,5,25);
gbc.gridx=1;
jp14.add(jtf24,gbc);
JLabel l44=new JLabel("Identity Number");
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=2;
jp14.add(l44,gbc);
JTextField jtf34=new JTextField(10);
gbc.gridx=3;
jp14.add(jtf34,gbc);
JLabel l54=new JLabel("Name Of Issuer");
gbc.gridx=0;
gbc.gridy=2;
jp14.add(l54,gbc);
JTextField jtf44=new JTextField(10);
gbc.insets=new Insets(5,5,5,25);
gbc.gridx=1;
jp14.add(jtf44,gbc);
JLabel l64=new JLabel("Place Of Issuer");
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=2;
jp14.add(l64,gbc);
JTextField jtf54=new JTextField(10);
gbc.gridx=3;
jp14.add(jtf54,gbc);
gbc.gridx=0;
gbc.gridy=2;
Box b14=Box.createVerticalBox(); //adding both panels to panel4 in vertical box
b14.add(jp04);
b14.add(jp14);
panel4.add(b14);
//<--------coding for fifth tab--------->
JPanel panel5 = new JPanel();
tabbedPane.addTab("Info Of Other Members",null,panel5,"fifth tab");
JPanel jp15=new JPanel(); //top panel jp15 for details above add delete buttons
jp15.setLayout(new GridBagLayout());
JLabel l15=new JLabel("Name");
gbc.gridx=0;
gbc.gridy=0;
gbc.insets=new Insets(10,10,10,10);
jp15.add(l15,gbc);
JTextField jtf15=new JTextField(10);
gbc.gridx=1;
jp15.add(jtf15,gbc);
JLabel l25=new JLabel("Sex");
gbc.gridx=2;
gbc.insets=new Insets(10,80,10,10);
jp15.add(l25,gbc);
JComboBox jcb15=new JComboBox();
jcb15.addItem("Male");
jcb15.addItem("Female");
gbc.gridx=3;
gbc.insets=new Insets(10,10,10,10);
jp15.add(jcb15,gbc);
JLabel l35=new JLabel("Relation");
gbc.gridx=0;
gbc.gridy=1;
gbc.insets=new Insets(10,10,40,10);
jp15.add(l35,gbc);
JTextField jtf25=new JTextField(10);
gbc.gridx=1;
jp15.add(jtf25,gbc);
JLabel l45=new JLabel("Age");
gbc.gridx=2;
gbc.insets=new Insets(10,80,40,10);
jp15.add(l45,gbc);
JTextField jtf35 =new JTextField(10);
gbc.gridx=3;
gbc.insets=new Insets(10,10,40,10);
jp15.add(jtf35,gbc);
JPanel jp25=new JPanel(); //middle panel jp25 for adding "add" and "delete" buttons
JButton jb15=new JButton("Add");
JButton jb25=new JButton("Delete");
jp25.add(jb15);
jp25.add(jb25);
//adding table #yet to be coded#
Box b15=Box.createVerticalBox();
b15.add(jp15);
b15.add(jp25);
panel5.add(b15); //adding jp15 and jp25 to panel5 in vertical box
//<--------coding for sixth tab--------->
JPanel panel6 = new JPanel();
tabbedPane.addTab("Related To Office",null,panel6,"sixth tab");
panel6.setLayout(new GridBagLayout());
//adding 4 labels and textfields vertically
JLabel l16=new JLabel("Investigation Officer");
gbc.gridx=0;
gbc.gridy=0;
gbc.insets=new Insets(40,40,20,10);
panel6.add(l16,gbc);
JTextField jtf16 =new JTextField(10);
gbc.gridx=1;
gbc.insets=new Insets(40,20,20,40);
panel6.add(jtf16,gbc);
JLabel l26=new JLabel("S.No.");
gbc.gridx=0;
gbc.gridy=1;
gbc.insets=new Insets(10,40,20,10);
panel6.add(l26,gbc);
JTextField jtf26 =new JTextField(10);
gbc.gridx=1;
gbc.insets=new Insets(10,20,20,40);
panel6.add(jtf26,gbc);
JLabel l36=new JLabel("Page No.");
gbc.gridx=0;
gbc.gridy=2;
gbc.insets=new Insets(10,40,20,10);
panel6.add(l36,gbc);
JTextField jtf36 =new JTextField(10);
gbc.gridx=1;
gbc.insets=new Insets(10,20,20,40);
panel6.add(jtf36,gbc);
JLabel l46=new JLabel("Date");
gbc.gridx=0;
gbc.gridy=3;
gbc.insets=new Insets(10,40,20,10);
panel6.add(l46,gbc);
JTextField jtf46=new JTextField(10);
gbc.gridx=1;
gbc.insets=new Insets(10,20,20,40);
panel6.add(jtf46,gbc);
/* Canvas c16=new Canvas(); //creating a rectangle frame for adding image
Image i16=c.createImage(200,100);
gbc.gridx=2;
gbc.gridy=0;
gbc.insets=new Insets(40,20,20,40);
gbc.gridheight=3;
panel6.add(i16,gbc);*/
JButton jb16=new JButton("Browse"); //adding "browse" and "save" buttons
gbc.insets=new Insets(10,20,20,40);
gbc.gridy=3;
panel6.add(jb16,gbc);
JButton jb26=new JButton("Save");
gbc.insets=new Insets(50,20,20,40);
gbc.gridy=4;
panel6.add(jb26,gbc);
//<-------end of sixth tab------->
add(tabbedPane);
//lower panel for cancel button
JPanel jp1=new JPanel();
jp1.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton can=new JButton("Cancel"); //initialising cancel button
jp1.add(can);
add(jp1,BorderLayout.SOUTH);
class Blistener implements ActionListener // class for action listener
public void actionPerformed(ActionEvent ae)
Object obj=ae.getSource();
try{
if(obj == can) //condition for cancel button
System.exit(0);
}catch(Exception e)
{System.out.println(e);
public class Project
public static void main(String args[]) //main method for the program
Design tabbedwin= new Design();
tabbedwin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedwin.setSize(900,700);
tabFirst things first. Next time please use the code formating tags when posting your code.
Secondly, we cant help you do your entire project or more specifically we cant do it for you. We can probably help you with one section at a time.
Thirdly, do not post your entire code. Create a Short, Self Contained, Compilable Example (SSCCE) that demonstrates the problem section, and that is independent of any third party libraries and can compiled instantly and tested.
Now, lets see if we can answer one problem here.
Ques 1. In the form I have one JComboBox of Country and other of State. Now I want it in such a way that whenI select a country from the Country JComboBox ,the corresponding states automatically appears in the State JComboBox.
Ans 1: Use an ItemListener or an ActionListener (javax.swing.event) for this. Once an item is selected, the event will be fired and then you populate your ComboBox with the requried data.
Ques 2. I need to add picture frame in the 6th tab so that the picture shows up when clicked browse and open.
Ans 2: You haven't created the JButton for the browse action, nor have you created the JLabel to show the picture in. Create the JButton, then the JLabel, attach an ActionListener to the button to listen for the button click, open a JFileChooser, select the image file, create an ImageIcon and pass the File object returned to it. Then call the JLabel.setIcon( the ImageIcon) to display the picture. You'll have to store the picture File information some where so can reference it when saving.
Ques 3.I need to add tables in 1st and 5th tab which shows up the details through database when added through several textboxes and combo boxes.
You need to do some reading. The Swing Tutorial has everything you need, from How to use Tables to How to connect to databases. There are also some examples in your JDK installation folder, ie, jdk/demos/jfc/tableExample. Here is the link to the swing tutorial on how to use Tables: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html. You find other useful links there.
ICE -
Please help me ( advanced graphical user interface)
I have to finish this problem until 5 pm.
Java How to program (Deitel & Deitel) - Third edition
page 696
please help me
13-7 : write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations : diameter=2 * radius, area = 3.14(pie) * radius * radius, circumference = 2 * pie * radius. Use the constant Math.PI for pi. All drawing should be done on a subclass of JPanel and the result of the calculations should be displayed in a read-only JTextArea.
13-10 : Write a program that uses the paint method to draw the current value of a JSlider on a subclass of JPanel. In addition, provide a JTextField where a specific value can be entered. The JTextField should display the current value of the JSlider at all times. A JLabel should be used to identify the JTextField. The JSlider methods setValue and getValue should be used.(Note : The setValue method is a public method that does not return a value and takes one integer arguement - the JSlider value, which determines the position of the thumb.)... until 5 pm.What time zone? I can only see that your message was posted yesterday 7:30 PM, in some unknown time zone.
And, you are more likely to get help if you show that you have tried to find a solution on your own. No sane person will do your work for you! -
Printing a TextArea. PLEASE HELP ME! :)
I'm trying to use this code for printing the content of a TextArea:
void Stampare(){
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
PageFormat documentPageFormat = new PageFormat();
documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
book.append(new Document(), documentPageFormat);
printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
catch (Exception PrintException){
PrintException.printStackTrace();
public class Document implements Printable {
private final static int POINTS_PER_INCH = 72;
public int print (Graphics g, PageFormat pageFormat, int page) {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setPaint(Color.black);
g2d.setStroke(new BasicStroke (4));
Rectangle2D.Double border = new Rectangle2D.Double (0, 0, pageFormat.getImageableWidth (), pageFormat.getImageableHeight ());
//g2d.draw(border);
String text = new String(areaStampa.getText());
Point2D.Double pen = new Point2D.Double(0.25 * POINTS_PER_INCH, 0.25 * POINTS_PER_INCH);
double width = 7.5 * POINTS_PER_INCH;
AttributedString paragraphText = new AttributedString(text);
paragraphText.addAttribute(TextAttribute.FONT, new Font("serif", Font.PLAIN, 12));
LineBreakMeasurer lineBreaker = new LineBreakMeasurer(paragraphText.getIterator(), new FontRenderContext(null, true, true));
TextLayout layout;
while ((layout = lineBreaker.nextLayout((float) width)) != null){
pen.y += layout.getAscent();
layout.draw(g2d, (float) pen.x, (float) pen.y);
pen.y += layout.getDescent() + layout.getLeading();
return (PAGE_EXISTS);
Obviuosly, it doesn't work as I would like it to do!
First of all, It doesn't print the lines as they are in the TextArea. I mean, the carriage returns are not the one that are in the TextArea.
Secondly, If there's a lot of text, It does exactly nothing! My purpose is to print pages as if they were a Word document, while here it seems that I cannot print more than a page.
Please, please, please, help me!!!! I'm becoming crazy with this problem!
Thanks a lot,
GiorgioI really need somebody who could give me the correct version of this application. Have you ever developed an application that uses a TextArea (or a JTextArea, if it is possible to use that) exactly as it was a Word document? I mean, something that would let you print what is inside the TextArea as if it was inside a Word document, with more than one page, all the lines printed correctly and so on. If you have that, please, please, please!!! send me a copy. My e-mail address is [email protected]
I would really, really appreciate that!
Thanks,
Giorgio -
Totally lost - can anyone please help?
Hi,
Can anyone please help with the following...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class project
public static void main(String[] args)
// Time time = new Time();
// System.out.println(time.getTime());
right r = new right();
left l = new left();
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JFrame aFrame = new JFrame();
aFrame.setTitle("Swing 3");
aFrame.setBounds(0, 0, 700, 500);
Container contentPane = aFrame.getContentPane();
// add(new left());
// add(new right());
//p.add(new left());
//p.add(new right());
r.textArea.setText("Red");
p.add(l);
p.add(r);
p3.add(p);
///p3.add(p2);
contentPane.add(p3);
aFrame.setVisible(true);
===================================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class left extends JPanel implements ActionListener
public JButton redButton,greenButton,blueButton,yellowButton;
public left()
JPanel pp = new JPanel();
setLayout(new BorderLayout());
redButton = new JButton("Red");
redButton.addActionListener(this); // step 4
greenButton = new JButton("Green");
greenButton.addActionListener(this);
blueButton = new JButton("Blue");
blueButton.addActionListener(this);
yellowButton = new JButton("Yellow");
yellowButton.addActionListener(this);
pp.add(redButton);
pp.add(greenButton);
pp.add(blueButton);
pp.add(yellowButton);
add(pp,BorderLayout.NORTH);
public void actionPerformed(ActionEvent e)
// e.getSource() // returns name of the button
if (e.getSource() == redButton){
// r.setT("red");
System.out.println("red button");
if (e.getSource() == greenButton)
//r.textArea.setText("green");
System.out.println("green button");
if (e.getSource() == blueButton)
//r.textArea.setText("blue");
System.out.println("blue button");
if (e.getSource() == yellowButton)
// r.textArea.setText("yellow");
System.out.println("yellow button");
===================================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class right extends JPanel
JButton oneButton,twoButton;
public JTextArea textArea;
public right()
JPanel jp = new JPanel();
setLayout(new BorderLayout());
oneButton = new JButton("one");
twoButton = new JButton("two");
textArea = new JTextArea();
jp.add(oneButton);
jp.add(twoButton);
add(jp,BorderLayout.SOUTH);
add(textArea,BorderLayout.NORTH);
}In the left class I want to be able to click a button and then display a message in the JTextArea. I want main to be global - so the r object can be accessed anywhere.
If anyone could get this to work I'd be really grateful.import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ProjectRx
public static void main(String[] args)
// For the buttons in left to set text in the textArea
// in right the two classes must communicate in some way.
// Left must have a way to talk to right.
// Option 1:
// Pass a reference to right to left which left can use
// to access the textArea in right.
ProjectRightRx r = new ProjectRightRx();
r.textArea.setText("Red");
ProjectLeftRx l = new ProjectLeftRx(r);
// Option 2:
// You could send a reference to the textArea to left
// instead of the reference to its enclosing class.
//r.textArea.setText("Red");
//ProjectLeftRx l = new ProjectLeftRx(r.textArea);
JFrame aFrame = new JFrame();
aFrame.setTitle("Swing 3");
aFrame.setSize(400, 400);
aFrame.setLocation(100,100);
Container contentPane = aFrame.getContentPane();
contentPane.add(r, "First");
contentPane.add(l, "Last");
aFrame.setVisible(true);
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ProjectLeftRx extends JPanel implements ActionListener
ProjectRightRx r;
public JButton redButton,greenButton,blueButton,yellowButton;
public ProjectLeftRx(ProjectRightRx r)
this.r = r;
JPanel pp = new JPanel();
pp.setBorder(BorderFactory.createTitledBorder("Left"));
setLayout(new BorderLayout());
redButton = new JButton("Red");
redButton.addActionListener(this); // step 4
greenButton = new JButton("Green");
greenButton.addActionListener(this);
blueButton = new JButton("Blue");
blueButton.addActionListener(this);
yellowButton = new JButton("Yellow");
yellowButton.addActionListener(this);
pp.add(redButton);
pp.add(greenButton);
pp.add(blueButton);
pp.add(yellowButton);
add(pp,BorderLayout.NORTH);
public void actionPerformed(ActionEvent e)
// e.getSource() returns object that generated this event
// To get the name of the button you can try:
JButton button = (JButton)e.getSource();
String name = button.getActionCommand(); // or,
// button.getText();
String ac = e.getActionCommand();
System.out.println("name = " + name + " ac = " + ac);
if (e.getSource() == redButton){
// r.setT("red");
System.out.println("red button");
if (e.getSource() == greenButton)
r.textArea.setText("green");
System.out.println("green button");
if (e.getSource() == blueButton)
r.textArea.setText("blue");
System.out.println("blue button");
if (e.getSource() == yellowButton)
r.textArea.setText("yellow");
System.out.println("yellow button");
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ProjectRightRx extends JPanel
JButton oneButton,twoButton;
public JTextArea textArea;
public ProjectRightRx()
JPanel jp = new JPanel();
setLayout(new BorderLayout());
oneButton = new JButton("one");
twoButton = new JButton("two");
textArea = new JTextArea(10,25);
jp.add(oneButton);
jp.add(twoButton);
add(new JLabel("Right", JLabel.CENTER), BorderLayout.NORTH);
add(jp,BorderLayout.SOUTH);
add(textArea,BorderLayout.CENTER);
} -
Please help me with this program,the progarm should operates as follows
1)Calculate the number of words
2)Calculate the number of sentences
3)Calculate the repeated words
* Title: <p>
* Description: <p>
* Copyright: Copyright (c) cds<p>
* Company: s<p>
* @author cds
* @version 1.0
package count;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
import java.util.StringTokenizer;
public class Frame1 extends JFrame {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
XYLayout xYLayout2 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jta1 = new JTextArea();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea jta2 = new JTextArea();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JButton jbtCountSentances = new JButton();
JButton jbtCountWords = new JButton();
JButton jbtRWords = new JButton();
JButton jbtClearList = new JButton();
JTextField jtfNumOfSentances = new JTextField();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JTextField jtfNumOfWords = new JTextField();
private JFileChooser jFileChooser = new JFileChooser();
public static String sentence,RepeatedWords,repwords,chr=" ";
public static int i,words,token,periods,characters,len,long_word,long_sent,stop;
//Construct the frame
public Frame1() {
try {
jbInit();
catch(Exception e) {
e.printStackTrace();
}setSize(600,400);
show();
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
this.getContentPane().setLayout(xYLayout2);
jLabel1.setForeground(Color.blue);
jLabel1.setText(" Type your Doc");
jLabel2.setForeground(Color.blue);
jLabel2.setText(" Reapeated Words");
jbtCountSentances.setBackground(Color.white);
jbtCountSentances.setForeground(Color.blue);
jbtCountSentances.setText("Count Sentances");
jbtCountSentances.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbtCountSentances_actionPerformed(e);
jbtCountWords.setBackground(Color.white);
jbtCountWords.setForeground(Color.blue);
jbtCountWords.setText("Count Words");
jbtCountWords.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbtCountWords_actionPerformed(e);
jbtRWords.setBackground(Color.white);
jbtRWords.setForeground(Color.blue);
jbtRWords.setText("Reapeted Words");
jbtRWords.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbtRWords_actionPerformed(e);
jbtClearList.setBackground(Color.white);
jbtClearList.setForeground(Color.blue);
jbtClearList.setText("Clear");
jbtClearList.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbtClearList_actionPerformed(e);
jLabel3.setForeground(Color.blue);
jLabel3.setText("NumOfSentances");
jLabel4.setForeground(Color.blue);
jLabel4.setText("NumOfWords");
jPanel1.setBackground(SystemColor.activeCaption);
jPanel1.setForeground(SystemColor.activeCaption);
this.getContentPane().add(jPanel1, new XYConstraints(4, 4, 986, 469));
jPanel1.add(jScrollPane1, new XYConstraints(5, 105, 289, 192));
jScrollPane1.getViewport().add(jta1, null);
jPanel1.add(jScrollPane2, new XYConstraints(309, 102, 256, 193));
jScrollPane2.getViewport().add(jta2, null);
jPanel1.add(jLabel1, new XYConstraints(21, 81, 240, 22));
jPanel1.add(jLabel2, new XYConstraints(340, 78, 191, 20));
jPanel1.add(jbtCountSentances, new XYConstraints(69, 309, 190, 30));
jPanel1.add(jbtCountWords, new XYConstraints(69, 339, 190, 24));
jPanel1.add(jbtClearList, new XYConstraints(262, 337, 173, 26));
jPanel1.add(jbtRWords, new XYConstraints(262, 310, 174, 24));
jPanel1.add(jLabel3, new XYConstraints(14, 37, 105, 20));
jPanel1.add(jtfNumOfSentances, new XYConstraints(114, 37, 64, 23));
jPanel1.add(jLabel4, new XYConstraints(212, 38, 82, 23));
jPanel1.add(jtfNumOfWords, new XYConstraints(304, 35, 83, 27));
void jbtCountSentances_actionPerformed(ActionEvent e) {
BufferedReader stdin = new BufferedReader
(new InputStreamReader (System.in));
sentence = jta1.getText();
System.out.flush ();
len=sentence.length();
jtfNumOfSentances.setText(String.valueOf(num_periods()));
public static int num_periods () {
i=0;
periods=3;
chr= ".";
chr= "!";
chr= "?";
while (i<len){
if (sentence.charAt(i)==chr.charAt(0))
periods++;
i++;
return periods;
}//Method num_words
void jbtCountWords_actionPerformed(ActionEvent e) {
String s = jta1.getText();
StringTokenizer st = new StringTokenizer(s);
jtfNumOfWords.setText(String.valueOf(st.countTokens()));
void jbtRWords_actionPerformed(ActionEvent e) {
repwords = jta1.getText();
StringTokenizer st = new StringTokenizer(repwords);
while (st.hasMoreTokens())
RepeatedWords = st.nextToken(st.toString()) ;
jta2.append(RepeatedWords);
//}//Method num_words
/*void jbtClearList_actionPerformed(ActionEvent e) {
jta2.setText(null);
void jbtHelp_actionPerformed(ActionEvent e) {
jta1.append("Please open a File Write a document");
// JScrollPane jScrollPane1 = new JScrollPane();
//JTextArea jta1 = new JTextArea();
// JScrollPane jScrollPane2 = new JScrollPane();
//JTextArea jta2 = new JTextArea();
void jbtClearList_actionPerformed(ActionEvent e) {
jta2.setText(null);
jta1.setText(null);
jtfNumOfWords.setText(null);
jtfNumOfSentances.setText(null);Let me try specify the main problem in details
The code that i had posted displays an interface consisting of a 2 textarea.
The first textarea is for a user to enter a sentence and the second is for displaying duplicated words.
The program should allow the user to enter a sentence on the first textarea and when JButton named showduplicatedwords pressed it must display the words that are being duplicated in the sentence on the second textarea.And again count the number of sentence available when
JButton countsentence is pressed -
Cannot resolve Sybol!!!Please help me!
This is my code..
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
class ClientWorker implements Runnable {
private Socket client;
private JTextArea textArea;
ClientWorker(Socket client, JTextArea textArea) {
this.client = client;
this.textArea = textArea;
public void run(){
String line;
BufferedReader in = null;
PrintWriter out = null;
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.out.println("in or out failed");
System.exit(-1);
while(true){
try{
line = in.readLine();
//Send data back to client
out.println(line);
textArea.append(line);
} catch (IOException e) {
System.out.println("Read failed");
System.exit(-1);
public class FinalServer2 extends javax.swing.JFrame {
/** Creates new form FinalServer2 */
public FinalServer2() {
initComponents();
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
private void initComponents() {
textArea1 = new java.awt.TextArea();
button1 = new java.awt.Button();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
getContentPane().add(textArea1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 200, 130));
button1.setLabel("button1");
getContentPane().add(button1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 150, -1, -1));
pack();
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
/** @param args the command line arguments
public static void main(String[] args) {
FinalServer2 frame = new FinalServer2();
frame.setTitle("Server Program");
frame.pack();
frame.setVisible(true);
frame.listenSocket();
public void listenSocket() {
try{
server = new ServerSocket(1055);
} catch (IOException e) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
while(true){
ClientWorker w;
try{
w = new ClientWorker(server.accept(), textArea1);
Thread t = new Thread(w);
t.start();
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
// Variables declaration - do not modify
private java.awt.Button button1;
private java.awt.TextArea textArea1;
// End of variables declaration
Socket client = null;
BufferedReader in = null;
String line;
PrintWriter out = null;
ServerSocket server = null;
When i compile the code i recieve this message..
FinalServer2.java [111:1] cannot resolve symbol
symbol : constructor ClientWorker (java.net.Socket,java.awt.TextArea)
location: class ClientWorker
w = new ClientWorker(server.accept(), textArea1);
^
Please help me..ClientWorker(Socket client, JTextArea textArea) {
this.client = client;
this.textArea = textArea;
}<SNIP>
When i compile the code i recieve this message..
FinalServer2.java [111:1] cannot resolve symbol
symbol : constructor ClientWorker
(java.net.Socket,java.awt.TextArea)
location: class ClientWorker
w = new ClientWorker(server.accept(),
erver.accept(), textArea1);
^
Please help me..Look at the costructor and and error message, s=and see if you can work it out; if you are still having difficuly, post back in 10 mins. -
What am i doing wrong with this class please help
What am i doing wrong with this class? I'm trying to create a JFrame with a JTextArea and a ScrollPane so that text can be inserted into the text area. however evertime i run the program i cannot see the textarea but only the scrollpane. please help.
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Instructions extends JFrame{
public JTextArea textArea;
private String s;
private JScrollPane scrollPane;
public Instructions(){
super();
textArea = new JTextArea();
s = "Select a station and then\nadd\n";
textArea.append(s);
textArea.setEditable(true);
scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(textArea);
this.getContentPane().add(scrollPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Instructions");
this.setSize(400,400);
this.setVisible(true);
}//end constructor
public static void main(String args[]){
new Instructions();
}//end classI'm just winging this so it might be wrong:
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Instructions extends JFrame{
public JTextArea textArea;
private String s;
private JScrollPane scrollPane;
public Instructions(){
super();
textArea = new JTextArea();
s = "Select a station and then\nadd\n";
textArea.append(s);
textArea.setEditable(true);
scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Here you already have textArea in scrollPane so no need to put it in
// content pane, just put scrollPane in ContentPane.
// think of it as containers within containers
// when you try to put them both in at ContentPane level it will use
// it's layout manager to put them in there side by side or something
// so just leave this out this.getContentPane().add(textArea);
this.getContentPane().add(scrollPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Instructions");
this.setSize(400,400);
this.setVisible(true);
}//end constructor
public static void main(String args[]){
new Instructions();
}//end class -
Uk student..Please help !!
Hi. I am 17 years old at college in the Hertfordshire region studying java programming. I am really stuck on this program and would be really greatful for some help on this. Below is my program and i am trying to create a survey in which the user clicks on the radio button, then when ok is pressed, the corresponding number is written to a seperate file and then the file is called and displayed to create a sccesful display of previous and present results. PLEASE HELP!!!
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JApplet;
import javax.swing.JTextArea;
import java.io.*;
<applet code="Survey22" width = 1025 height = 660>
</Applet>
public class Survey22 extends JApplet implements ActionListener {
JLabel surveypic;
ImageIcon icon, name, surveyimage;
JMenuBar menubar;
JMenu menu;
JMenuItem menuitem;
JRadioButton rb1, rb2, rb3, rb4;
ButtonGroup group = new ButtonGroup();
JTextField field;
JButton ok;
JButton clear;
JMenuBar menubar2;
JMenu menu2;
JMenuItem menuitem2;
JLabel FinalFant, Quak, Crash, Hal, res;
ImageIcon results = new ImageIcon("RESULTS.JPG");
public void init(){
//***************************************************** MENUBAR **********************************************************
menubar = new JMenuBar();
menu = new JMenu("Menu");
ImageIcon results = new ImageIcon("RESULTS.JPG");
menuitem = new JMenuItem("",results);
menuitem.addActionListener(this);
menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menu.add(menuitem);
menubar.add(menu);
//****************************************************** MENUBAR *********************************************************
//****************************************************** OPTIONS *********************************************************
name = new ImageIcon("FINALFANTASY7.JPG");
rb1 = new JRadioButton(" Final Fantasy",name);
rb1.addActionListener(this);
getContentPane().setLayout(null);
getContentPane().add(rb1);
rb1.setBounds(10,210,270,140);
name = new ImageIcon("QUAKE.JPG");
rb2 = new JRadioButton(" Quake", name);
rb2.addActionListener(this);
getContentPane().setLayout(null);
getContentPane().add(rb2);
rb2.setBounds(310,210,270,140);
name = new ImageIcon("CRASHBANDICOOT.JPG");
rb3 = new JRadioButton(" Crash Bandicoot", name);
rb3.addActionListener(this);
getContentPane().setLayout(null);
getContentPane().add(rb3);
rb3.setBounds(610,210,300,140);
name = new ImageIcon("HALO.JPG");
rb4 = new JRadioButton(" Halo", name);
rb4.addActionListener(this);
getContentPane().setLayout(null);
getContentPane().add(rb4);
rb4.setBounds(10,365,350,160);
//****************************************************** OPTIONS *********************************************************
//****************************************************** EXTRAS **********************************************************
surveyimage = new ImageIcon("WELCOME.JPG");
surveypic = new JLabel(surveyimage);
getContentPane().add(surveypic);
surveypic.setBounds(10,40,465,150);
field = new JTextField();
field.setEditable(false);
getContentPane().setLayout( null );
getContentPane().add(field);
field.setBounds(600,40,100,30);
ok = new JButton("Ok");
ok.addActionListener(this);
getContentPane().add(ok);
ok.setBounds(575,100,73,30);
clear = new JButton("Cancel");
clear.addActionListener(this);
getContentPane().add(clear);
clear.setBounds(655,100,73,30);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(menubar, BorderLayout.NORTH);
//******************************************************* Action ************************************************************
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == " Final Fantasy") field.setText(" Final Fantasy");
else if(e.getActionCommand() == " Quake") field.setText(" Quake");
else if(e.getActionCommand() == " Crash Bandicoot") field.setText(" Crash bandicoot");
else if(e.getActionCommand() == " Halo") field.setText(" Halo");
else if(e.getActionCommand() == "Cancel") field.setText("");
else if(e.getActionCommand() == "Ok" || e.getActionCommand() == "") {
try{
FileWriter fw = new FileWriter("Survey2Results.txt", true);
char result = 0;
if(field.getText().equals(" Final Fantasy")) result = 1;
else if(field.getText().equals(" Quake")) result = 2;
else if(field.getText().equals(" Crash bandicoot")) result = 3;
else if(field.getText().equals(" Halo")) result = 4;
else if(field.getText().equals(null)) result = 0;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
fw.write(result);
fw.close();
getContentPane().remove(field);
getContentPane().remove(ok);
getContentPane().remove(clear);
getContentPane().remove(surveypic);
getContentPane().remove(rb1);
getContentPane().remove(rb2);
getContentPane().remove(rb3);
getContentPane().remove(rb4);
getContentPane().remove(menubar);
String out;
FileReader fr = new FileReader("Survey2Results.txt");
BufferedReader br = new BufferedReader(fr);
out = br.readLine();
int ff=0, q=0, h=0, cb =0;
for(int i = 0; i < out.length(); i++){
int nums[] = { out.charAt(i)};
if(out.charAt(i) == '1') ff++;
else if(out.charAt(i) == '2') q++;
else if(out.charAt(i) == '3') cb++;
else if(out.charAt(i) == '4') h++;
res = new JLabel(results);
FinalFant = new JLabel("Final Fantasy, currently has : " + ff + " Votes.");
Quak = new JLabel("Quake, currently has : " + q + " votes.");
Crash = new JLabel("Crash Bandicoot, currently has : " + cb + " votes.");
Hal = new JLabel("Halo, currently has : " + h + " votes.");
fr.close();
getContentPane().setLayout(null);
getContentPane().add(res);
getContentPane().add(FinalFant);
getContentPane().add(Quak);
getContentPane().add(Crash);
getContentPane().add(Hal);
res.setBounds(390,50,200,100);
FinalFant.setBounds(30,200,300,50);
Quak.setBounds(400,200,200,50);
Crash.setBounds(30,400,200,50);
Hal.setBounds(400,400,200,50);
repaint();
}catch(IOException exc){}
Thanks Matt.Do you have a specific problem or question, or would you just like us to do your homework for you?
If you have a specific question about how to use a certain class or what's creating an exception in your code, we're all more than happy to help out. Otherwise, if you neeed help in designing your project, you should probably see your prof. a TA, or a friend
Hi. I am 17 years old at college in the Hertfordshire
region studying java programming. I am really stuck
on this program and would be really greatful for some
help on this. Below is my program and i am trying to
create a survey in which the user clicks on the radio
button, then when ok is pressed, the corresponding
number is written to a seperate file and then the
file is called and displayed to create a sccesful
display of previous and present results. PLEASE
HELP!!! -
Hi!
I have build an application in java. now i want to write a help file for the user that contains the explanation of the program,about how to use it(like menu help in internet explorer)
Do you have any idea where should i write the information?using JTextArea? i don't have any idea about it, please help me how it usually be done.
I have prepare 1 menu for help in menu bar, but i don't know what is the action for it.
thanks a lot.You can write the help file in HTML and then use a
JEditorPane to display it. You need to use the
setPage(url) method to set the contents of the
JEditorPane. It works fine though and since you can
use HTML, you can include screen sots of your program
as long as they are gif or jpeg.or bmp or png - or anything else that is html supported
Look at JTextPane (also uses 'setPage("\some.html")' ) inside a JScrollPane. Nice classes to work with. -
Retrieve all data on textArea question ---please help
hi, I am writing a program that will retrieve all data from Access to JTextArea. For example, DB has 10 records and I would like to show all 10 records at once on JTextArea. However, in my code, i can retrieve the last records only. The following is part of my code, please help me to clear my thought. I think i got the idea about loop but dunno how to do. thanks.
//the data access class about sql query
public static String retrieve() throws SQLException
String query = "SELECT ClockIn, ClockOut FROM TimeCard";
Statement statement = connection.createStatement();
ResultSet r = statement.executeQuery(query);
String t3 = null;
while(r.next())
String t1 = r.getString(1);
String t2 = r.getString(2);
t3 = t1 + " " + t2 + "\n";
statement.close();
return t3;
//the JTextArea class
public void performUserDisplay()
testTime tTime = new testTime();
try
testTime.connect();
message.setText(tTime.retrieve()); //*** i think here has the problem too but i have no idea what to do
testTime.disconnect();
catch(Exception e)
System.out.println("Exception");
I know maybe i should use message.append() but still, I am sort of new to java, so i don't really know how to do it.
Please help me and thanks in advanceChange
String t3 = null;
while(r.next())
String t1 = r.getString(1);
String t2 = r.getString(2);
t3 = t1 + " " + t2 + "\n";
statement.close();
return t3;
...to
StringBuffer res = new StringBuffer();
while(r.next())
res.append(r.getString(1)).append(" ").append(r.getString(2)).append("\n");
statement.close();
return res.toString(); -
Need help pulling data from an outside file into my array. Please help =)
Hi,
I need help adapting my array to read the interest rates from an outside file. Here is my code and the outside file I wrote. Please help. Oh, the array in question is on line 259, inside of my calculation() method.
Thanks...
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.io.*;
import javax.swing.JOptionPane;
public class Workshop5 extends JFrame implements ActionListener
//declare gui components
//declare labels
JPanel contentPane = new JPanel();
JPanel graphPane = new JPanel();
JLabel instructionLabel = new JLabel();
JLabel amountLabel = new JLabel();
JLabel orLabel = new JLabel();
JLabel comboBoxLabel = new JLabel();
JLabel termLabel = new JLabel();
JLabel rateLabel = new JLabel();
JLabel calcLabel = new JLabel();
JLabel paymentLabel = new JLabel();
JLabel tableLabel = new JLabel();
//declare font object
Font labelFont = new Font("Tahoma", Font.PLAIN, 16);
//declare text fields
JTextField amountField = new JTextField(20);
JTextField termField = new JTextField(20);
JTextField rateField = new JTextField(20);
JTextField paymentField= new JTextField(20);
//declare combo box for loan selection
JComboBox comboBox = new JComboBox();
//declare button group and radio buttons
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton enterRadioButton = new JRadioButton("Enter amount, term, & rate", true);
JRadioButton selectRadioButton = new JRadioButton("Select a preset term/rate loan", false);
//declare button objects
JButton clearButton = new JButton();
JButton calcButton = new JButton();
JButton quitButton = new JButton();
//declare text area for amortization
JTextArea amortTextArea = new JTextArea();
JTextArea testTextArea = new JTextArea();
//declare scroll bar for amortization table
JScrollPane scrollPane = new JScrollPane(amortTextArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public Workshop5()
instructionLabel.setText("Choose one of the following payment calculation options:");
instructionLabel.setFont(new Font("Tahoma",Font.PLAIN,16));
//adds both buttons to button group
buttonGroup.add(enterRadioButton);
buttonGroup.add(selectRadioButton);
enterRadioButton.setFont(labelFont);
enterRadioButton.setBackground(Color.WHITE);
enterRadioButton.setContentAreaFilled(false);
enterRadioButton.addActionListener(this); //adds action listener to enter radio button
orLabel.setText("OR");
orLabel.setFont(new Font("Tahoma",Font.BOLD,18));
selectRadioButton.setFont(labelFont);
selectRadioButton.setBackground(Color.WHITE);
selectRadioButton.setContentAreaFilled(false);
selectRadioButton.addActionListener(this); //adds action listener to select radio button
amountLabel.setText("Enter mortgage amount");
amountLabel.setFont(new Font("Tahoma", Font.PLAIN,16));
amountField.requestFocusInWindow();
termLabel.setText("Enter term length in years:");
termLabel.setFont(new Font("Tahoma", Font.PLAIN,14));
rateLabel.setText("Enter interest rate:");
rateLabel.setFont(new Font("Tahoma", Font.PLAIN,14));
comboBoxLabel.setText("Select a loan:");
comboBoxLabel.setFont(new Font("Tahoma", Font.PLAIN,14));
comboBox.setBackground(new Color(255,255,255));
comboBox.setFont(new Font("Tahoma", Font.PLAIN, 14));
comboBox.setEnabled(false);
ComboBox();
calcLabel.setText("Press Calculate button to determine monthly payment.");
calcLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
calcButton.setText("Calculate");
calcButton.setFont(new Font("Tahoma", Font.BOLD, 14));
calcButton.addActionListener(this);
//define monthly payment label
paymentLabel.setText("Monthly payment:");
paymentLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
//define monthly payment text field
paymentField.setFont(new Font("Tahoma", Font.BOLD,16));
paymentField.setBackground(new Color(255,255,255));
paymentField.setEditable(false);
//define clear button
clearButton.setText("Clear");
clearButton.setFont(new Font("Tahoma", Font.BOLD,14));
clearButton.addActionListener(this);
//define quit button
quitButton.setText("Quit");
quitButton.setFont(new Font("Tahoma", Font.BOLD,14));
quitButton.addActionListener(this);
tableLabel.setText("Amoritization Table");
tableLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
graphPane.setBackground(Color.WHITE);
//add components to content
getContentPane().add(contentPane);
contentPane.setLayout(null);
addComponent(contentPane, instructionLabel, 80,10,450,26);
addComponent(contentPane, enterRadioButton, 30,40,220,30);
addComponent(contentPane, orLabel, 280,40,100,30);
addComponent(contentPane, selectRadioButton, 335,40,350,30);
addComponent(contentPane, amountLabel, 100,80,220,26);
addComponent(contentPane, amountField, 300,80,150,26);
addComponent(contentPane, termLabel, 15,125,200,30);
addComponent(contentPane, termField, 195,125,125,30);
addComponent(contentPane, rateLabel, 62,160,200,30);
addComponent(contentPane, rateField, 195,165,125,30);
addComponent(contentPane, comboBoxLabel, 400,125,200,26);
addComponent(contentPane, comboBox, 400,155,150,30);
addComponent(contentPane, calcLabel, 100,200,400,30);
addComponent(contentPane, calcButton, 250,240,100,30);
addComponent(contentPane, paymentLabel, 150,285,200,30);
addComponent(contentPane, paymentField, 300,285,100,30);
addComponent(contentPane, clearButton, 100,330,100,30);
addComponent(contentPane, quitButton, 400,330,100,30);
addComponent(contentPane, tableLabel, 200,370,300,26);
addComponent(contentPane, scrollPane, 10,400,450,360);
addComponent(contentPane, graphPane, 475,400,305,360);
//add window listener to close window when user presses X
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
pack();
//method to add components
private void addComponent(Container container, Component c, int x, int y, int width, int height)
c.setBounds(x, y, width, height);
container.add(c);
//action performed method
public void actionPerformed(ActionEvent event)
Object source = event.getSource();
if (source == calcButton)
Calculate();
if (source == clearButton)
Clear();
if (source == quitButton)
Exit();
//defines active area based on user selection of mortgage calculation method
//if user chooses to enter the mortgage manually, combo box fields are inactive
if (source == enterRadioButton)
comboBox.setEnabled(false);
termField.setEnabled(true);
termField.setEditable(true);
rateField.setEnabled(true);
rateField.setEditable(true);
amountField.setText("");
amountField.requestFocusInWindow();
termField.setText("");
rateField.setText("");
paymentField.setText("");
amortTextArea.setText("");
//if user chooses to select from a preset mortgage, rate and term fields are inactive
if (source == selectRadioButton)
comboBox.setEnabled(true);
termField.setEnabled(false);
termField.setEditable(false);
rateField.setEnabled(false);
rateField.setEditable(false);
amountField.setText("");
amountField.requestFocusInWindow();
termField.setText("");
rateField.setText("");
paymentField.setText("");
amortTextArea.setText("");
}//end of action performed method
//combo box method
public void ComboBox()
String[] LoanArray = {" 7 years at 5.35 %", " 15 years at 5.50 %", " 30 years at 5.75 %"};
for (int i = 0; i < LoanArray.length; i++)
comboBox.addItem(LoanArray);
}//end combo box method
//calculation method
void Calculate()
//resets fields
paymentField.setText("");
amortTextArea.setText("");
//calculation variables
NumberFormat currency = NumberFormat.getCurrencyInstance();
int [] termArray = {7, 15, 30}; //array of years
double [] yearlyInterestArray = {5.35, 5.5, 5.75}; //array of interest
int totalMonths = 0; //total months
double Loan = 0.0; //amount of loan
double MonthlyInterest = 0.0; //monthly interest rate
double Payment = 0.0; //calculate payment
double monthlyPayment = 0.0; //calculate monthly payment
double Interest = 0.0; //variable for Interest Array input
int Term = 0; //variable for Term Array input
double NewMonthlyInterest = 0.0; //new interest amount
double NewLoan = 0.0; //new loan amount
double Reduction = 0.0; //principle reduction
//validate input
try
Loan = Double.parseDouble(amountField.getText());
catch (NumberFormatException e)
JOptionPane.showMessageDialog(null,"Invalid Entry. Please enter valid loan amount.", "Error", JOptionPane.WARNING_MESSAGE);
//resets input fields after error message
amountField.setText("");
amountField.requestFocusInWindow();
//if select button is chosen
if (selectRadioButton.isSelected())
int index = comboBox.getSelectedIndex();
Term = termArray[index];
Interest = yearlyInterestArray[index];
//if user chooses to enter mortgage information
else
if (enterRadioButton.isSelected())
//validates input
try
Term = Integer.parseInt(termField.getText());
catch (NumberFormatException e)
JOptionPane.showMessageDialog(null,"Invalid entry. Please enter valid term length in years.", "Error",JOptionPane.WARNING_MESSAGE);
//clears fields after error message
termField.setText("");
termField.requestFocusInWindow();
try
Interest = Double.parseDouble(rateField.getText());
catch (NumberFormatException e)
JOptionPane.showMessageDialog(null,"Invalid entry. Please enter valid rate (without percent symbol).","Error",JOptionPane.WARNING_MESSAGE);
//clears fields after error message
rateField.setText("");
rateField.requestFocusInWindow();
//perform calculations
if (Loan > 0)
Loan = Double.parseDouble(amountField.getText());
MonthlyInterest = (Interest / 12)/100;
totalMonths = Term * 12;
monthlyPayment = Loan * MonthlyInterest *(Math.pow((1 + MonthlyInterest), totalMonths)/(Math.pow((1 + MonthlyInterest), totalMonths)-1));
paymentField.setText("" + currency.format(monthlyPayment));
//send information to amortization text area
amortTextArea.append("Number\t");
amortTextArea.append(" Amount\t");
amortTextArea.append("Interest\t");
amortTextArea.append("Principle\t");
amortTextArea.append("Balance\n");
NewLoan = Loan;
for (int i = 1; i <= totalMonths; i++)
NewMonthlyInterest = MonthlyInterest * NewLoan;
Reduction = monthlyPayment - NewMonthlyInterest;
NewLoan = NewLoan - Reduction;
amortTextArea.append(" " + i +"\t");
amortTextArea.append(" " + currency.format(monthlyPayment) + "\t");
amortTextArea.append(" " + currency.format(NewMonthlyInterest)+ "\t");
amortTextArea.append(" " + currency.format(Reduction) + "\t");
amortTextArea.append(" " + currency.format(NewLoan) + "\n");
//resets fields if loan amount is less than zero
if((Loan <= 0 || Term <= 0 || Interest <= 0))
paymentField.setText("");
amortTextArea.setText("");
}//end calcualtion method
//clear method
void Clear()
amountField.setText("");
amountField.requestFocusInWindow();
termField.setText("");
rateField.setText("");
paymentField.setText("");
amortTextArea.setText("");
}//end of clear method
//main method
public static void main(String args[])
Workshop5 f = new Workshop5();
f.setTitle("Carol's Mortgage Calculator");
f.setBounds(200,100,800,800);
f.setResizable(false);
f.setVisible(true);
}//end of main method
//exit method
void Exit()
System.exit(0);
}//end of exit method
}//program end
My data file is called: "InterestData.dat" and only contains the following text:
5.35, 5.5, 5.75Ok, now I am getting this error message:
cannot resolve symbol method lenght()
Please help me out here, this is due tomorrow and I've been killing myself on it...
attaching program with revised code included, see beginning of calculation method line 250:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.* ;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.io.*;
import javax.swing.JOptionPane;
public class Workshop5 extends JFrame implements ActionListener
//declare gui components
//declare labels
JPanel contentPane = new JPanel();
JPanel graphPane = new JPanel();
JLabel instructionLabel = new JLabel();
JLabel amountLabel = new JLabel();
JLabel orLabel = new JLabel();
JLabel comboBoxLabel = new JLabel();
JLabel termLabel = new JLabel();
JLabel rateLabel = new JLabel();
JLabel calcLabel = new JLabel();
JLabel paymentLabel = new JLabel();
JLabel tableLabel = new JLabel();
//declare font object
Font labelFont = new Font("Tahoma", Font.PLAIN, 16);
//declare text fields
JTextField amountField = new JTextField(20);
JTextField termField = new JTextField(20);
JTextField rateField = new JTextField(20);
JTextField paymentField= new JTextField(20);
//declare combo box for loan selection
JComboBox comboBox = new JComboBox();
//declare button group and radio buttons
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton enterRadioButton = new JRadioButton("Enter amount, term, & rate", true);
JRadioButton selectRadioButton = new JRadioButton("Select a preset term/rate loan", false);
//declare button objects
JButton clearButton = new JButton();
JButton calcButton = new JButton();
JButton quitButton = new JButton();
//declare text area for amortization
JTextArea amortTextArea = new JTextArea();
JTextArea testTextArea = new JTextArea();
//declare scroll bar for amortization table
JScrollPane scrollPane = new JScrollPane(amortTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public Workshop5()
instructionLabel.setText("Choose one of the following payment calculation options:");
instructionLabel.setFont(new Font("Tahoma",Font.PLAIN,16));
//adds both buttons to button group
buttonGroup.add(enterRadioButton);
buttonGroup.add (selectRadioButton);
enterRadioButton.setFont(labelFont);
enterRadioButton.setBackground(Color.WHITE);
enterRadioButton.setContentAreaFilled(false);
enterRadioButton.addActionListener(this); //adds action listener to enter radio button
orLabel.setText("OR");
orLabel.setFont(new Font("Tahoma",Font.BOLD,18));
selectRadioButton.setFont(labelFont);
selectRadioButton.setBackground(Color.WHITE);
selectRadioButton.setContentAreaFilled(false);
selectRadioButton.addActionListener (this); //adds action listener to select radio button
amountLabel.setText("Enter mortgage amount");
amountLabel.setFont(new Font("Tahoma", Font.PLAIN,16));
amountField.requestFocusInWindow();
termLabel.setText("Enter term length in years:");
termLabel.setFont(new Font("Tahoma", Font.PLAIN,14));
rateLabel.setText("Enter interest rate:");
rateLabel.setFont(new Font("Tahoma", Font.PLAIN,14));
comboBoxLabel.setText("Select a loan:");
comboBoxLabel.setFont(new Font("Tahoma", Font.PLAIN,14));
comboBox.setBackground(new Color(255,255,255));
comboBox.setFont(new Font("Tahoma", Font.PLAIN, 14));
comboBox.setEnabled(false);
ComboBox();
calcLabel.setText("Press Calculate button to determine monthly payment.");
calcLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
calcButton.setText("Calculate");
calcButton.setFont(new Font("Tahoma", Font.BOLD, 14));
calcButton.setBackground(new Color(202,255,112));
calcButton.addActionListener(this);
//define monthly payment label
paymentLabel.setText("Monthly payment:");
paymentLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
//define monthly payment text field
paymentField.setFont (new Font("Tahoma", Font.BOLD,16));
paymentField.setBackground(new Color(255,255,255));
paymentField.setEditable(false);
//define clear button
clearButton.setText("Clear");
clearButton.setFont(new Font("Tahoma", Font.BOLD,14));
clearButton.setBackground(new Color(202,255,112));
clearButton.addActionListener(this);
//define quit button
quitButton.setText("Quit");
quitButton.setFont(new Font("Tahoma", Font.BOLD,14));
quitButton.setBackground(new Color(202,255,112));
quitButton.addActionListener(this);
//define label for amortization table
tableLabel.setText ("Amoritization Table");
tableLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
graphPane.setBackground(Color.WHITE);
//add components to content
getContentPane().add(contentPane);
contentPane.setLayout(null);
addComponent(contentPane, instructionLabel, 80,10,450,26);
addComponent(contentPane, enterRadioButton, 30,40,220,30);
addComponent(contentPane, orLabel, 280,40,100,30);
addComponent(contentPane, selectRadioButton, 335,40,350,30);
addComponent(contentPane, amountLabel, 100,80,220,26);
addComponent(contentPane, amountField, 300,80,150,26);
addComponent(contentPane, termLabel, 15,125,200,30);
addComponent(contentPane, termField, 195,125,125,30);
addComponent(contentPane, rateLabel, 62,160,200,30);
addComponent(contentPane, rateField, 195,165,125,30);
addComponent(contentPane, comboBoxLabel, 400,125,200,26);
addComponent(contentPane, comboBox, 400,155,150,30);
addComponent(contentPane, calcLabel, 100,200,400,30);
addComponent(contentPane, calcButton, 250,240,100,30);
addComponent(contentPane, paymentLabel, 150,285,200,30);
addComponent(contentPane, paymentField, 300,285,100,30);
addComponent(contentPane, clearButton, 100,330,100,30);
addComponent(contentPane, quitButton, 400,330,100,30);
addComponent(contentPane, tableLabel, 200,370,300,26);
addComponent(contentPane, scrollPane, 10,400,450,360);
addComponent(contentPane, graphPane, 475,400,305,360);
//add window listener to close window when user presses X
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
pack();
//method to add components
private void addComponent(Container container, Component c, int x, int y, int width, int height)
c.setBounds(x, y, width, height);
container.add(c);
//action performed method
public void actionPerformed(ActionEvent event)
Object source = event.getSource();
if (source == calcButton)
Calculate();
if (source == clearButton)
Clear();
if (source == quitButton)
Exit();
//defines active area based on user selection of mortgage calculation method
//if user chooses to enter the mortgage manually, combo box fields are inactive
if (source == enterRadioButton)
comboBox.setEnabled(false);
termField.setEnabled(true);
termField.setEditable(true);
rateField.setEnabled(true);
rateField.setEditable(true);
amountField.setText ("");
amountField.requestFocusInWindow();
termField.setText("");
rateField.setText("");
paymentField.setText ("");
amortTextArea.setText("");
//if user chooses to select from a preset mortgage, rate and term fields are inactive
if (source == selectRadioButton)
comboBox.setEnabled(true);
termField.setEnabled(false);
termField.setEditable(false);
rateField.setEnabled (false);
rateField.setEditable(false);
amountField.setText("");
amountField.requestFocusInWindow();
termField.setText ("");
rateField.setText("");
paymentField.setText("");
amortTextArea.setText("");
}//end of action performed method
//combo box method
public void ComboBox()
String[] LoanArray = {" 7 years at 5.35 %", " 15 years at 5.50 %", " 30 years at 5.75 %"};
for (int i = 0; i < LoanArray.length; i++)
comboBox.addItem(LoanArray);
}//end combo box method
//calculation method
void Calculate()
//resets fields
paymentField.setText("");
amortTextArea.setText("");
//calculation variables
NumberFormat currency = NumberFormat.getCurrencyInstance();
//declare input stream object
InputStream istream;
//create a file object to refer to the outside file
File interestData = new File("InterestFile.dat");
//assign instream to the new file object
istream = new FileInputStream(interestData);
try
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(interestData));
String line = "";
while((line = in.readLine()) != null)
sb.append(line);
in.close();
String fileData = sb.toString();
String[] splitData = fileData.split(", ");
double [] yearlyInterestArray = new double[splitData.length()];
for(int j = 0; j < splitData.length(); j++)
yearlyInterestArray[j] = new Double(splitData[j]).doubleValue();
catch (IOException e)
JOptionPane.showMessageDialog(null, "File does not exist." + e.getMessage());
int [] termArray = {7, 15, 30}; //array of years
double [] yearlyInterestArray = { 5.35, 5.5, 5.75}; //array of interest
int totalMonths = 0; //total months
double Loan = 0.0; //amount of loan
double MonthlyInterest = 0.0; //monthly interest rate
double Payment = 0.0; //calculate payment
double monthlyPayment = 0.0; //calculate monthly payment
double Interest = 0.0; //variable for Interest Array input
int Term = 0; //variable for Term Array input
double NewMonthlyInterest = 0.0; //new interest amount
double NewLoan = 0.0; //new loan amount
double Reduction = 0.0; //principle reduction
//validate input
try
Loan = Double.parseDouble(amountField.getText());
catch (NumberFormatException e)
JOptionPane.showMessageDialog(null,"Invalid Entry. Please enter valid loan amount.", "Error", JOptionPane.WARNING_MESSAGE);
//resets input fields after error message
amountField.setText("");
amountField.requestFocusInWindow ();
//if select button is chosen
if (selectRadioButton.isSelected())
int index = comboBox.getSelectedIndex ();
Term = termArray[index];
Interest = yearlyInterestArray[index];
//if user chooses to enter mortgage information
else
if (enterRadioButton.isSelected())
//validates input
try
Term = Integer.parseInt(termField.getText());
catch (NumberFormatException e)
JOptionPane.showMessageDialog(null,"Invalid entry. Please enter valid term length in years.", "Error",JOptionPane.WARNING_MESSAGE);
//clears fields after error message
termField.setText("");
termField.requestFocusInWindow();
try
Interest = Double.parseDouble(rateField.getText());
catch (NumberFormatException e)
JOptionPane.showMessageDialog(null,"Invalid entry. Please enter valid rate (without percent symbol).","Error",JOptionPane.WARNING_MESSAGE);
//clears fields after error message
rateField.setText("");
rateField.requestFocusInWindow();
//perform calculations
if (Loan > 0)
Loan = Double.parseDouble(amountField.getText ());
MonthlyInterest = (Interest / 12)/100;
totalMonths = Term * 12;
monthlyPayment = Loan * MonthlyInterest *(Math.pow ((1 + MonthlyInterest), totalMonths)/(Math.pow((1 + MonthlyInterest), totalMonths)-1));
paymentField.setText("" + currency.format(monthlyPayment));
//send information to amortization text area
amortTextArea.append("Number\t");
amortTextArea.append(" Amount\t");
amortTextArea.append("Interest\t");
amortTextArea.append("Principle\t");
amortTextArea.append("Balance\n");
NewLoan = Loan;
for (int i = 1; i <= totalMonths; i++)
NewMonthlyInterest = MonthlyInterest * NewLoan;
Reduction = monthlyPayment - NewMonthlyInterest;
NewLoan = NewLoan - Reduction;
amortTextArea.append(" " + i +"\t");
amortTextArea.append (" " + currency.format(monthlyPayment) + "\t");
amortTextArea.append(" " + currency.format(NewMonthlyInterest)+ "\t");
amortTextArea.append(" " + currency.format(Reduction) + "\t");
amortTextArea.append(" " + currency.format(NewLoan) + "\n");
//resets fields if loan amount is less than zero
if((Loan <= 0 || Term <= 0 || Interest <= 0))
paymentField.setText("");
amortTextArea.setText("");
}//end calcualtion method
//clear method
void Clear()
amountField.setText("");
amountField.requestFocusInWindow();
termField.setText("");
rateField.setText("");
paymentField.setText("");
amortTextArea.setText("");
}//end of clear method
//main method
public static void main(String args[])
Workshop5 f = new Workshop5();
f.setTitle("Carol's Mortgage Calculator");
f.setBounds(200,100,800,800);
f.setResizable(false);
f.setVisible(true);
}//end of main method
//exit method
void Exit()
System.exit(0);
}//end of exit method
}//program end -
Beyond me ! please help..
try
String k= input.getText();//variable input is a JTextField
int m=Integer.parseInt(k);
Vector v=new Vector();
v.add(new Integer(m));
Collections.sort(v);
int a = ((Integer) v.firstElement()).intValue();
output.setText(a+"");// variable output is a JTextArea
catch( NumberFormatException x)
JOptionPane.showMessageDialog(null,
"You Entered wrong input. Try again" );
What i am trying to do is get numbers from JTextField, store
them in a Vector, sort out the numbers(0-9) order and display
in a TextArea one number per line. Every thing works fine except..
I am getting
1-unsorted numbers
2-input TextField is not clearing the first input
3-output TextArea is displaying all the numbers in one contineous
line.
Will appriciate solution,referal or whatever.Please help!
Thanks in advanceimport java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class hiaahrma extends JFrame implements ActionListener {
JTextField field = new JTextField(5);
JTextArea textArea = new JTextArea(5,6);
Vector vector = new Vector();
public hiaahrma() {
field.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridwidth = gbc.REMAINDER;
panel.add(field, gbc);
panel.add(new JScrollPane(textArea), gbc);
getContentPane().add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
public void actionPerformed(ActionEvent e) {
JTextField field = (JTextField)e.getSource();
String text = field.getText();
//System.out.println("text = " + text);
int nextInt;
try
for(int j = 0; j < text.length(); j++) {
nextInt = Integer.parseInt(String.valueOf(text.charAt(j)));
vector.add(j, new Integer(nextInt));
Collections.sort(vector);
textArea.setText("");
for(int j = 0; j < vector.size(); j++) {
nextInt = ((Integer)vector.get(j)).intValue();
textArea.setText(textArea.getText() + String.valueOf(nextInt) + "\n");
catch(NumberFormatException nfe) {
System.out.println("NFE: " + nfe.toString());
return;
vector.removeAllElements();
//System.out.println("vector.size() = " + vector.size());
public static void main(String[] args) {
new hiaahrma();
} -
I wrote this simple frame work for a project and
when I run it, it has a small square the I can't
write to in the area labled "Graphics". I try
to write "Blue Title" and a small square blocks
out the "e " in "Blue Title". Will someone please
help me with this, I don't know why the small
square is their or how to get rid of it - thanks.
The complete program follows, so all you have to
do is copy and paste it into a file, compile and
run it, to see what I am talking about.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
//import java.io.*;
class ButtonPanel extends JPanel
public ButtonPanel()
JPanel panel = new JPanel();
this.setBorder(new TitledBorder("Button Panel"));
this.add(panel);
JButton getDataStr = new JButton("Echo Data");
panel.add(getDataStr);
JCheckBox force00 = new JCheckBox("Force: (0, 0)", false);
panel.add(force00);
JButton compute = new JButton("Compute");
panel.add(compute);
class PlotterPanel extends JPanel
static final int LFT_MARGIN = 60;
static final int RHT_MARGIN = 20;
static final int TOP_MARGIN = 20;
static final int BOT_MARGIN = 20;
private int plotWidth = 400;
private int plotHeight = 400;
private int width = plotWidth+LFT_MARGIN+RHT_MARGIN;
private int height = plotHeight+TOP_MARGIN+BOT_MARGIN;
public PlotterPanel()
JPanel panel = new JPanel();
this.setPreferredSize(new Dimension(width, height));
this.add(panel);
public void paintComponent(Graphics graphics)
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D)graphics;
setBackground(Color.white);
setForeground(Color.blue);
graphics2D.drawString("Blue Title", (getWidth()/2)-25, 15);
class PlotPanel extends JPanel
public PlotPanel()
JPanel panel = new JPanel();
this.setBorder(new TitledBorder("Graphics"));
panel.setLayout(new BorderLayout());
this.add(panel);
JPanel plotterPanel = new PlotterPanel();
panel.add(plotterPanel, BorderLayout.CENTER);
class PasteInputPanel extends JPanel
public PasteInputPanel()
JPanel panel = new JPanel();
this.setBorder(new TitledBorder("Paste Input"));
panel.setLayout(new BorderLayout());
this.add(panel);
JTextArea results = new JTextArea("Paste TextArea", 21, 20);
results.setEditable(true);
panel.add(results);
class FileInputPanel extends JPanel
public FileInputPanel()
JPanel panel = new JPanel();
this.setBorder(new TitledBorder("File Input"));
panel.setLayout(new BorderLayout());
this.add(panel);
JLabel fileLbl = new JLabel(" File: ");
panel.add(fileLbl, BorderLayout.WEST);
JTextField dataFile = new JTextField("./FileName.ext");
panel.add(dataFile, BorderLayout.EAST);
JLabel statusLbl = new JLabel("Status: ");
panel.add(statusLbl, BorderLayout.SOUTH);
class InputPanel extends JPanel
public InputPanel()
JPanel panel = new JPanel();
this.setBorder(new TitledBorder("Input Panel"));
panel.setLayout(new BorderLayout());
this.add(panel);
JPanel fileInput = new FileInputPanel();
panel.add(fileInput, BorderLayout.NORTH);
JPanel pasteInput = new PasteInputPanel();
panel.add(pasteInput, BorderLayout.CENTER);
class ResultsPanel extends JPanel
public ResultsPanel()
JPanel panel = new JPanel();
this.setBorder(new TitledBorder("Results Panel"));
this.add(panel);
JTextArea results = new JTextArea("Results TextArea", 5, 68);
results.setEditable(true);
panel.add(results);
class GuiBugPanel extends JPanel
public GuiBugPanel()
this.setLayout(new BorderLayout());
this.add(new ResultsPanel(), BorderLayout.NORTH);
this.add(new InputPanel(), BorderLayout.WEST);
this.add(new PlotPanel(), BorderLayout.CENTER);
this.add(new ButtonPanel(), BorderLayout.SOUTH);
class GuiBugFrame extends JFrame
public GuiBugFrame(String title)
super(title);
this.getContentPane().add(new GuiBugPanel());
public class GuiBug
public static void main(String[] args)
final GuiBugFrame frame = new GuiBugFrame("GuiBugFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((screenSize.width - frame.getWidth())/2, (screenSize.height - frame.getHeight())/2);
frame.setVisible(true);
}bbritta:
I commented out the line:
this.add(panel);
and that small square disappeared - many thanks for
your help - I can't believe that I spent so much
time looking for a fix and you came up with it
in a very short time - thanks again.
Maybe you are looking for
-
24" iMac 2.8GHz "Color Profile"
Hello,what do you think is the best Color Profile in System Preferences for your 24" Aluminum iMac?There are 14 different profiles and I do not notice much of a difference between most of them.I am using the Wide Gamut RGB Profile.I just would like s
-
Atan2 equivalent in a Formula Node?
Does anyone have an atan2 (2-input inverse arctangent) equivalent for use in a Formula Node? I was surprised to see that such a common function wasn't supported in Formula Nodes. Without it, my easily readable Formula Node equations get junked and be
-
0PS_C02 update rules are not getting activated when i install 4rm BI conten
Hi All, In BI Dev. sys for PS module- Project System - Dates 0PS_C02 cube- update rules are not getting activated when i install from the business content and its giving the error " IC=0PS_C02 IS=0PS_DAT_PRJ error when checking the update rules
-
Creating FlatFile for Hierarchies
Hi, Could any one give me the steps involved in creating a flat file to load the hierarchies in the BW system. Any documentation is appreciated. Thanks,
-
My Compaq Presario CQ57 has been crashing a lot lately so I did a system diagnostics test. After the Quick test I got a 'Hard Disk 1 Fault' error. Could someone please explain this to me and help me with a solution to it?