Results 1 to 5 of 5
Thread: Simple JFrame Problem
- 06-05-2008, 01:52 AM #1
Member
- Join Date
- May 2008
- Posts
- 4
- Rep Power
- 0
Simple JFrame Problem
Hi,
I'm having issues adding a JScrollPane to a JFrame via another method. The pane isn't visible unless you first minise the frame/look at something else - validate() doesn't work. The only way I can solve it is to close the initial frame, then recreate it right before I add the pane - which obviously looks awful to the user. Can someone pls help me?
Java Code:private void initialise(){ frame = new JFrame("Relational Algebra and SQL Visualisation Tool"); //frame.setTitle(); frame.getContentPane().setLayout(null); frame.setSize(screenSize); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //add stuff frame.setVisible(true); } private class EventListener implements ActionListener{ public void actionPerformed(ActionEvent event) { // Actions taken when button is clicked JButton sent = (JButton) event.getSource(); String button = sent.getText(); if (button.equals("Generate RA Tree")) { // Parse query string and generate tree Tree tree = new Tree(); JScrollPane j = new JScrollPane(); j.setLayout(null); j = tree.genTree(text.getText()); frame.dispose(); initialise(); frame.getContentPane().add(j); //etc. } }
-
You may not want to add things directly to a JFrame. I often find it better to add them to a JPanel that is added to the JFrame's contentPane. Then after changing the components on the JPanel, call revalidate() on it (sometimes also requiring a repaint()), and you should see the changes.
Also, I strongly suggest you avoid using the "null" layout and learn to use the Swing/AWT layout managers. Once you've used them and have become familiar with them, they will make your GUI coding much easier.Last edited by Fubarable; 06-05-2008 at 03:32 AM.
- 06-06-2008, 12:57 AM #3
Member
- Join Date
- May 2008
- Posts
- 4
- Rep Power
- 0
Hey, thanks. I used revalidate() and such and it works fine.
Only thing is, before I add the scrollpane, I add textfields to it. Now I can see the scrollpane, but none of the text fields inside it! I assume it's another revalidate()-type problem but I have no idea where to try anything! Anyone care to help?
(Apologies, I'm vv new to Swing!)
(that's all the code that should be needed to understand my problem - obv. I'm adding a text field containing text to the scrollpane, which is then eventually routed back to the method where it is added to the frame).Java Code:private void initialise(){ frame = new JFrame("Relational Algebra and SQL Visualisation Tool"); //etc. frame.setVisible(true); } private class EventListener implements ActionListener{ public void actionPerformed(ActionEvent event) { // Actions taken when button is clicked JButton sent = (JButton) event.getSource(); String button = sent.getText(); if (button.equals("Generate RA Tree")) { // Parse query string and generate tree Tree tree = new Tree(); JScrollPane j = new JScrollPane(); j.setLayout(null); j = tree.genTree(text.getText()); //frame.dispose(); //initialise(); frame.getContentPane().add(j); j.invalidate(); j.validate(); j.repaint(); // can now see the scrollpane, but non of the text fields on it } } public JScrollPane genTree(String query){ // Parses query and creates RA tree JScrollPane scroll = new JScrollPane(); scroll.setLayout(null); scroll = drawTree(); return scroll; } public JScrollPane drawTree(){ int pos = 20; JScrollPane scroll = new JScrollPane(); scroll.setBounds(584, 71, 600, 600); scroll.setLayout(null); for (int i = ((left.length)-1); i>(-1); i--){ if (!(left[i].equals(""))){ JTextField x = new JTextField(left[i]); x.setEditable(false); if ((right[0] == null) && (centre[0] == null)){ x.setBounds(180, pos, 140, 30); } System.out.println(x.getText()); x.setHorizontalAlignment(JTextField.CENTER); scroll.add(x); pos += 50; } } return scroll; }
-
Please don't cross-post questions in multiple forums (here, the forum.java.sun.com, and lord-knows where). It can frustrate many who volunteer time here to help someone only to find that the question had already been answered elsewhere. No one likes wasting time. It's also a breech of the forum policies which you agreed to on joining this forum.
Last edited by Fubarable; 06-06-2008 at 07:07 AM.
- 06-06-2008, 07:09 AM #5
Similar Threads
-
My Simple Refresh Problem
By pmcastillo in forum New To JavaReplies: 0Last Post: 03-26-2008, 07:59 AM -
JFrame problem
By vassil_zorev in forum AWT / SwingReplies: 1Last Post: 01-25-2008, 02:53 AM -
Picture in a JFrame problem
By saytri in forum New To JavaReplies: 3Last Post: 01-12-2008, 09:44 AM -
JFrame problem
By saytri in forum New To JavaReplies: 6Last Post: 01-11-2008, 05:12 PM -
New to JSF, simple problem with Eclispe and Tomcat
By nibeck in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-16-2007, 05:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks