Problem with Dynamic GUI in NB

Hi all!

i'm a noob in GUI programming, BTW i created a stand alone class (read app) that can draw a Line Graph.

Let's name this class Drawer.

Drawer extends JPanel, anyway when in my NB project i try to add the Drawer to a JPanel doing

// i added the following code in the initComponents()

Drawer d =new Drawer();

jPanel2.add(d);// jPanel2 is created by Drag & Drop in the IDE

it doesn't happen anything, nothing is displayed, neither if i call the methods:

pack();

repaint();

updateUI();

suggestions?

thanks everyone!

Gio-Kun

[715 byte] By [Gio-Kuna] at [2007-11-27 6:10:15]
# 1

Since you are using NetBeanz, your container probably has some

g*d-awful layout manager. Perhaps d's size is 0x0.

This might help:Drawer d = new Drawer();

d.setPreferredSize(new Dimension(400,300));

jPanel2.add(d);

Hippolytea at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...
# 2
i do the setPreferredSize(new Dimension(500,500));in the constructor of the Drawer class.BTW i use the Free design layout, because if i use any other layout nothing remain as it is!thanks for your reply!
Gio-Kuna at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...
# 3
> BTW i use the Free design layout, because if i use any other layout nothing remain as it is!? That sounds like a good thing -- isn't it the job of the layout managerto resize and position components intelligently? Come back GridBagLayout. All is forgiven.
Hippolytea at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...
# 4
> BTW i use the Free design layout, because if i use> any other layout nothing remain as it is!You wouldn't be an ex-VB programmer by any chance? :)See http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
cotton.ma at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...
# 5
never seen VB.C/C++Javaand Web programming languages.sorry... :-D
Gio-Kuna at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...
# 6

> suggestions?

Swing related questions should be posted in the Swing forum.

You use the revalidate() method on the jPanel2, when you add a component to it and the GUI is already visible. Sometimes you may also need a repaint(), but you never use updateUI().

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...
# 7

Here i post the full example code:

this is the main class of the project:

Frame.java

package drawer;

import java.awt.Point;

public class Frame extends javax.swing.JFrame {

/** Creates new form Frame */

public Frame() {

drawer = new Drawer();

init();

}

/*this is the init component modified by me to add the drawer object to the panel*/

private void init(){

panel = new javax.swing.JPanel();

addButton = new javax.swing.JButton();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

yField = new javax.swing.JTextField();

xField = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

panel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

panel.setPreferredSize(new java.awt.Dimension(300, 200));

org.jdesktop.layout.GroupLayout panelLayout = new org.jdesktop.layout.GroupLayout(panel);

panel.setLayout(panelLayout);

panelLayout.setHorizontalGroup(

panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(0, 298, Short.MAX_VALUE)

);

panelLayout.setVerticalGroup(

panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(0, 198, Short.MAX_VALUE)

);

addButton.setText("Add");

addButton.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

addButtonMouseClicked(evt);

}

});

jLabel1.setText("X");

jLabel2.setText("Y");

yField.setText("int y");

yField.setPreferredSize(new java.awt.Dimension(75, 22));

xField.setText("int x");

xField.setPreferredSize(new java.awt.Dimension(75, 22));

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(panel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.add(layout.createSequentialGroup()

.add(35, 35, 35)

.add(jLabel1)

.add(96, 96, 96)

.add(jLabel2)))

.add(20, 20, 20))

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.add(xField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 30, Short.MAX_VALUE)

.add(yField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(addButton)

.add(95, 95, 95))))

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(panel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 24, Short.MAX_VALUE)

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)

.add(jLabel1)

.add(jLabel2))

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)

.add(addButton)

.add(yField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.add(xField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))

.add(12, 12, 12))

);

/*Here i add the drawer object to the panel*/

panel.add(drawer);

/*i revalidate and repaint the panel*/

panel.revalidate();

panel.repaint();

/*validation of the frame*/

this.validate();

this.pack();

}

/*This method handles the insertion of new points on the Graph*/

private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {

Point p;

int x = 0,y = 0;

x = Integer.parseInt(xField.getText());

y = Integer.parseInt(yField.getText());

p = new Point(x,y);

System.out.println(p.toString());

drawer.addPoint(p);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

Frame frame;

frame = new Frame();

frame.setResizable(false);

frame.setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton addButton;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JPanel panel;

private javax.swing.JTextField xField;

private javax.swing.JTextField yField;

// End of variables declaration

private Drawer drawer;

}

here there's the other class of the project:

the task of this class is painting the graph.

you can try separately by uncommenting the main.

Drawer.java

package drawer;

import java.util.*;

import java.awt.*;

import javax.swing.*;

import java.lang.Thread;

public class Drawer extends JPanel{

LinkedList<Point> pointList;

Graphics g;

public Drawer(){

pointList = new LinkedList<Point>();

setPreferredSize(new Dimension(290,190));

setBackground(Color.WHITE);

/*to be used if this class is used as a stand alone application*/

//pointList.add(new Point(0,0));

//pointList.add(new Point(30,50));

//pointList.add(new Point(50,150));

}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.setColor(Color.BLACK);

Point start, end;

//System.out.println(pointList.size());

start = (Point)pointList.get(0);

for(int i=1;i<pointList.size();i++){

end = (Point)pointList.get(i);

//System.out.println("Start: "+start.toString()+"\tEnd: "+end.toString());

g.drawLine(start.x,290-start.y,end.x,190-end.y);

start = end;

}

}

/*this method is called only if you use this class as stand alone application*/

public void addItems(){

pointList.add(new Point(70,255));

pointList.add(new Point(70,352));

pointList.add(new Point(80,255));

pointList.add(new Point(110,255));

repaint();

}

public void addPoint(Point p){

pointList.add(p);

System.out.println("Successfully added point:"+p.toString()+"to the pointList");

repaint();

}

/*

public static void main(String[] args){

JFrame frame = new JFrame("Line drawer");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Drawer d= new Drawer();

frame.getContentPane().add(d);

frame.setResizable(false);

frame.pack();

frame.setVisible(true);

Thread t = new Thread();

try{

t.sleep(3000);

}

catch(Exception e){}

d.addItems();

}

*/

}

hope this clarify the problem.

thanks in advance.

sorry camickr, i clicked on the wrong forum but i hope this will be the last question on this problem.

thanks everyone!>

Gio-Kuna at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...
# 8

> [url=http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE)

> which demonstrates the incorrect behaviour, because I [still] can't guess

> what you are doing based on the information provided.

I doubt anyone is going to bother demangling that nasty GUI generated code.

If you're still stuck, and you really need help then hand code a minimal program which reproduces the problem.

I'll bet 2 bucks that you figure the problem out yourself in the process...

corlettka at 2007-7-12 17:15:17 > top of Java-index,Java Essentials,Java Programming...