gui reload? possible?

Hi there.

i have a litle problem.

im trying to do GUI but there is a question i can't solve: i need to do a dynamic GUI so as if the user presses one button some combobox appear in my GUI frame, if another some checkBoxes.

How can i do this? maybe there is some way i can reload initial frame?

Thanks for answers

[344 byte] By [isolateda] at [2007-11-26 21:13:42]
# 1
panel.add( someComponent );panel.revalidate();
camickra at 2007-7-10 2:51:44 > top of Java-index,Desktop,Core GUI APIs...
# 2
don't know why but doesn't work for me ;[
isolateda at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 3

it should work

try this

hide the frame and show it the frame should be updated

if it still doesn t ot work try a little bit stronger approach

remove the contain and then add it again and call revalidate

make sure you revalidate all the component which need to be

lildavesflavaa at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 4

jep... managed to do the revalidate to work...

but now i have another problem...

public void drawBox(int kiek) {

for(int i=0;i<kiek;i++)

{

NR1.add(jCheckBox1, new XYConstraints(222 , 67 + (postX * i-200*timesY), -1, -1));

}

NR1.revalidate();

after initializing drawBox with kiek=5 it draws me a window with five check boxes. but there is an option on the side to choose this amount from a combobox. and if i choose a number less then the previuos the extra checkbox doesn't disapear...

what could u suggest?>

isolateda at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 5

Call invalidate() method before change something and validate() after that.

For example:

panel.invalidate();

panel.add();

panel.validate();

but you still can have a problem with frame: your components can demand more space when frame's size and you won't see its (depends on yout layout manager). In this case call .pack() method of your frame. This method changes size of frame to possible minimum size.

sklimenkoa at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 6

maybe someone can test:

public void drawBox(int kiek) {

NR1.invalidate();

//NR1.validate();//

int postX=20;

int postY=60;

JCheckBox jCheckBox1=null;

int timesY=0;

buttons.clear();

for(int i=0;i<kiek;i++)

{

timesY=i/10;

jCheckBox1 = new JCheckBox();

buttons.add(jCheckBox1);

jCheckBox1.setText(" " + (i+1)+" ");

NR1.add(jCheckBox1, new XYConstraints(222 +timesY*postY, 67 + (postX * i-200*timesY), -1, -1));

}

NR1.validate();

this is the whole procedure. but it doesn't work....

i get an eventaction and after it i call draw.... but it fails...

Message was edited by:

isolated>

isolateda at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 7
Please write type of NR1 and buttons variables. Is buttons list or Container?What layout has NR1 component?
sklimenkoa at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 8

You have problem with your layout manager not with validate method. XYConstaints - not standard layout.

Please try this example:

package test;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class MainFrame extends JFrame {

private JPanel NR1 = new JPanel();

public MainFrame() throws HeadlessException {

setLayout(new BorderLayout());

JButton b = new JButton("Add more checkboxes");

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

drawBox(5);

}

});

add(b, BorderLayout.NORTH);

add(NR1, BorderLayout.CENTER);

}

public void drawBox(int kiek) {

NR1.invalidate();

//int postX = 20;

//int postY = 60;

JCheckBox jCheckBox1 = null;

//int timesY = 0;

//buttons.clear();

for (int i = 0; i < kiek; i++) {

//timesY = i / 10;

jCheckBox1 = new JCheckBox();

//buttons.add(jCheckBox1);

jCheckBox1.setText(" " + (i + 1) + " ");

NR1.add(jCheckBox1);

//NR1.add(jCheckBox1, new XYConstraints(222 + timesY * postY, 67 + (postX * i - 200 * timesY), -1, -1));

}

NR1.validate();

}

public static void main(String[] args) {

MainFrame frame = new MainFrame();

frame.setSize(400, 300);

frame.setVisible(true);

}

}

It addes five checkboxes to panel when you press button.

sklimenkoa at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 9
jTabbedPane1.add(NR1, "Registration");List buttons= new Vector(); about layout it is xylayout if i understood your question.
isolateda at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 10

Try don't use XYLayout:

1. It's better use relative layout

2. XYLayout - not default (it doesn't exist in JDK1.5). I don't known what realization of this layout you are using (Eclips and so on).

Try to change your code according to my example:

package test;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class MainFrame extends JFrame {

private JPanel NR1 = new JPanel();

public MainFrame() throws HeadlessException {

setLayout(new BorderLayout());

JButton b = new JButton("Add more checkboxes");

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

drawBox(5);

}

});

add(b, BorderLayout.NORTH);

add(NR1, BorderLayout.CENTER);

NR1.setLayout(null);

}

public void drawBox(int kiek) {

NR1.invalidate();

int postX = 20;

int postY = 60;

JCheckBox jCheckBox1 = null;

int timesY = 0;

//buttons.clear();

for (int i = 0; i < kiek; i++) {

timesY = i / 10;

jCheckBox1 = new JCheckBox();

//buttons.add(jCheckBox1);

jCheckBox1.setText(" " + (i + 1) + " ");

Dimension d = jCheckBox1.getPreferredSize();

jCheckBox1.setBounds(222 + timesY * postY, 67 + (postX * i - 200 * timesY),

d.width, d.height);

NR1.add(jCheckBox1);

}

NR1.validate();

NR1.repaint();

}

public static void main(String[] args) {

MainFrame frame = new MainFrame();

frame.setSize(400, 300);

frame.setVisible(true);

}

}

sklimenkoa at 2007-7-10 2:51:45 > top of Java-index,Desktop,Core GUI APIs...
# 11

> Call invalidate() method before change something and validate() after that.

You don't need to call invalidate() before adding the component.

You use revalidate() after adding the component.

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-10 2:51:46 > top of Java-index,Desktop,Core GUI APIs...
# 12

public void jButton2_actionPerformed(ActionEvent e) {

jPanel1.invalidate();

int post=20;

for(int i=0;i<7;i++){

jCheckBox1 = new JCheckBox();

jCheckBox1.setText("jCheckBox" + i);

jPanel1.add(jCheckBox1,

new XYConstraints(205 , 41+ post * i, -1, -1));

}

jPanel1.validate();

jPanel1.repaint();

here http://saulius.sa.funpic.de/java/java.JPG u can find a part of what i'm trying to do..

lets say there are two buttons: the diference is

for(int i=0;i<7;i++)

and for(int i=0;i<5;i++)

after i paint the gui with 5 i can paint with seven... but if i try to paint 5 again i still see 7 checkboxes....

so what could be the reason?

isolateda at 2007-7-10 2:51:46 > top of Java-index,Desktop,Core GUI APIs...
# 13
i used sklimenko example... added another buttom with draw(7)if i press button to draw 5 checkboxes everything is ok...if i press button to draw 7 is works... but if i try to draw 5 again it fails...any other advices what could be the problem?
isolateda at 2007-7-10 2:51:46 > top of Java-index,Desktop,Core GUI APIs...
# 14

If you call jButton2_actionPerformed method more that once you will have problems because you don't remove previouse checkboxes.

So when you press button you create 5 checkboxes. When you bress button again you create another 5 checkoxes (now on the JPanel you have 10 checkboxes and last 5 hide previouse 5)

To change it you should remove previouse checkboxes before inserting new as shown in example below:

public void jButton2_actionPerformed(ActionEvent e) {

jPanel1.invalidate();

jPanel1.removeAll(); // removes previouse checkboxes

int post=20;

.........

}

sklimenkoa at 2007-7-10 2:51:46 > top of Java-index,Desktop,Core GUI APIs...
# 15
thanks for answer...but i don't figure out how to do this...removeall removes everything in my tab so in this case it removes all other components on the tab..so i do remove(int) but i don't like this way....maybe U could suggest how to do this in a proper
isolateda at 2007-7-21 18:13:42 > top of Java-index,Desktop,Core GUI APIs...
# 16

You have special list: buttons. So you can use it to get components for removing:

Iterator iter = buttons.iterator();

while (ite.hasNext()) {

panel.remove((Component)iter.next());

}

buttons.clear();

sklimenkoa at 2007-7-21 18:13:42 > top of Java-index,Desktop,Core GUI APIs...
# 17

hi again...

i have gui with a combobox and a button.

when i press the button some new gui addElement.show apears.

in the new gui i can add new record.

after doing all this i want to reload the parent gui(with combobox) but with no luck.

so how can i access and revalidate/repaint parent gui?

maybe there are any other ways how i can do such things?

example of what i have

http://saulius.sa.funpic.de/java/java1.JPG

Trying to do thing like in the link above...

I can't find out how after presing a button "registruoti" i can reload "klientas" GUI.

any suggestions?

isolateda at 2007-7-21 18:13:42 > top of Java-index,Desktop,Core GUI APIs...