Results 1 to 20 of 23
Thread: JTextField Disappearing
- 02-12-2012, 12:08 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
JTextField Disappearing
I have been having and issue with a JTextField where it is removed from my JPanel after I remove another panel from my JFrame and re-add the panel containing the JTextField. I checked the components of my Jpanel using:
Java Code:Component[] components = matchingWordPanel.getComponents(); for(int y = 0; y < components.length; y++){ System.out.println("Panel: " + components[y]); }
In addition, I tried adding a JPanel in the place of my JTextField and it was not removed.
What would cause my JTextField to go missing?Last edited by DinnerFork; 02-12-2012 at 12:11 AM.
- 02-12-2012, 12:27 AM #2
Re: JTextField Disappearing
What would cause my JTextField to go missing?
What way have you chosen?
Hard to tell without seeing the code.
- 02-12-2012, 12:43 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Re: JTextField Disappearing
What way have you chosen?
If that is the case, it should never be removed from the panel. It just seems to be removed right as I swap panels when the "exitImage" button is clicked.
Here is a somewhat condensed version of the function:
Java Code:public void editVocab(JPanel mainPanel, JTextField vocabName, JTextField matchingWord, JEditorPane vocabDefinition, JEditorPane vocabSynonyms, ActionListener ACL, ImageIcon vocabIcon){ mainPanel.removeAll(); //Container for everything that goes inside mainpanel. editPanel = new JPanel(){ protected void paintComponent(Graphics g) { Dimension d = getSize(); g.drawImage(new ImageIcon("C:\\Program Files\\LangPrac\\BlueMoon.jpg").getImage(), 0, 0, d.width, d.height, null); super.paintComponent(g); } }; JLabel vocabNameLabel = new JLabel("Vocabulary Name"); JLabel matchingWordLabel = new JLabel("Matching Word"); JPanel vocabNamePanel = new JPanel();//Contains Vocabulary Name JLabel & JTextField final JPanel matchingWordPanel = new JPanel();//Contains Matching Word JLabel & JTextField //Test panel to see if it gets removed //JPanel matchingWordContainer = new JPanel(); //matchingWordContainer.setOpaque(false); //matchingWordContainer.add(matchingWord); //matchingWordContainer.setPreferredSize(new Dimension(210, 20)); ActionListener getImageListener = new ActionListener(){ public void actionPerformed(ActionEvent event){ //Testing to see if the component is removed. Component[] components = matchingWordPanel.getComponents(); for(int y = 0; y < components.length; y++){ System.out.println("Panel: " + components[y]); } String source = event.getActionCommand(); if(source.equals("getImage")){ tempPanel.setVisible(false); tempPanel.removeAll(); getVocabImage.setOriginalImage(getImageIcon); tempPanel.add(getVocabImage.getPanel()); tempPanel.invalidate(); tempPanel.validate(); getVocabImage.getPanel().setVisible(true); tempPanel.setVisible(true); } else if(source.equals("exitImage")){ tempPanel.setVisible(false); tempPanel.removeAll(); tempPanel.add(tempEditPanel); changeImage = getVocabImage.changeImage(); if(changeImage){ getImageIcon = getVocabImage.getNewImage(); setupVocabIcon(); getVocabImage.setOriginalImage(getImageIcon); } tempPanel.invalidate(); tempPanel.repaint(); tempPanel.validate(); tempPanel.setVisible(true); } } }; vocabName.setOpaque(false); matchingWord.setOpaque(false); vocabName.setPreferredSize(new Dimension(210, 20)); matchingWord.setPreferredSize(new Dimension(210, 20)); vocabName.setBackground(new Color(1).RED); matchingWord.setBackground(new Color(1).WHITE); vocabName.setForeground(new Color(1).WHITE); matchingWord.setForeground(new Color(1).WHITE); vocabNameLabel.setForeground(new Color(1).WHITE); matchingWordLabel.setForeground(new Color(1).WHITE); vocabNamePanel.add(vocabNameLabel); vocabNamePanel.add(vocabName); matchingWordPanel.add(matchingWord); matchingWordPanel.add(matchingWordLabel); vocabNamePanel.setLayout(new FlowLayout()); matchingWordPanel.setLayout(new FlowLayout()); editPanel.add(vocabNamePanel); editPanel.add(matchingWordPanel); vocabNamePanel.setOpaque(false); matchingWordPanel.setOpaque(false); editPanel.setLayout(new FlowLayout(1, 0, 15)); //editPanel.setLayout(new GridLayout(0,1)); mainPanel.add(editPanel); mainPanel.setVisible(false); mainPanel.invalidate(); mainPanel.repaint(); mainPanel.validate(); mainPanel.setVisible(true); }
- 02-12-2012, 12:47 AM #4
Re: JTextField Disappearing
You need to post a small, complete program that compiles, executes and shows the problem(SSCCE).
- 02-12-2012, 06:30 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 13
Re: JTextField Disappearing
Whenever I see the removeAll() method I recommend you check out: How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
- 02-12-2012, 08:26 AM #6
Re: JTextField Disappearing
Moved from 'New to Java'
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-12-2012, 07:28 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Re: JTextField Disappearing
Here is a smaller working version of the original. Every time I try to use the add new vocab function twice, my matchingWord and vocabName JTextFields still disappear. I noticed that this doesn't occur if I run a new instance of GVI though.
I will also try using the cardlayout to see if it prevents the issue.
- 02-12-2012, 07:42 PM #8
Re: JTextField Disappearing
this doesn't occur if I run a new instance of GVI
- 02-12-2012, 08:11 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Re: JTextField Disappearing
Have you solved your problem now?
- 02-12-2012, 08:13 PM #10
Re: JTextField Disappearing
Please post a simple single program that compiles, executes and shows your problem.
When I compile and execute your code I get a blank screen. I'd rather work with one file, not three.
- 02-12-2012, 09:11 PM #11
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Re: JTextField Disappearing
Here is a single program version. I didn't get a blank screen issue before, but I tried some changes to prevent it.
- 02-12-2012, 09:16 PM #12
Re: JTextField Disappearing
Here's the code so we can discuss it.
Java Code:import java.awt.ComponentOrientation; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class TestRun implements ActionListener{ private JTextField matchingWord; private JTextField vocabName; private JPanel splitPane; private JPanel addPanel; private JPanel mainPanel; private JPanel getVocabularyPanel; private JButton[] buttons; private File[] allFiles; private Icon[] fileIcons; private JScrollPane scroll; private JPanel[] panels; private JPanel container; private JPanel currentVocabImage; private JPanel menuContainer; private JLabel[] labels; private JButton exitButton; private boolean boolListenerSet; private final static String workDir = System.getProperty("user.dir"); private JButton acceptButton; private JPanel editPanel; private boolean firstRun; private JFrame mainFrame; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public TestRun(){ mainFrame = new JFrame(); vocabName = new JTextField(""); matchingWord = new JTextField(""); mainPanel = new JPanel(); mainPanel.setOpaque(false); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public JPanel setupMainMenu(){ JButton addVocabButton = new JButton("Add new Vocab Word"); firstRun = true; addPanel = new JPanel(); splitPane = new JPanel(); splitPane.add(addPanel); splitPane.setOpaque(false); addVocabButton.setActionCommand("addVocab"); addVocabButton.addActionListener(this); splitPane.setOpaque(false); addPanel.add(addVocabButton); mainPanel.add(addPanel); mainPanel.setLayout(new GridLayout(0,1)); mainPanel.setVisible(true); mainFrame.add(mainPanel); mainFrame.setPreferredSize(new Dimension(700,700)); mainFrame.pack(); mainFrame.setDefaultCloseOperation(new JFrame().EXIT_ON_CLOSE); mainFrame.setVisible(true); return mainPanel; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void newVocab(){ setupEditVocabularyMenu(mainPanel, vocabName, matchingWord, this); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void mainMenu(){ mainPanel.removeAll(); mainPanel.setVisible(false); mainPanel.add(addPanel); mainPanel.invalidate(); mainPanel.repaint(); mainPanel.validate(); mainPanel.setVisible(true); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void actionPerformed(ActionEvent event) { String source = event.getActionCommand(); if(source.equals("acceptVocab")){ mainMenu(); } else if(source.equals("addVocab")){ vocabName.setText(""); matchingWord.setText(""); newVocab(); } } public static void main(String args[]){ TestRun main = new TestRun(); main.setupMainMenu(); } public void setupGetVocabularyPanel(){ getVocabularyPanel = new JPanel(); currentVocabImage = new JPanel(); menuContainer = new JPanel(); currentVocabImage = new JPanel(); currentVocabImage.setPreferredSize(new Dimension(400,100)); getVocabularyPanel.setPreferredSize(new Dimension(500, 400)); getVocabularyPanel.setLayout(new GridLayout()); getVocabularyPanel.setOpaque(false); getFileNames(); displayFiles(); getVocabularyPanel.setVisible(true); } public void getFileNames(){ File[] listOfFiles = new File(workDir).listFiles(); allFiles = listOfFiles; try{ fileIcons = new Icon[listOfFiles.length]; }catch(NullPointerException NPE){System.out.println("No Image Files in directory. (GetVocabImage)");} } public void displayFiles(){ if(fileIcons == null){ fileIcons = new Icon[0]; } getVocabularyPanel.setLayout(new GridLayout()); panels = new JPanel[fileIcons.length];//Container for buttons and labels container = new JPanel();//Container for panels buttons = new JButton[fileIcons.length];//Displays the images labels = new JLabel[fileIcons.length];//Displays the image names boolListenerSet = false;//Is the action listenr for exitButton set? exitButton = new JButton("Exit");//Previous Menu exitButton.setActionCommand("exitImage"); exitButton.setPreferredSize(new Dimension(130, 35)); JPanel exitPanel = new JPanel(); exitPanel.add(exitButton); exitPanel.setPreferredSize(new Dimension(150, 40)); exitPanel.setLayout(new FlowLayout()); exitPanel.setOpaque(false); String extension = " ";//Extension of file container = new JPanel(); menuContainer.setOpaque(false); menuContainer.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); menuContainer.setPreferredSize(new Dimension(300,600)); menuContainer.setOpaque(false); container.setLayout(new GridLayout(4,0,30,5)); container.setOpaque(false); container.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); container.setPreferredSize(new Dimension((int)37.5 * panels.length, 600)); scroll = new JScrollPane();//(Holds buttons and labels container); scroll.setPreferredSize(new Dimension(300,600)); scroll.getViewport().setPreferredSize(new Dimension(100,100)); scroll.getViewport().setOpaque(false); scroll.getViewport().add(container); scroll.setOpaque(false); getVocabularyPanel.add(scroll); menuContainer.add(exitPanel); getVocabularyPanel.add(menuContainer); getVocabularyPanel.setLayout(new GridLayout()); getVocabularyPanel.setOpaque(false); //Add the panels to the file menu for(int x = 0; x < fileIcons.length ; x++){ extension = getExt(x); if(extension.equalsIgnoreCase("JPEG Image") || extension.equalsIgnoreCase("Bitmap Image") || extension.equalsIgnoreCase("IrfanView JPG File") || extension.equalsIgnoreCase("JPG File")) { panels[x] = new JPanel(); panels[x].setPreferredSize(new Dimension(150,160)); panels[x].setOpaque(false); container.add(panels[x]); } } //Add buttons and labels to panels for(int x = 0; x < fileIcons.length ; x++){ labels[x] = new JLabel(); extension = getExt(x); buttons[x] = new JButton(); if(extension.equalsIgnoreCase("JPEG Image") || extension.equalsIgnoreCase("Bitmap Image") || extension.equalsIgnoreCase("IrfanView JPG File") || extension.equalsIgnoreCase("JPG File")) { buttons[x] = new JButton(""); buttons[x].setText("activated"); buttons[x].setPreferredSize(new Dimension(150,120)); buttons[x].setActionCommand(Integer.toString(x)); labels[x].setPreferredSize(new Dimension(150,40)); panels[x].add(buttons[x]); panels[x].add(labels[x]); panels[x].setLayout(new FlowLayout()); panels[x].repaint(); container.repaint(); } buttons[x].setActionCommand(Integer.toString(x)); labels[x].setText(allFiles[x].getName()); } } //Get file extension public String getExt(int index){ javax.swing.JFileChooser fc = new javax.swing.JFileChooser(); String filetype = fc.getTypeDescription(new java.io.File(allFiles[index].getPath())); return filetype; } public void setExitButtonListener(ActionListener getACL){ if(!boolListenerSet){ exitButton.addActionListener(getACL); boolListenerSet = true; } } public JPanel getVocabularyPanel(){ return getVocabularyPanel; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void refreshVocabularyPanel(){ getVocabularyPanel.setVisible(false); getVocabularyPanel.invalidate(); getVocabularyPanel.repaint(); getVocabularyPanel.validate(); getVocabularyPanel.setLayout(new GridLayout()); getVocabularyPanel.setVisible(true); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void setupEditVocabularyMenu(JPanel mainPanel, JTextField vocabName, JTextField matchingWord, ActionListener ACL){ mainPanel.removeAll(); editPanel = new JPanel(); JLabel vocabNameLabel = new JLabel("Vocabulary Name"); JLabel matchingWordLabel = new JLabel("Matching Word"); JPanel vocabNamePanel = new JPanel(); JPanel matchingWordPanel = new JPanel(); JPanel getImagePanel = new JPanel(); JPanel acceptCancelPanel = new JPanel(); JPanel imageContainer = new JPanel(); final JPanel tempEditPanel = editPanel; final JPanel tempPanel = mainPanel; JButton getImageButton = new JButton("Change Image"); imageContainer.setOpaque(false); imageContainer.setPreferredSize(new Dimension(450, 80)); imageContainer.setLayout(new GridLayout(1, 2)); imageContainer.add(getImagePanel); getImagePanel.add(getImageButton); getImagePanel.setOpaque(false); ActionListener getImageListener = new ActionListener(){ public void actionPerformed(ActionEvent event){ String source = event.getActionCommand(); if(source.equals("getImage")){ tempPanel.setVisible(false); tempPanel.removeAll(); tempPanel.add(getVocabularyPanel()); tempPanel.invalidate(); tempPanel.validate(); getVocabularyPanel().setVisible(true); tempPanel.setVisible(true); } else if(source.equals("exitImage")){ tempPanel.setVisible(false); tempPanel.removeAll(); tempPanel.add(tempEditPanel); tempPanel.invalidate(); tempPanel.repaint(); tempPanel.validate(); tempPanel.setVisible(true); } } }; if(firstRun){ setupGetVocabularyPanel(); firstRun = false; } setExitButtonListener(getImageListener); getImageButton.addActionListener(getImageListener); getImageButton.setActionCommand("getImage"); vocabName.setOpaque(false); matchingWord.setOpaque(false); acceptButton = new JButton("Accept"); acceptButton.setPreferredSize(new Dimension(180, 30)); acceptButton.addActionListener(ACL); acceptButton.setActionCommand("acceptVocab"); vocabName.setPreferredSize(new Dimension(210, 20)); matchingWord.setPreferredSize(new Dimension(210, 20)); acceptCancelPanel.setPreferredSize(new Dimension(450, 40)); acceptCancelPanel.add(acceptButton); vocabNamePanel.add(vocabNameLabel); vocabNamePanel.add(vocabName); matchingWordPanel.add(matchingWordLabel); matchingWordPanel.add(matchingWord); vocabNamePanel.setLayout(new FlowLayout()); matchingWordPanel.setLayout(new FlowLayout()); acceptCancelPanel.setLayout(new FlowLayout()); editPanel.add(vocabNamePanel); editPanel.add(matchingWordPanel); editPanel.add(imageContainer); editPanel.add(acceptCancelPanel); vocabNamePanel.setOpaque(false); matchingWordPanel.setOpaque(false); editPanel.setOpaque(false); editPanel.setLayout(new FlowLayout(1, 0, 15)); mainPanel.add(editPanel); mainPanel.setVisible(false); mainPanel.invalidate(); mainPanel.repaint(); mainPanel.validate(); mainPanel.setVisible(true); } }
- 02-12-2012, 09:18 PM #13
Re: JTextField Disappearing
The first thing I see is that your code is not a small simple program. 400 lines is past being small.
What is supposed to happen when this code is executed?Last edited by Norm; 02-12-2012 at 09:21 PM.
- 02-12-2012, 09:55 PM #14
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Re: JTextField Disappearing
Well, you should be able to add a new vocabulary word, enter a name and matching word, enter the change image menu, exit the menu, and accept the changes. It works fine the first time around, but after repeating the same step it leads to the JTextFields for vocabName and matchingWord being removed from the panel upon exiting the change image menu.
I've been using this to check to see if the panel was there or not:
Java Code:Component[] components = matchingWordPanel.getComponents(); for(int y = 0; y < components.length; y++){ System.out.println("Panel: " + components[y]);
- 02-12-2012, 09:58 PM #15
Re: JTextField Disappearing
Try making a small simpler program that shows your problem. This one is too big and hangs when I try to execute it.
There is too much not related to the GUI to wade through. Strip it down to a GUI only, minimum components and no files.
- 02-12-2012, 10:23 PM #16
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Re: JTextField Disappearing
I remove some of the other parts.
- 02-12-2012, 10:27 PM #17
Re: JTextField Disappearing
What is the code supposed to do?
How can I see what the problem is?
- 02-12-2012, 10:42 PM #18
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Re: JTextField Disappearing
It is supposed to let you set a matching word, vocabulary name, and image for any number of vocabulary words.
It should let you run enter the add new vocabulary menu, the change image menu, exit that menu, and accept to get back to the start. However, anytime you repeat it will remove the JTextFields when exiting the change image menu.
- 02-12-2012, 11:02 PM #19
Re: JTextField Disappearing
Have two reference variables for the panels makes for confusion!!!
- 02-12-2012, 11:20 PM #20
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Press enter on JTextField to make focus on next JTextField?
By userno69 in forum AWT / SwingReplies: 3Last Post: 11-14-2011, 06:13 AM -
Why is javaw.exe Randomly Disappearing
By Land Of Java in forum JavaFXReplies: 0Last Post: 10-05-2011, 09:43 PM -
Problem Loading JTextField in JFrame / Problema al cargar JTextField en JFrame
By thor_inc in forum AWT / SwingReplies: 0Last Post: 08-30-2011, 10:18 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 07:44 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 05:25 PM
Bookmarks