Ed Mirsky

Greenhorn
+ Follow
since Apr 22, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ed Mirsky

I've found the solution to the problem of resetting the JScrollPane view. Use

myJScrollPane.getHorizontalScrollbar().setValue(int Pos);

myJScrollPane.getVerticalScrollbar().setValue(int Pos);
17 years ago
Ilja,

I wasn't able to get your suggestion to work. My program consists of a custom JPanel called DrawingPanel. I wrap the DrawingPanel in a JScrollPane then add the DrawingPanelto my JPanel window.

I want the user to press a "MoreData" button to update the date in the window. I used the following (see jButtonMoreDataActionPerformed below), but it didn't work. Where did I go wrong?


//..... Button to get a new set of data and reset the window
private void jButtonMoreDataActionPerformed( ActionEvent evt ){
Rectangle visible = jScrollPane.getVisibleRect();
jScrollPane.scrollRectToVisible( visible );

}

// CODE FRAGMENTS TO SHOW THE SETUP OF THE WINDOW

// Create a custom JPanel to draw on
DrawingPanel drawingPanel= new DrawingPanel();

// Add the custom JPanel to a JScrollPane
JScrollPane jScrollPane = new JScrollPane( drawingPanel );

// Add the widgets to the page
JPanel jPanel = new JPanel();
SpringLayout springLayout = new SpringLayout();
jPanel.setLayout(springLayout);

// Add the scroll pane to the page
jPanel.add( jScrollPane );

// Lay out the buttons in one row and as many columns
// as necessary, with 6 pixels of padding all around.
SpringUtilities.makeCompactGrid(
jPanel, // Container
1, 2, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad

// Add the spring layout to the grid bag
// code here

// Custom inner class
public class DrawingPanel extends javax.swing.JPanel
{
public void paintComponent(Graphics g){
super.paintComponent(g);

// stuff
}
}

Thank you for your help,

Ed
17 years ago
Hi all,

In java awt I was able to reset the scroll position of a ScrollPane with:

myScrollPane.setScrollPosition(int x, int y);
// Scrolls to the specified position within the child component.

How do I reset the scroll position to the upper left corner of my javax.swing.JScrollPane?

Thank's for any and all help,

Ed
18 years ago
Hi Everyone,

I have a problem. I�m new at java and I need help showing and hiding a JPanel. I was able to do it in awt but not in swing.

I have an inner class:

public class MycanvasA extends java.awt.Canvas
{
public void paint(Graphics g)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize();
//... stuff
}
}// class

I then create an instance of the canvas:

MycanvasA canvasA = new MycanvasA();

Since it extends java.awt.Canvas, I'm able to use setVisible to show or hide the canvas:
canvasA.setVisible(false);


I rewrote the application using swing:

public class MycanvasA extends javax.swing.JPanel
{
public void paintComponent(Graphics g)
{
//... stuff
}
}

HOW do I show or hide the canvas? I can't use:
canvasA.setVisible(false);


Thank you in advance,

Ed
18 years ago
Please help me tame the GridBagLayout.

I use a GridBagLayout and SpringUtilities to create my MainFrame JFrame window. (See the code snippets below.) The program asks a series of questions. The first question is displayed at start up. The user hits the "Next" button to bring up subsequent questions.

PROBLEM: The window resizes itself before displaying the second question, and remains set to the new size. What�s going on?

Thank you in advance,

Ed

import javax.swing.*;
import utl.SpringUtilities;

public class MainFrame extends javax.swing.Jframe implements MenuConstants
{
// FIELDS
JFrame jFrame;

//Creates new form JMainFrame
public MainFrame()
{
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
*/
private void initComponents()
{
//--- Setup the frame
JFrame.setDefaultLookAndFeelDecorated(true);
jFrame = new JFrame(sTitle+": Key Ideas � Elementary Concepts");

//--- Set up the content pane.
Container newContentPane = jFrame.getContentPane();

//--- Set the layout
newContentPane.setLayout(new GridBagLayout());
newContentPane.setBackground(Color.yellow);

//--- Set the stretchyness of rows and columns in a GridBagLayout
gbc.weightx = 0.5;// Specifies how to distribute extra horizontal space.
gbc.weighty = 0.5; // Specifies how to distribute extra horizontal space.
gbc.fill = GridBagConstraints.NONE; // Don't resize

//--- Top row
setupTopRow(newContentPane);

//--- Middle row
setupMiddleRow(newContentPane);

//--- Bottom row
setupBottomRow(newContentPane);

//--- Add content pane to frame
jFrame.setContentPane(newContentPane);

//--- Create the menus
setupMenus();

//--- Create the menu bar
setupMenuBar();

//--- Install the menu bar in the frame
jFrame.setJMenuBar(menuBar);
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); jFrame.setSize(798, 570); //(798, 780);
//jFrame.pack(); // Set to minimum/preferred size.
//frame.setVisible(true); // take out in 5.0

//--- Display the window
jFrame.setVisible(true);

}// initComponents

void addGB(Container c, Component component, int x, inty)
{
// Check and set the layout manager to GridBagLayout if necessary
if ((c.getLayout() instanceof GridBagLayout) ==false){
c.setLayout(new GridBagLayout() );
}

// Set objects position
gbc.gridx = x;
gbc.gridy = y;

// Add the object to the newContentPane
c.add(component, gbc);
}//addGB

private void setupTopRow(Container c)
{
int visibleWidth = 638,
visibleHeight = 150;

Dimension minimumSize = new Dimension(visibleWidth, visibleHeight);
canvasFigure = new DrawingPanelTop();
canvasFigure.setOpaque(true);

jlHeader.setBackground(mySet.background);
jlHeader.setOpaque(true);
jlHeader.setHorizontalAlignment(JLabel.CENTER);
jlHeader.setFont(new Font("Dialog", Font.BOLD, 14));

canvasFigure.setMinimumSize(minimumSize);
canvasFigure.setPreferredSize(minimumSize);
canvasFigure.setMaximumSize(minimumSize);
canvasFigure.setBackground(mySet.background);
gbc.gridwidth = 6;
gbc.weighty = 1.0;

// Add the widgets
JPanel jpTop = new JPanel(); // New container
SpringLayout slTop = new SpringLayout();
jpTop.setLayout(slTop);

// Top row
jpTop.add( jlHeader );
jpTop.add( canvasFigure );

//Lay out the buttons in one row and as many columns
//as necessary, with 6 pixels of padding all around.
SpringUtilities.makeCompactGrid(
jpTop, // Container
2, 1, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad

// Add the spring layout to the grid bag
addGB(c, jpTop, 0, 0);
}//setupTopRow

private void setupMiddleRow(Container c)
{
int visibleWidth = 338,
visibleHeight = 280,
canvasHeight = 1800;

// Create JSplitPane with 2 JScrollPanes each with a JPanel to hold widgets
//--- Question
canvasQuestion = new DrawingPanelLeft();
canvasQuestion.setBackground(mySet.background); // JPanel
canvasQuestion.setOpaque(true);
canvasQuestion.setMinimumSize(new Dimension(visibleWidth, canvasHeight));
canvasQuestion.setPreferredSize(new Dimension(visibleWidth, canvasHeight));
canvasQuestion.setMaximumSize(new Dimension(visibleWidth, canvasHeight));

// The visible JScrollPane in the canvas
leftScrollPane = new JScrollPane( canvasQuestion ); // Add JPanel to JScrollPane
leftScrollPane.setBackground(mySet.background);
leftScrollPane.setOpaque(true);
leftScrollPane.setMinimumSize(new Dimension(visibleWidth, visibleHeight));
leftScrollPane.setPreferredSize(new Dimension(visibleWidth, visibleHeight));
leftScrollPane.setMaximumSize(new Dimension(visibleWidth, visibleHeight));

//--- Solution
canvasSolution = new DrawingPanelRight(); // JPanel
canvasSolution.setBackground(mySet.background);
canvasSolution.setOpaque(true);
canvasSolution.setMinimumSize(new Dimension(visibleWidth, canvasHeight));
canvasSolution.setPreferredSize(new Dimension(visibleWidth, canvasHeight));
canvasSolution.setMaximumSize(new Dimension(visibleWidth, canvasHeight));

// The visible JScrollPane in the canvas
rightScrollPane = new JScrollPane( canvasSolution ); // Add JPanel to JScrollPane
rightScrollPane.setBackground(mySet.background);
rightScrollPane.setOpaque(true);
rightScrollPane.setMinimumSize(new Dimension(visibleWidth, visibleHeight));
rightScrollPane.setPreferredSize(new Dimension(visibleWidth, visibleHeight));
rightScrollPane.setMaximumSize(new Dimension(visibleWidth, visibleHeight));

// Add the widgets
JPanel jpMiddle = new JPanel(); // New container
SpringLayout slMiddle = new SpringLayout();
jpMiddle.setLayout(slMiddle);
jpMiddle.setOpaque(true);

// Middle row
jpMiddle.add( leftScrollPane );
jpMiddle.add( rightScrollPane );

// Lay out the buttons in one row and as many columns
// as necessary, with X pixels of padding all around.
SpringUtilities.makeCompactGrid(
jpMiddle, // Container
1, 2, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad

// Add the spring layout to the grid bag
addGB(c, jpMiddle, 0, 1);
}//setupMiddleRow

private void setupBottomRow(Container c)
{
jButtonNext.setBounds(0, 0, 90, 26);
jButtonNext.setBackground(new java.awt.Color(255, 128, 0));
jButtonNext.setFont(new Font("Dialog", Font.BOLD, 16));
jButtonNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});


// Add the widgets
JPanel jpBottom = new JPanel(); // New container
SpringLayout slBottom = new SpringLayout();
jpBottom.setLayout(slBottom);

// Bottom row
jpBottom.add(jButtonNext);

//Lay out the buttons in one row and as many columns
//as necessary, with 6 pixels of padding all around.
SpringUtilities.makeCompactGrid(
jpBottom, // Container
1, 1, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad

// Add the spring layout to the grid bag
addGB(c, jpBottom, 0, 2);
}


// Button selection <<<>>><<<>>> <<<>>> <<<>>><<<>>>

private void buttonActionPerformed(java.awt.event.ActionEvent evt)
{
// Get the current state of the buttons
if(evt.getActionCommand().equals("Next Q"))
jbNextActionPerformed();
}

void jbNextActionPerformed()
{
askQuestion();
}



// Main <<<>>><<<>>> <<<>>> <<<>>><<<>>>

public static void main(String args[])
{
new MainFrame();
}
18 years ago
Manuel,

Thanks for trying to help me. As a newcomer to java, I hadn't heard of Java Web Start. The idea of installing an application on a remote computer and maintaining control over it is intriguing, but for now, I'll stick to trying to get an applet running on the internet.

You were rightly confused about the structure of my applications. I referred to MainFrame.class as Main Frame and I draw in JPanels not canvas.

I think most of the 7 MB of my application is taken up by support methods and text files. (Explanations are quite wordy.) At this point, images files account for approximately 200 KB.

Is there anything I can do to make the size of my application/applet smaller: < 7 MB? Does putting the questions in files rather than in classes affect the size of the program?

Thank you for your help,

Ed
18 years ago
I�m thinking of putting a math website up. I created an application, which has a 7,712 KB executable jar file. My project looks like this:

Main Frame
* A top canvas into which I draw and put images.
* A left scroll pane into which I draw.
* A right scroll pane into which I draw and put images.

Current Project Structure
Along with a large set of support classes, I have about 20 classes with 100 question per class. Each question uses the canvas and two scroll panes.

Advice Needed
Will my current project structure �fly� on the internet with 7,712 KB? Should I create a file for each of the 20 classes, and put 100 question into each file? I assume I can write the methods as a file of objects.

All help greatly appreciated,

Ed
18 years ago
I�m a novice and I need help.

To make the notation simpler (I hope),
Let D� mean Directory
Let DD� mean subdirectory

I was as able to run my application from the DOS command prompt when it had the following structure:

D� myJ2
DinnerMenu.class (has main(String args[] and uses SpringUtilities)
SpringUtilities.class
Calculator.class ( a dialog which uses SpringUtilities)
SquareRootDialog.class (a dialog which uses SpringUtilities)
DD�pac (a subdirectory with many directories)

I decided to put Calculator.class and SquareRootDialog.class in a directory called dlg, and I put SpringUtilities.class into a directory called utl. The new structure of the application is:

D� myJ2
DinnerMenu.class
DD� utl
SpringUtilities.class
DD� dlg
Calculator.class
SquareRootDialog.class
DD� pac
Classes used by Calculator and SquareRootDialog

I import SpringUtilities into DinnerMenu, Calculator, and SquareRootDialog with:
import utl.SpringUtilities;

import SpringUtilities;

( Problem 1)
I need to recompile Calulator.class and SquareRootDialog.class before compiling DinnerMenu.class, but I can�t figure out how to do that. (I�ve been trying for a week.)

I�ve tried the following without success to compile Calculator:

C:>cd\myJ2\dlg
C:\myJ2\dlg>path= C:\Program Files\Java\jdk1.5.0_03\bin
C:\myJ2\dlg>set CLASSPATH=C:\myJ2\utl;C:\myJ2\pac;C:\myJ2\dlg
C:\myJ2\dlg> javac -classpath %CLASSPATH% Calculator.java

SAMPLE ERROR MESSAGE ---------------------------------------
Calculator.java:17: package pac.libr.mat does not exist
import pac.libr.mat.MathLibrary;
^
Calculator.java:18: package pac.libr.sta does not exist
import pac.libr.sta.StatLibrary;
^
Calculator.java:19: package utl does not exist
import utl.SpringUtilities;

------------------------------------------- END ERROR MESSAGE

(Problem 2)
I want to create an executable JAR file. Will this do the job?
CREATE THE JAR FILE
C:\myJ2\dlg>cd\myJ2
C:\>myJ2jar cvf DinnerMenu.jar DinnerMenu*.class dlg

(Problem 3)
Ultimately, I want to run the application. Will this do the job?
RUN THE APPLICATION
C:\myJ2>set CLASSPATH=C:\myJ2\myJar.jar
C:\myJ2>java -classpath "./DinnerMenu.jar" DinnerMenu

(Problem 4)
One last question. I think the jdk1.5 has SpringUtilities. Can I eliminate SpringUtilities.class from my application and import it into DinnerMenu and Calculator with something like: import SpringUtilities;

Thanks in advance for you help,

Ed
18 years ago
I�m a novice and I need help.

To make the notation simpler (I hope),
Let D� mean Directory
Let DD� mean subdirectory

I was as able to run my application from the DOS command prompt when it had the following structure:

D� myJ2
DinnerMenu.class (has main(String args[] and uses SpringUtilities)
SpringUtilities.class
Calculator.class ( a dialog which uses SpringUtilities)
SquareRootDialog.class (a dialog which uses SpringUtilities)
DD�pac (a subdirectory with many directories)

I decided to put Calculator.class and SquareRootDialog.class in a directory called dlg, and I put SpringUtilities.class into a directory called utl. The new structure of the application is:

D� myJ2
DinnerMenu.class
DD� utl
SpringUtilities.class
DD� dlg
Calculator.class
SquareRootDialog.class
DD� pac
Classes used by Calculator and SquareRootDialog

I import SpringUtilities into DinnerMenu, Calculator, and SquareRootDialog with:
import utl.SpringUtilities;

import SpringUtilities;

( Problem 1)
I need to recompile Calulator.class and SquareRootDialog.class before compiling DinnerMenu.class, but I can�t figure out how to do that. (I�ve been trying for a week.)

I�ve tried the following without success to compile Calculator:

C:>cd\myJ2\dlg
C:\myJ2\dlg>path= C:\Program Files\Java\jdk1.5.0_03\bin
C:\myJ2\dlg>set CLASSPATH=C:\myJ2\utl;C:\myJ2\pac;C:\myJ2\dlg
C:\myJ2\dlg> javac -classpath %CLASSPATH% Calculator.java

SAMPLE ERROR MESSAGE ---------------------------------------
Calculator.java:17: package pac.libr.mat does not exist
import pac.libr.mat.MathLibrary;
^
Calculator.java:18: package pac.libr.sta does not exist
import pac.libr.sta.StatLibrary;
^
Calculator.java:19: package utl does not exist
import utl.SpringUtilities;

------------------------------------------- END ERROR MESSAGE

(Problem 2)
I want to create an executable JAR file. Will this do the job?
CREATE THE JAR FILE
C:\myJ2\dlg>cd\myJ2
C:\>myJ2jar cvf DinnerMenu.jar DinnerMenu*.class dlg

(Problem 3)
Ultimately, I want to run the application. Will this do the job?
RUN THE APPLICATION
C:\myJ2>set CLASSPATH=C:\myJ2\myJar.jar
C:\myJ2>java -classpath "./DinnerMenu.jar" DinnerMenu

(Problem 4)
One last question. I think the jdk1.5 has SpringUtilities. Can I eliminate SpringUtilities.class from my application and import it into DinnerMenu and Calculator with something like: import SpringUtilities;

Thanks in advance for you help,

Ed
18 years ago
I�m a novice and I need help.

To make the notation simpler (I hope),
Let D� mean Directory
Let DD� mean subdirectory

I was as able to run my application from the DOS command prompt when it had the following structure:

D� myJ2
DinnerMenu.class (has main(String args[] and uses SpringUtilities)
SpringUtilities.class
Calculator.class ( a dialog which uses SpringUtilities)
SquareRootDialog.class (a dialog which uses SpringUtilities)
DD�pac (a subdirectory with many directories)

I decided to put Calculator.class and SquareRootDialog.class in a directory called dlg, and I put SpringUtilities.class into a directory called utl. The new structure of the application is:

D� myJ2
DinnerMenu.class
DD� utl
SpringUtilities.class
DD� dlg
Calculator.class
SquareRootDialog.class
DD� pac
Classes used by Calculator and SquareRootDialog

I import SpringUtilities into DinnerMenu, Calculator, and SquareRootDialog with:
import utl.SpringUtilities;

import SpringUtilities;

( Problem 1)
I need to recompile Calulator.class and SquareRootDialog.class before compiling DinnerMenu.class, but I can�t figure out how to do that. (I�ve been trying for a week.)

I�ve tried the following without success to compile Calculator:

C:>cd\myJ2\dlg
C:\myJ2\dlg>path= C:\Program Files\Java\jdk1.5.0_03\bin
C:\myJ2\dlg>set CLASSPATH=C:\myJ2\utl;C:\myJ2\pac;C:\myJ2\dlg
C:\myJ2\dlg> javac -classpath %CLASSPATH% Calculator.java

SAMPLE ERROR MESSAGE ---------------------------------------
Calculator.java:17: package pac.libr.mat does not exist
import pac.libr.mat.MathLibrary;
^
Calculator.java:18: package pac.libr.sta does not exist
import pac.libr.sta.StatLibrary;
^
Calculator.java:19: package utl does not exist
import utl.SpringUtilities;

------------------------------------------- END ERROR MESSAGE

(Problem 2)
I want to create an executable JAR file. Will this do the job?
CREATE THE JAR FILE
C:\myJ2\dlg>cd\myJ2
C:\>myJ2jar cvf DinnerMenu.jar DinnerMenu*.class dlg

(Problem 3)
Ultimately, I want to run the application. Will this do the job?
RUN THE APPLICATION
C:\myJ2>set CLASSPATH=C:\myJ2\myJar.jar
C:\myJ2>java -classpath "./DinnerMenu.jar" DinnerMenu

(Problem 4)
One last question. I think the jdk1.5 has SpringUtilities. Can I eliminate SpringUtilities.class from my application and import it into DinnerMenu and Calculator with something like: import SpringUtilities;

Thanks in advance for you help,

Ed
18 years ago
I down loaded eclipse-SDK-3.0.2-win32, and purchased the Eclipse Cookbook, 2004 ISBN: 0-596-00710-8. Example files are at
http://examples.oreilly.com/eclipseckbk/

Chapter 7 �Eclipse and Ant� develops a project called AntProject, which I am unable to get to work. Since the figures in the chapter don�t match the dialogs in Eclipse, I suspect the chapter needs to be updated to match the new Eclipse SDK.

I imported the example from Ch 07, which I downloaded from the Eclipse website, and got the following error message when I ran it:

Buildfile: C:\Documents and Settings\Administrator\Local Settings\Temp\eclipse-SDK-3.0.2-win32\eclipse\workspace\AntProject\build.xml
Initialization:
[delete] Deleting directory C:\Documents and Settings\Administrator\Local Settings\Temp\eclipse-SDK-3.0.2-win32\eclipse\workspace\AntProject\bin
[mkdir] Created dir: C:\Documents and Settings\Administrator\Local Settings\Temp\eclipse-SDK-3.0.2-win32\eclipse\workspace\AntProject\bin
[mkdir] Created dir: C:\Documents and Settings\Administrator\Local Settings\Temp\eclipse-SDK-3.0.2-win32\eclipse\workspace\AntProject\bin\lib
Compilation:
[javac] Compiling 1 source file to C:\Documents and Settings\Administrator\Local Settings\Temp\eclipse-SDK-3.0.2-win32\eclipse\workspace\AntProject\bin
BUILD FAILED: C:\Documents and Settings\Administrator\Local Settings\Temp\eclipse-SDK-3.0.2-win32\eclipse\workspace\AntProject\build.xml:20: Compiler Adapter 'org.eclipse.jdt.core.JDTCompilerAdapter' can't be found.
Total time: 1 second

How should I change the build.xml file below to get the program to work?

<?xml version="1.0" encoding="UTF-8"?>
<project name="AntProject" default="Build" basedir=".">

<property name="build.compiler"
value="org.eclipse.jdt.core.JDTCompilerAdapter" />
<property name="srcDir" location="src" />
<property name="binDir" location="bin" />
<property name="jarDir" location="${binDir}/lib" />
<property name="jarFile" location="${jarDir}/AntProject.jar" />

<target name="Initialization">
<delete dir="${binDir}" />
<delete dir="${jarDir}" />
<mkdir dir="${binDir}" />
<mkdir dir="${jarDir}" />
</target>

<target name="Compilation" depends="Initialization">
<javac srcdir="${srcDir}"
destdir="${binDir}" />
</target>

<target name="Jar" depends="Initialization, Compilation">
<jar destfile="${jarFile}" basedir="${binDir}" />
</target>

<target name="Build" depends="Initialization, Compilation, Jar">
<echo message="Ant is building your project." />
</target>

</project>

Thank you in advance,

Ed
18 years ago