Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    JDesktopPane JInternalFrame

    salve a tutti
    io ho un problema con java
    non riesco a far visualizzare dentro un JDesktopPane una classe che deriva JInternalFrame
    ecco il codice della classe che ha JDesktopPane

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.*;
    import java.lang.reflect.*;
    import java.util.*;

    public class Menu
    {
    public static void main(String[] args)
    {
    JFrame frame = new MenuFrame();

    frame.show();
    }
    }

    class MenuFrame extends JFrame implements TreeSelectionListener
    {
    public MenuFrame()
    {
    setTitle("Menu");
    setSize(400,300);
    addWindowListener
    (
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }

    }
    );


    //costruisce l'albero
    TreeNode root = makeTree();
    model = new DefaultTreeModel(root);
    tree = new JTree(model);
    //tree.setEditable(true);
    //tree.setRootVisible(false);

    //imposta modalità di selezione
    tree.addTreeSelectionListener(this);
    int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
    tree.getSelectionModel().setSelectionMode(mode);

    JDesktopPane command = new JDesktopPane();

    JSplitPane splitPane;
    JPanel albero = new JPanel();
    albero.setLayout(new GridLayout(1, 2));
    albero.add(new JScrollPane(tree));

    splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, albero, command );
    splitPane.setOneTouchExpandable( true );
    splitPane.setDividerLocation( 150 );
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);


    //aggiunge riquadro di scorrimento e vi inserisce l'albero
    Container contentPane = getContentPane();

    contentPane.add(splitPane, "Center");

    }
    public void valueChanged(TreeSelectionEvent event) {
    try
    {
    String face = tree.getLastSelectedPathComponent().toString();
    Class cls = Class.forName(face);
    command.add(new JScrollPane((JComponent)cls.newInstance()));

    }catch(Exception e)

    {e.printStackTrace();}

    }

    public TreeNode makeTree()
    {
    ..........................
    }


    private DefaultTreeModel model;
    private JTree tree;
    private JDesktopPane command;




    }



    ecco il codice della classe che viene chiamata:



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

    public class Copy extends JInternalFrame {
    public static void main(String[] args) {
    new Copy();
    }

    private JList sampleJList;
    private JTextField valueField;

    private JButton button;

    public Copy() {
    //super("Copy");

    JInternalFrame copy = new JInternalFrame("Copy ",true,true,true,true);
    copy.setSize(200,200);
    copy.setVisible(true);


    WindowUtilities.setNativeLookAndFeel();
    //addInternalFrameListener(new ExitListener());
    Container content = getContentPane();

    // Create the JList, set the number of visible rows, add a
    // listener, and put it in a JScrollPane.
    String[] entries = { "Entry 1", "Entry 2", "Entry 3",
    "Entry 4", "Entry 5", "Entry 6" };
    sampleJList = new JList(entries);
    sampleJList.setVisibleRowCount(4);
    Font displayFont = new Font("century gothic", Font.BOLD, 12);
    sampleJList.setFont(displayFont);
    sampleJList.addListSelectionListener(new ValueReporter());
    JScrollPane listPane = new JScrollPane(sampleJList);

    JPanel listPanel = new JPanel();
    listPanel.setBackground(Color.white);
    Border listPanelBorder =
    BorderFactory.createTitledBorder("Copy file");
    listPanel.setBorder(listPanelBorder);
    listPanel.add(listPane);
    content.add(listPanel, BorderLayout.CENTER);

    JLabel valueLabel = new JLabel("New name:");
    valueLabel.setFont(displayFont);
    valueField = new JTextField("None", 20);
    valueField.setFont(displayFont);



    button = new JButton("Copy");

    JPanel valuePanel = new JPanel(new GridLayout(4, 1, 2, 2));
    valuePanel.setBackground(Color.white);
    //Border valuePanelBorder =
    // BorderFactory.createTitledBorder("Edit Selection");
    //valuePanel.setBorder(valuePanelBorder);
    valuePanel.add(valueLabel);
    valuePanel.add(valueField);



    valuePanel.add(button);

    content.add(valuePanel, BorderLayout.SOUTH);
    pack();
    setVisible(true);

    }

    private class ValueReporter implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent event) {
    if (!event.getValueIsAdjusting())
    valueField.setText(sampleJList.getSelectedValue(). toString());
    }
    }
    }

    qualcuno sarebbe così gentile da dirmi dove sbaglio


    grazie mille

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: JDesktopPane JInternalFrame

    Originariamente inviato da ironcuzzo
    qualcuno sarebbe così gentile da dirmi dove sbaglio
    Innanzitutto il codice dovresti postarlo ben scritto e sopratutto ben indentato, perché così si capisce abbastanza poco.

    Comunque la prima (ma proprio la primissima) cosa su cui mi è "caduto" l'occhio è questa: hai dichiarato una variabile di istanza:

    private JDesktopPane command;

    ma nel costruttore hai scritto:

    JDesktopPane command = new JDesktopPane();

    cioè hai definito una variabile locale.

    E questo già non va bene ... perché quella di istanza rimane a null e dove la usi (es. in valueChanged) puoi immaginare che succede ....
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    quindi devo inizializzarla fuori dal costruttore:

    private JDesktopPane command = new JDesktopPane();

    ?

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da ironcuzzo
    quindi devo inizializzarla fuori dal costruttore:

    private JDesktopPane command = new JDesktopPane();

    ?
    Non è che "devi".... o la inizializzi come hai appena detto cioè con la dichiarazione della variabile oppure la inizializzi nel costruttore ma ovviamente non dichiarando una nuova variabile!!
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    perfetto....ma anche correggiendo ciò il problema non si risolve

  6. #6
    se vuoi ti spedisco il codice così capisci meglio

  7. #7
    aiutami andrea!...non lasciarmi in balia di java!

  8. #8
    ho risolto era JScrollPane che dava problemi....

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.