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

    Passaggio da Choice a JComboBox

    Salve, in una mia applicazione ho un menu a tendina implementato con Choice...
    Dato che volevo passare a Swing, pensavo di sostituire l' oggetto Choice con una JComboBox...
    Ho semplicemente sostituito queste parti:
    codice:
     private String[] algStrings = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg"};
    .
    .
    // private Choice algChoice;
     private JComboBox alg;
    .
    .
    /*algChoice = new Choice();
    * for(int i = 0; i < algStrings.length; i++)
    * algChoice.add(algStrings[i]);
    */
    alg= new JComboBox();
          for(int i = 0; i < algStrings.length; i++)
             alg.addItem (algStrings[i]);
    .
    .
    //if (algChoice.getSelectedIndex() == 0)
    if (alg.getSelectedIndex() == 0)
    .
    .
    //algChoice.setEnabled(true);
          alg.setEnabled(true);
    Il problema è che quando lancio l' applicazione non scendono tutte le opzioni, ovvero non si apre la tendina...
    Delle 7 opzioni si vede solo la prima e se clicco sopra l' oggetto non calano le altre opzioni...
    Qualche suggerimento?
    Grazie.

  2. #2
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    72
    Strano dovrebbe funzionare , prova o a postare il codice e versione del compilatore e prova questo piccolo codice (sicuramente funzionante) per vedere come si comporta con te.

    codice:
    import javax.swing.*;
    public class TestChoice2 {
    	public static void main(String[] args) {
    		JFrame frame=new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel=new JPanel();
    		String[] algStrings = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg"};
    		JComboBox alg=new JComboBox();
    		for(int t=0;t<algStrings.length;t++){
    			alg.addItem(algStrings[t]);
    		}
    		panel.add(alg);
    		frame.getContentPane().add(panel);
    		frame.setSize(500,500);
    		frame.show();
    	}
    }

  3. #3
    Il tuo codice ovviamente funziona...
    Ti scrivo parte del codice... Vediamo un po' se riesci a capirci qualcosa almeno tu...

    codice:
    import java.awt.*;
    import java.awt.event.*;
    import java.net.MalformedURLException;
    import java.util.Vector;
    import javax.swing.*;
    
    public class App extends JFrame implements ActionListener, KeyListener, ItemListener, Runnable
    {
    //dichiarazione variabili non interessate al problema...
    .
    .
    //fine dichiarazione variabili non interessate al problema...
    
    private Starter s;
    private Pi pi;
    private JTextField orField, maxField, heField, moField, paField, exField, stField, tiField;
    private Panel puPanel;
    private List dirList;
    private JButton sol, cont, re, sh;
    private String[] algStrings = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg"};
    private JComboBox alg;
    private Checkbox cbE, cbF;
    private Panel pi;
    
    public App(Starter _s)
    {  
    super("Titolo");
    s = _s;
    setBackground(SystemColor.activeCaptionBorder);
    .
    .
    .
    CheckboxGroup cbGroup = new CheckboxGroup();
    cbE = new Checkbox("e", cbGroup, true);
    cbE.addItemListener(this);
    cbF = new Checkbox("f", cbGroup, false);
    cbF.addItemListener(this);
    JPanel cbPanel = new JPanel();
          cbPanel.setLayout(new FlowLayout());
          cbPanel.add(new JLabel("Scegli:", JLabel.LEFT));
          cbPanel.add(cbE);
          cbPanel.add(cbF);
    
    Panel algPanel= new JPanel();
          algPanel.setLayout(new FlowLayout());
          algPanel.add(new JLabel("Scegli:", JLabel.RIGHT));
          alg= new JComboBox();
          for(int i = 0; i < algStrings.length; i++)
             alg.addItem (algStrings[i]);
          algPanel.add(alg);
    .
    .
    orField = new JTextField(10);
          orField .addKeyListener(this);
          orField .setBackground(Color.white);
          JPanel orPanel = new JPanel();
          orPanel.setLayout(new FlowLayout());
          orPanel.add(new JLabel("Scegli:", JLabel.RIGHT));
          orPanel.add(orField);
    
          sol = new JButton("sol");
          sol .addActionListener(this);
          re= new JButton("re");
          re.addActionListener(this);
          sh= new JButton("sh");
          sh.addActionListener(this);
          sh.setEnabled(false);
          cont = new JButton("cont");
          cont.addActionListener(this);
          cont.setEnabled(false);
    
          maxField = new JTextField(10);
          maxField .setBackground(Color.white);
          JPanel maxPanel = new JPanel();
          maxPanel .setLayout(new FlowLayout());
          maxPanel .add(new JLabel("Scegli:", JLabel.RIGHT));
          maxPanel .add(maxField);
          maxPanel .add(sol);
          maxPanel .add(re);
    
          Panel userPanel = new Panel();
          userPanel.setLayout(new GridLayout(4, 1));
          userPanel.add(cbPanel);
          userPanel.add(algPanel);
          userPanel.add(orPanel);
          userPanel.add(maxPanel);
    
    JLabel title = new JLabel("Titolo", JLabel.CENTER);
          title.setFont(new Font("sansserif", Font.BOLD, 20));
          JPanel titlePanel = new JPanel();
          titlePanel.setLayout(new FlowLayout());
          titlePanel.add(title);
    
          Panel topPanel = new Panel();
          topPanel.setLayout(new BorderLayout());
          topPanel.add("North", titlePanel);
          topPanel.add("South", new Box(userPanel, "Input", Box.LEFT));
          pa = new Pa(s, SIZE);
    
          Panel contPanel = new Panel();
          contPanel.setLayout(new FlowLayout());
          contPanel.add(cont);
          Panel buttonPanel = new Panel();
          buttonPanel.setLayout(new FlowLayout());
          buttonPanel.add(sh);
          buttonPanel.add(contPanel);
    Panel contPanel = new Panel();
          contPanel .setLayout(new FlowLayout());
          contPanel .add(cont);
          Panel buttonPanel = new Panel();
          buttonPanel.setLayout(new FlowLayout());
          buttonPanel.add(sh);
          buttonPanel.add(contPanel);
    
          puPanel = new Panel();
          puPanel.setLayout(new FlowLayout());
          puPanel.add(new Label());
          puPanel.add(pi);
          puPanel.add(new Label());
          
          Panel puPanel = new Panel();
          puPanel.setLayout(new BorderLayout());
          puPanel.add("North", puPanel);
          puPanel.add("South", buttonPanel);
    
          Panel puButtonPanel = new Panel();
          puButtonPanel.setLayout(new BorderLayout());
          puButtonPanel.add("North", puPanel);
    
          //da qui ci sono 6 JTextField, evito di riportarli
    .
    .
    
          Panel infoPanel = new Panel();
          infoPanel.setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          
          gbc.insets = new Insets(5, 0, 1, 10);
          gbc.anchor = GridBagConstraints.EAST;
          gbc.gridwidth = 1;
          gbc.weightx = 0;
          gbc.fill = GridBagConstraints.NONE;
          infoPanel.add(new Label("St:", Label.RIGHT), gbc);
          gbc.anchor = GridBagConstraints.CENTER;
          gbc.gridwidth = GridBagConstraints.REMAINDER;
          gbc.weightx = 100;
          gbc.fill = GridBagConstraints.HORIZONTAL;
          infoPanel.add(stField, gbc);
        //così per tutti e 6 i JTextField
    .
    .  
          gbc.anchor = GridBagConstraints.CENTER;
          gbc.gridwidth = GridBagConstraints.REMAINDER;
          gbc.gridheight = GridBagConstraints.REMAINDER;
          gbc.weightx = 100;
          gbc.weighty = 100;
          gbc.fill = GridBagConstraints.BOTH;
          infoPanel.add(dirList, gbc);
    
          JPanel centerPanel = new JPanel();
          centerPanel.setLayout(new BorderLayout());
          centerPanel.add("West", new Box(puButtonPanel, "Pu", Box.LEFT));
          centerPanel.add("Center", new Box(infoPanel, "Sol", Box.LEFT));
    
          setLayout(new BorderLayout());
          add("North", topPanel);
          add("Center", centerPanel);
    
          validate();
          pack();
          setResizable(false);
          setVisible(true);
       }
    
    public void run()
       {
    .
    .
    }
    private void prepareToRun()
       {
          .
          .   
          cbE.setEnabled(false);
          cbF.setEnabled(false);
          alg.setEnabled(false);
          orField.setEnabled(false);
          maxField.setEnabled(false);
          sol.transferFocus();
          sol.setEnabled(false);
       }
    
    public void start()
       {
        .
         .
       }
    
    public void actionPerformed(ActionEvent ae)
       {
          .
          .
          .
             cbE.setEnabled(true);
             cbF.setEnabled(true);
             alg.setEnabled(true);
             .
             .
       }
    
    public static void main(String[] args)
       {
          
          App pa = new App(null);
         
       }
    
    }
    In pratica il frame è composto da:
    1 pannello per il titolo;
    4 pannelli per gli input dell' utente;
    1 pannello per la visualizzazione dell' elaborazione;
    1 pannello informativo.

    Se può essere utile alla comprensione dell' errore, quando ho provato a sostituire le Checkbox con le JRadioButton, appare lo stesso problema... E' come se questi componenti fossere insensibili.

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    72
    Da quello che ho potuto constatare se prepareToRun viene chiamo subito , tu disabiliti la combo
    codice:
    private void prepareToRun()
       {
          cbE.setEnabled(false);
          cbF.setEnabled(false);
          alg.setEnabled(false);
          orField.setEnabled(false);
          maxField.setEnabled(false);
          sol.transferFocus();
          sol.setEnabled(false);
       }
    Ora però dal codice postato non si capisce se viene chiamata subito ma dal nome del metodo immagino di si.
    Poi però associ un evento che abilita

    codice:
    public void actionPerformed(ActionEvent ae)
       {
             cbE.setEnabled(true);
             cbF.setEnabled(true);
             alg.setEnabled(true);
       }
    Ma dal codice non capisco a cosa è associato , cioè chi scatena quell'evento?.

    Scatena quell'evento (un pulsante ?? ) e dovrebbero comparire gli altri item.

    Ciao.

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    72
    Per debuggare aggiungi un System.out

    codice:
    public void actionPerformed(ActionEvent ae)
       {
             cbE.setEnabled(true);
             cbF.setEnabled(true);
             alg.setEnabled(true);
         System.out.println("Attivo");
       }
    Così vedi se viene mai chiamato.

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    72
    Purtroppo (o per fortuna ) spengo il PC ora e lo riaccenderò Lunedì mattina , spero cmq di esserti stati d'aiuto , fammi sapere come è andata.

    Ciao.

  7. #7
    Dopo qualche modifica, appare la tendina...Peccato che vada sotto ad altri pannelli... VVoVe: VVoVe:
    Effettivamente nel codice c' è ancora qualche pannello Panel e non JPanel...Può essere quello il motivo? Ho provato a sostituire i Panel con i JPanel... Ma nell' applicazione questi pannelli e i loro componenti (JtextField, JButton) non compaiono più...Se passo il mouse sopra le zone in questione allora compaiono...
    Pensavo fosse più facile la conversione da Awt a Swing...
    Qualche aiuto?

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.