Results 1 to 5 of 5
Thread: Java user interface problem
- 05-22-2010, 05:51 PM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Java user interface problem
Hey everyone!
I've got trouble changing the text for the text field in my application. More
precisely, I'd like to add lines every now and then without the other lines
vanishing, similar to the DOS console.
This is GUI.java
This is how I'm trying to change the text in the field (even though that's not what I actually want to do, see above).Java Code:import java.awt.*; import javax.swing.*; public class GUI extends JFrame { public void createAndShowGUI() { //Create and set up the window. JFrame myframe = new JFrame("TopLevelDemo"); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myframe.setPreferredSize(new Dimension(800,600)); myframe.setLayout(new BorderLayout()); //Create an output text Field JTextField outputField = new JTextField(10); outputField.addActionListener(null); outputField.setPreferredSize(new Dimension(800,525)); outputField.setEditable(false); outputField.ad myframe.getContentPane().add(outputField, BorderLayout.NORTH); //Display the window. myframe.pack(); myframe.setVisible(true); } }
Any help would be very much appreciated!Java Code:public class General{ public static void main(String[] args) { final GUI iface = new GUI(); //I had to declare that final, does anyone know why btw? iface.createAndShowGUI(); iface.myframe.outputField.setText("Test"); //<- Does not work. } }
- 05-22-2010, 06:32 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
To display multiple lines of simple text in a Swing GUI, you'll want to use a JTextArea, not a JTextField. Also, look at the API and the tutorials regarding this component and in particular, check out the append method.
Que tengas suerte!
- 05-22-2010, 09:17 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
I don't understand what you want.
Do you want something like this:
If you want something else, please explain it better.Java Code:import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextArea; public class AddLines { public static void main(String[] args) { AddFrame frame = new AddFrame(); frame.showGUI(); } } class AddFrame extends JFrame { JPanel panel; JTextField textField; JTextArea textArea; JButton button; JLabel textLabel; public AddFrame() { panel = new JPanel(); textField = new JTextField(20); button = new JButton("Change text"); textLabel = new JLabel("Text is not changed"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textLabel.setText(textField.getText()); } }); panel.add(textField, BorderLayout.NORTH); panel.add(button, BorderLayout.SOUTH); panel.add(textLabel, BorderLayout.CENTER); getContentPane().add(panel); } public void showGUI() { setTitle("Add Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200,200); setLocation(200,200); pack(); setVisible(true); } }
- 05-22-2010, 09:43 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Or you maybe want something like this:
Java Code:import java.util.Vector; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextArea; public class AddLines { public static void main(String[] args) { AddFrame frame = new AddFrame(); frame.showGUI(); } } class AddFrame extends JFrame { JPanel panel; JTextField textField; JTextArea textArea; JButton button; Vector<String> allTextFields; public AddFrame() { panel = new JPanel(); panel.setLayout(new BorderLayout()); textField = new JTextField(20); button = new JButton("Add text to console"); textArea = new JTextArea(6,20); allTextFields = new Vector<String>(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { allTextFields.add(textField.getText().toString()); String text=""; for(int i = 0; i < allTextFields.size(); i++ ) text += allTextFields.get(i) + "\n"; textArea.setText(text); textField.setText(""); } }); panel.add(textField, BorderLayout.NORTH); panel.add(textArea, BorderLayout.SOUTH); panel.add(button, BorderLayout.WEST); getContentPane().add(panel); } public void showGUI() { setTitle("Add to console"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200,200); setLocation(200,200); pack(); setVisible(true); } }
- 05-23-2010, 02:52 PM #5
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
simple user interface designer
By v1nsai in forum New To JavaReplies: 3Last Post: 08-17-2009, 01:53 AM -
User Interface
By swikar.java in forum Advanced JavaReplies: 16Last Post: 12-09-2008, 02:37 PM -
J2ME User Interface Developer
By mobileapps in forum Jobs OfferedReplies: 0Last Post: 10-03-2008, 12:43 PM -
user interface development using JSP
By pradeep1_mca@yahoo.com in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 06-02-2008, 01:48 PM -
Help user interface
By carl in forum New To JavaReplies: 1Last Post: 07-31-2007, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks