Skip to main content

swing Problem

No replies
swebert23
Offline
Joined: 2010-03-26

from this Frame i want to send textfield values to a class in next Frame :

package applicationclient2;

/**
*
* @author Admin
*/
public class Frame extends javax.swing.JFrame {

/** Creates new form Frame */
public Frame() {
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.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(226, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(168, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(159, 159, 159))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(96, 96, 96)
.addComponent(jButton1)
.addContainerGap(106, Short.MAX_VALUE))
);

pack();
}//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

String Str=jTextField1.getText();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

and values will have to be in this blow class[ DrawingCanvas]:

public class AnotherPrintDemo extends JFrame {
DrawingCanvas canvas;

JButton setUpButton = new JButton("Page Setup");

JButton printButton = new JButton("Print");

JButton cancelButton = new JButton("Cancel");

public AnotherPrintDemo() {
super();
Container container = getContentPane();

canvas = new DrawingCanvas();
container.add(canvas);

JPanel panel = new JPanel(new GridLayout(1, 3));

ButtonListener buttonListener = new ButtonListener();
setUpButton.addActionListener(buttonListener);
panel.add(setUpButton);

printButton.addActionListener(buttonListener);
panel.add(printButton);

cancelButton.addActionListener(buttonListener);
panel.add(cancelButton);

container.add(BorderLayout.SOUTH, panel);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(650, 275);
setVisible(true);
}

class ButtonListener implements ActionListener {
PrinterJob printJob;

PageFormat pageFormat;

PrintableCanvas printableCanvas;

ButtonListener() {
printJob = PrinterJob.getPrinterJob();
pageFormat = printJob.defaultPage();
}

public void actionPerformed(ActionEvent e) {
JButton tempButton = (JButton) e.getSource();

if (tempButton.equals(setUpButton)) {
pageFormat = printJob.pageDialog(pageFormat);
printJob.validatePage(pageFormat);
} else if (tempButton.equals(printButton)) {
printableCanvas = new PrintableCanvas(pageFormat);
printJob.setPrintable(printableCanvas);

boolean ok = printJob.printDialog();
if (ok) {
try {
printJob.print();
} catch (Exception pe) {
System.out.println("Printing Exception Occured!");
pe.printStackTrace();
}
}
} else if (tempButton.equals(cancelButton)) {
printJob.cancel();
}
}
}

public static void main(String arg[]) {
new AnotherPrintDemo();
}
}

class DrawingCanvas extends JPanel {
String Str1;
Font font;

FontMetrics fontMetrics;

int w, h;

DrawingCanvas() {
setBackground(Color.white);
setSize(400, 275);

w = this.getWidth();
h = this.getHeight();

font = new Font("Dialog", Font.BOLD, 50);
fontMetrics = getFontMetrics(font);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;

paintContent(g2D, w, h);
}

public void paintContent(Graphics2D g2D, int w, int h) {
g2D.setFont(font);

g2D.drawString(Str1, 0,
(float) (0.5 * h - 1.25 * fontMetrics.getHeight()));

}
}

class PrintableCanvas implements Printable {
DrawingCanvas canvas;

PageFormat pageFormat;

public PrintableCanvas(PageFormat pf) {
pageFormat = pf;
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}

Graphics2D g2D = (Graphics2D) g;

canvas = new DrawingCanvas();

canvas.paintContent(g2D, (int) pageFormat.getImageableWidth(),
(int) pageFormat.getImageableHeight());

// successful printing of the page
return Printable.PAGE_EXISTS;
}
}

so the String str1 can get first frame textfield value
how can it be done plz help me anyone