Swing program help
A while ago I was good in java, and used swing a lot, but time has passed and I forgot. Now I'm trying to implement something but have no idea how. Here's what I want to do.
I'm making a 2-dimensional linked list, and I want to show it graphically
but at first only the top level should be visible. when the user clicks one of the items, it should expand the sublist of that item and show them, when another top-item is clicked, it should close the previous sublist and open the other one. kind of like a tree structure.. I also want to have a text field and an "add" button, when text is entered and the add button is pressed, if no sublist is displayed, it adds the string to the top list. If a sublist is displayed, it adds to the sublist. Can someone give me an idea how to arrange my jbuttons and stuff so that they can interact with each other? I couldn't do it. thanks.
[899 byte] By [
nayon_7a] at [2007-11-27 6:34:02]

> A while ago I was good in java, and used swing a lot,
> but time has passed and I forgot. Now I'm trying to
> implement something but have no idea how.
If you were good at least you would have an idea. Right?
> Here's
> what I want to do.
> I'm making a 2-dimensional linked list, and I want to
> show it graphically
> but at first only the top level should be visible.
> when the user clicks one of the items, it should
> expand the sublist of that item and show them, when
> another top-item is clicked, it should close the
> previous sublist and open the other one. kind of like
> a tree structure.. I also want to have a text field
> and an "add" button, when text is entered and the add
> button is pressed, if no sublist is displayed, it
> adds the string to the top list. If a sublist is
> displayed, it adds to the sublist. Can someone give
> me an idea how to arrange my jbuttons and stuff so
> that they can interact with each other?
How far have you gone. Where are you stuck?
> I couldn't do
> it. thanks.
You could do it, you can do it, you did it before and you should do it. How sure am I you aint oulling a prank to get thy homework done for you? In case yo haven't noticed, your forum mates are here to HELP you, not do the entire homework for you...
Jamwaa at 2007-7-12 18:00:11 >

I of course wrote all the j items, buttons panels etc, and created listeners for them.
I am thinking of displaying the list via buttons, when pressed they show their sublist, but I have a few questions:
How can I acquire the data in the JTextField in the listener of the JButton?
Where should I write the list so that when I acquire the data from the field with the button, I can add it to the list and the list can be displayed?
I can display a sublist of a button when it is pressed, by adding them to the paanel the button is in, but how do I remove them from the panel? How do I do so when another button is pressed?
Basically these questions boil down to: How do I make the listeners of each item intereact? should I write one general purpose listener?
> I of course wrote all the j items, buttons panels
> etc, and created listeners for them.
> I am thinking of displaying the list via buttons,
> when pressed they show their sublist, but I have a
> few questions:
>
> How can I acquire the data in the JTextField in the
> listener of the JButton?
public void actionPerformed(ActionEvent e)
{
String text = jTextField.getText();
}
Jamwaa at 2007-7-12 18:00:11 >

Can you give me an idea why the following does not work?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame
{
public MainFrame()
{
super("Knowledge Base");
setSize(640, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new BorderLayout());
JPanel panel = new JPanel(new FlowLayout());
JButton button1 = new JButton( "Add info" );
panel.add( button1 );
button1.addActionListener( new MyButtonListener(this));
String text = "lol";
panel.add(new JButton(text));
content.add(panel, BorderLayout.SOUTH);
JPanel panel2 = new JPanel(new FlowLayout());
JTextField infoEnter = new JTextField(10);
panel2.add( infoEnter );
content.add(panel2, BorderLayout.NORTH);
content.add(new JScrollPane(new JTextArea()), BorderLayout.CENTER);/**/
}
private class MyButtonListener implements ActionListener
{
private JFrame parentComponent;
MyButtonListener(JFrame parentComponent)
{
this.parentComponent=parentComponent;
}
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(parentComponent, "Info Added!");
text = infoEnter.getText();
}
}
}
Because some variables are not visible. Try
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame
{
private JButton button1;
private JPanel panel ;
private String text = "lol";
private JPanel panel2 ;
private JTextField infoEnter ;
public MainFrame()
{
super("Knowledge Base");
setSize(640, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new BorderLayout());
panel = new JPanel(new FlowLayout());
button1 = new JButton( "Add info" );
panel.add( button1 );
button1.addActionListener( new MyButtonListener(this));
panel.add(new JButton(text));
content.add(panel, BorderLayout.SOUTH);
panel2 = new JPanel(new FlowLayout());
infoEnter = new JTextField(10);
panel2.add( infoEnter );
content.add(panel2, BorderLayout.NORTH);
content.add(new JScrollPane(new JTextArea()), BorderLayout.CENTER);/**/
}
private class MyButtonListener implements ActionListener
{
private JFrame parentComponent;
MyButtonListener(JFrame parentComponent)
{
this.parentComponent=parentComponent;
}
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(parentComponent, "Info Added!");
text = infoEnter.getText();
}
}
}
Jamwaa at 2007-7-12 18:00:11 >

