Public Variable Problem

B

Bob Rivers

Hi,

I'm having a weird problem. I don't know if I am doing a basic
mistake, or it's a problem.

Basically, I want to share a variable value between to methods. I have
tried many differents ways to do it. The last one, that I posted
bellow, I am trying to do it by using a set/get methods.

My problem is that I usually do programs using servlets. I never user
awt/swing, so maybe I am doing something wrong.

I receive a value through the class constructor. This value will be
used on another method. I am using some System.out in order to check
if the value is properly received and passed.

The constructor and set methods are ok. They show the value. But the
get and paint prints nothing.

I am posting two classes. The first one that has the problem. The
second one is just for peiple that wants to compile and see the
problem.

TIA,

Bob

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;

public class mjlDisplayPanel extends JPanel {

public String lines = null;

public mjlDisplayPanel (String srcCode) {
System.out.println("constructor -> " + srcCode);
setLines(srcCode);
}

public void paintComponent(Graphics g) {
String sourceCode = getLines();
String[] result =
sourceCode.split(DefaultEditorKit.EndOfLineStringProperty);

for (int x = 0; x < result.length; x++)
System.out.println("paint -> " + result[x]);

super.paintComponent(g);
g.drawLine(0, 0, 9, 9);
}

public void setLines(String args){
System.out.println("set -> " + args);
lines = args;
}

public String getLines(){
System.out.println("get -> " + lines);
return lines;
}
}

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

public class myJLogo implements ActionListener {
JPanel mainPanel;
JScrollPane sourcePanel;
mjlDisplayPanel displayPanel;
JTextField textField = null;
JTextArea textArea = null;

public myJLogo() {
textArea = new JTextArea(20, 40);
textArea.setEditable(true);
sourcePanel = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

displayPanel = new mjlDisplayPanel(textArea.getText());

mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,2,5,5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

JButton button = new JButton("Desenhar");
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayPanel = new mjlDisplayPanel(textArea.getText());
}
});

mainPanel.add(sourcePanel);
mainPanel.add(displayPanel);
mainPanel.add(button);
}

public void actionPerformed(ActionEvent event) {
// tbd
}

public static void main(String[] args) {
myJLogo mjl = new myJLogo();
JFrame mjlFrame = new JFrame("myJLogo");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}
mjlFrame.setContentPane(mjl.mainPanel);
mjlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mjlFrame.pack();
mjlFrame.setVisible(true);
}
}
 
M

Moshe Sayag

The problem has nothing to do with the value of "lines" but actually in
the other class - myJLogo. You are creating an new mjlDisplayPanel every
time the button is pressed and its "lines" value is initialized by "".

Try to modify myJLogo.java and replace

displayPanel = new mjlDisplayPanel(textArea.getText());

by

displayPanel.setLines(textArea.getText());

P.S.
According to the Java Code Convention, every class name should start
with a capital letter, so the classes should be renamed to something
like MjlDisplayPanel and MyJLogo
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367

Moshe

Bob said:
Hi,

I'm having a weird problem. I don't know if I am doing a basic
mistake, or it's a problem.

Basically, I want to share a variable value between to methods. I have
tried many differents ways to do it. The last one, that I posted
bellow, I am trying to do it by using a set/get methods.

My problem is that I usually do programs using servlets. I never user
awt/swing, so maybe I am doing something wrong.

I receive a value through the class constructor. This value will be
used on another method. I am using some System.out in order to check
if the value is properly received and passed.

The constructor and set methods are ok. They show the value. But the
get and paint prints nothing.

I am posting two classes. The first one that has the problem. The
second one is just for peiple that wants to compile and see the
problem.

TIA,

Bob

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;

public class mjlDisplayPanel extends JPanel {

public String lines = null;

public mjlDisplayPanel (String srcCode) {
System.out.println("constructor -> " + srcCode);
setLines(srcCode);
}

public void paintComponent(Graphics g) {
String sourceCode = getLines();
String[] result =
sourceCode.split(DefaultEditorKit.EndOfLineStringProperty);

for (int x = 0; x < result.length; x++)
System.out.println("paint -> " + result[x]);

super.paintComponent(g);
g.drawLine(0, 0, 9, 9);
}

public void setLines(String args){
System.out.println("set -> " + args);
lines = args;
}

public String getLines(){
System.out.println("get -> " + lines);
return lines;
}
}

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

public class myJLogo implements ActionListener {
JPanel mainPanel;
JScrollPane sourcePanel;
mjlDisplayPanel displayPanel;
JTextField textField = null;
JTextArea textArea = null;

public myJLogo() {
textArea = new JTextArea(20, 40);
textArea.setEditable(true);
sourcePanel = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

displayPanel = new mjlDisplayPanel(textArea.getText());

mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,2,5,5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

JButton button = new JButton("Desenhar");
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayPanel = new mjlDisplayPanel(textArea.getText());
}
});

mainPanel.add(sourcePanel);
mainPanel.add(displayPanel);
mainPanel.add(button);
}

public void actionPerformed(ActionEvent event) {
// tbd
}

public static void main(String[] args) {
myJLogo mjl = new myJLogo();
JFrame mjlFrame = new JFrame("myJLogo");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}
mjlFrame.setContentPane(mjl.mainPanel);
mjlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mjlFrame.pack();
mjlFrame.setVisible(true);
}
}
 
A

Andrew Thompson

| Hi,
|
| I'm having a weird problem. I don't know if I am doing a basic
| mistake, or it's a problem.

Either I have misunderstood you, or you
have made a silly mistake, add this to 'paintComponent'

g.drawString( sourceCode, 20, 20 );

HTH
 
B

Bob Rivers

Hi,

Moshe solved the problem. I create a new object every time I click the button.

Thanks,

Bob

Hi,

I'm having a weird problem. I don't know if I am doing a basic
mistake, or it's a problem.

Basically, I want to share a variable value between to methods. I have
tried many differents ways to do it. The last one, that I posted
bellow, I am trying to do it by using a set/get methods.

My problem is that I usually do programs using servlets. I never user
awt/swing, so maybe I am doing something wrong.

I receive a value through the class constructor. This value will be
used on another method. I am using some System.out in order to check
if the value is properly received and passed.

The constructor and set methods are ok. They show the value. But the
get and paint prints nothing.

I am posting two classes. The first one that has the problem. The
second one is just for peiple that wants to compile and see the
problem.

TIA,

Bob

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;

public class mjlDisplayPanel extends JPanel {

public String lines = null;

public mjlDisplayPanel (String srcCode) {
System.out.println("constructor -> " + srcCode);
setLines(srcCode);
}

public void paintComponent(Graphics g) {
String sourceCode = getLines();
String[] result =
sourceCode.split(DefaultEditorKit.EndOfLineStringProperty);

for (int x = 0; x < result.length; x++)
System.out.println("paint -> " + result[x]);

super.paintComponent(g);
g.drawLine(0, 0, 9, 9);
}

public void setLines(String args){
System.out.println("set -> " + args);
lines = args;
}

public String getLines(){
System.out.println("get -> " + lines);
return lines;
}
}

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

public class myJLogo implements ActionListener {
JPanel mainPanel;
JScrollPane sourcePanel;
mjlDisplayPanel displayPanel;
JTextField textField = null;
JTextArea textArea = null;

public myJLogo() {
textArea = new JTextArea(20, 40);
textArea.setEditable(true);
sourcePanel = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

displayPanel = new mjlDisplayPanel(textArea.getText());

mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,2,5,5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

JButton button = new JButton("Desenhar");
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayPanel = new mjlDisplayPanel(textArea.getText());
}
});

mainPanel.add(sourcePanel);
mainPanel.add(displayPanel);
mainPanel.add(button);
}

public void actionPerformed(ActionEvent event) {
// tbd
}

public static void main(String[] args) {
myJLogo mjl = new myJLogo();
JFrame mjlFrame = new JFrame("myJLogo");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}
mjlFrame.setContentPane(mjl.mainPanel);
mjlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mjlFrame.pack();
mjlFrame.setVisible(true);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top