Results 1 to 6 of 6
Thread: Text are not visible in GUI
- 05-13-2009, 06:56 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Text are not visible in GUI
My text area in this program I'm working on is not visible, only the buttons are....any help appreciated.
Java Code:import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyText extends JFrame { private Toolkit toolkit; public static int LineCount = 0; //Counts the lines. public static String[] content; public static void ReadFile() throws Exception { BufferedReader TextFileReader = new BufferedReader(new FileReader("./info.txt")); String c; String t; t = "Blank"; while((c = TextFileReader.readLine()) != null) { LineCount++; t += ","+ c; } t = t.replaceAll("Blank,",""); content = new String[LineCount]; content = t.split(","); TextFileReader.close(); for(int i = 0; i < 7; i++) //Keep printing as long as i is smaller than 7, which is what I need for my text file. System.out.println(content[i]); //prints the first part of my list of names. } public MyText() { setTitle("Name and Address"); setSize(300, 400); toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation((size.width - getWidth())/2, (size.height - getHeight())/2); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null); JButton beep = new JButton("Next"); beep.setBounds(150, 60, 80, 30); beep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); for(int i = 0; i > 0; i++) System.out.println(content[i]); } }); JButton close = new JButton("Back"); close.setBounds(50, 60, 80, 30); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); for(int i = 0; i > 0; i--) System.out.println(content[i]); } }); panel.add(beep); panel.add(close); } public static void main(String[] args) { MyText buttons = new MyText(); buttons.setVisible(true); } }
- 05-13-2009, 09:02 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
You have no text that should be displayed in the GUI. Use JLabels and/or JTextAreas to display text in a GUI.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-13-2009, 09:12 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
JTextArea is much better. So in future you can do any formatting and processing easily than the button.
First of all you have to add all the components in the panel. So you may want to look at the layouts as well.
- 05-15-2009, 03:07 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
I think that JLabel may actually work better if you don't need the text to be user-editable. JTextAreas do not allow text alignment and are actually have very little in the way of formatting capabilities. If you use a JLabel you can just use html new-line tags. (Both <br> and <p> create new lines, but I believe you would use <br> to force new line, and <p> for possible new line.)
My take on it is that unless you need user-editable text, use a JLabel as it allows text alignment.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-15-2009, 08:52 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Yes Singing, it's depends on the requirement of our thread starter.
- 05-15-2009, 09:14 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Or else, you can use setEditable(false) to disable the content edit on the JTextArea. Still you can copy the content and selectable, but cannot change the content.
Similar Threads
-
Tab or Table not visible
By madhuvanthi2312 in forum SWT / JFaceReplies: 1Last Post: 04-25-2009, 10:00 AM -
only ~200x200px Applet area visible
By jumper19 in forum New To JavaReplies: 8Last Post: 02-02-2009, 04:05 AM -
Address bar not visible
By Akashchopra521 in forum New To JavaReplies: 0Last Post: 12-03-2008, 08:13 AM -
constructor ... is not visible
By emceenugget in forum New To JavaReplies: 2Last Post: 09-27-2008, 06:12 AM -
How do you keep multiple popups visible?
By Jacob Abernathy in forum New To JavaReplies: 6Last Post: 09-15-2008, 07:35 PM
Bookmarks