Text area doesn't seem to be working - java

I am try to create a text area that will display a list of scores. For some reason though, the text area only expands when someone types in it, and the user should not even be allowed to type in it. I thought i wrote in the code for it properly but for some reason it doesn't seem to work. The "hello" phrase I appended isn't even displaying in the text area. Can anyone provide some advice:
public HighScores() throws FileNotFoundException, IOException{
frame.setVisible(true);
frame.setSize(400,200);
frame.add(main);
GridBagConstraints g = new GridBagConstraints();
g.insets = new Insets(10,10,10,10);
g.gridx = 0;
g.gridy = 0;
main.add(highscorespanel, g);
highscorespanel.add(highscores);
g.gridx = 0;
g.gridy = 1;
main.add(textareapanel, g);
Color c = textareapanel.getBackground();
textareapanel.setBackground(c);
textareapanel.add(ta);
ta = new JTextArea ();
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
ta.append("hello");
JScrollPane sp = new JScrollPane(ta);
BufferedReader br = new BufferedReader(new FileReader("src/BattleShip/scores.txt"));
String namescore = br.readLine();
while(namescore!=null){
ta.append("\t"+namescore);
}

Im almost sure you don't need the answer anymore but you need to move your line "textareapanel.add(ta);" to some point after you initialize ta.
ta = new JTextArea();
textareapanel.add(ta);
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
EDIT:
At a second look you want a JScrollPane for your JTextArea so you code should be like this:
JTextArea ta = new JTextArea();
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
JScrollPane sp = new JScrollPane(ta);
textareapanel.add(sp);

Related

JTextField no size?

Currently my JTextField component is sizing to what appears to be 0. I have attempted to setPreferredSize, setSize(new Dimension), added columns & removed the anchor with GridBagConstraints. Nothing has worked thus far. Here is my code:
private void createPayFrame() {
JFrame payFrame = new JFrame();
payFrame.setSize(new Dimension(450, 300));
payFrame.setLocationRelativeTo(null);
payFrame.setLayout(new GridBagLayout());
JLabel payment = new JLabel("<html>Your bill has been generated. It is located in your "
+ System.getProperty("user.home") + "\\eclipse-workspace\\Turbo Team directory</html>");
GridBagConstraints gbc = new GridBagConstraints();
JTextField payField = new JTextField(10);
payField.setToolTipText("Enter in the amount due here");
gbc.anchor = GridBagConstraints.ABOVE_BASELINE;
gbc.insets = new Insets(0,0,20,0);
payFrame.add(payment, gbc);
gbc.gridy = 1;
gbc.insets = new Insets(10,0,0,0);
payFrame.add(payField, gbc);
payFrame.setVisible(true);
}
This is what it looks like when I run the code:
Any idea what is going on here?
Set the fill for the textfield
gbc.fill = GridBagConstraints.HORIZONTAL;
I've had this problem with GridBagLayout when the JTextField was too large to display. Have you tried with less columns? Maybe try to start at new JTextField(1) and work your way up to see if it works at smaller lengths.

how to add a "console" like window to a GUI?

I have created a GUI. It has several buttons. i would like to add a console like object underneath them in which i would be able to write messages so the user would see them.
what element/object/class should i use? i want it to be able to present messages in different lines.
here is my code for creating the GUI:
public ssGUI() {
setLayout(new BorderLayout());
bRunNoAdj = new JButton("Run no adjustment");
bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunNoAdj.setMnemonic(KeyEvent.VK_E);
bRunNoAdj.addActionListener(this);
bRunNoAdj.setEnabled(false);
bRunNoAdj.setBackground(Color.white);
bRunAdj = new JButton("Run adjustment");
bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunAdj.setMnemonic(KeyEvent.VK_E);
bRunAdj.addActionListener(this);
bRunAdj.setEnabled(false);
bRunAdj.setBackground(Color.white);
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
bConnect.setBackground(Color.white);
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
bDisconnect.setBackground(Color.white);
bStationary = new JButton("Show Stationary");
bStationary.setMnemonic(KeyEvent.VK_E);
bStationary.addActionListener(this);
bStationary.setEnabled(false);
bStationary.setBackground(Color.white);
bMoving = new JButton("Show Moving");
bMoving.setMnemonic(KeyEvent.VK_E);
bMoving.addActionListener(this);
bMoving.setEnabled(false);
bMoving.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(bConnect);
topPanel.add(bDisconnect);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.add(bRunNoAdj);
centerPanel.add(bRunAdj);
add(centerPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(bStationary);
bottomPanel.add(bMoving);
add(bottomPanel, BorderLayout.SOUTH);
}
any help would be appreciated, thank you.
The easiest would probably be to use a JTextArea.
You would ofcourse prevent the user from editing the area, this can be done like this:
JTextArea area = new JTextArea();
area.setEditable(false);
And if you want the user to be able to scroll the area you can add it to a JScrollPane like this:
JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);
And lastly you can add a line to the area by doing:
area.setText(area.getText() + "\n" + newLine);
Or preferably:
area.append("\n" + newLine);
I hope this helps :)
Use a JTextArea. Call text_area.setEditable(false); on it to make it read-only.
I think what you mean is JTextArea or JTextField

Swing components not moving to desired cell in GridBagLayout

I'm trying to create a simple UI for a program that can read a file, write to a file and search for text within a file. I have most of my Components created, the problem is that they're all being "drawn" in the same (center) cell. I've tried applying weights, widths, etc. all to no avail.
Here's my base code for the UI:
public void GUI(){
//Create main window for Program
JFrame mainWindow = new JFrame("Simple Data Base"); //Init frame
mainWindow.setSize(500, 400); //Set frame size
mainWindow.setVisible(true); //Make frame visible
//Create panel for the main window of the GUI
JPanel simpleGUI = new JPanel( new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
simpleGUI.setBackground(Color.cyan);
//Create button linking to read function
JButton readButton = new JButton("Read"); //Init button, and give text
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
//Create button linking to the search function
JButton searchButton = new JButton("Search");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 1;
//Create label prompting user to specify desired function
JLabel promptText = new JLabel("Click 'Read' to read a file, 'Search' to search within a file, 'Write' to write to a file:");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 0;
//Add components to Main window
mainWindow.getContentPane().add(simpleGUI);
simpleGUI.add(promptText, gbCons);
simpleGUI.add(readButton, gbCons);
simpleGUI.add(searchButton, gbCons);
}
the problem is that they're all being "drawn" in the same (center) cell.
simpleGUI.add(promptText, gbCons);
simpleGUI.add(readButton, gbCons);
simpleGUI.add(searchButton, gbCons);
You are using the same GridBagConstraints for each component so the contraints are identical for each component.
You neeed to:
set the constraints
add the component to the panel using the constraints
repeat steps 1 and 2.
For example:
JButton readButton = new JButton("Read");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 0;
gbCons.gridy = 1;
simpleGUI.add(readButton, gbCons);
JButton searchButton = new JButton("Search");
gbCons.fill = GridBagConstraints.BOTH;
gbCons.gridx = 1;
gbCons.gridy = 1;
simpleGUI.add(searchButton, gbCons);
I suggest you read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples.
Download the demo code and use that example as your starting code. The demo code will show you how to better structure your class by:
NOT extending JFrame
Creating the GUI on the Event Dispatch Thread
Use the pack() method, NOT the setSize(...) method
Make the frame visible AFTER all components have been added to the frame

Grid Layout does not display correctly

I'm trying to build simple calculator gui with display and 9 buttons
public void init()
{
setSize(60,80);
inf = new InfoButton(this);
zero = new CalcButton(this,"0");
one = new CalcButton(this,"1");
add = new CalcButton(this,"+");
sub = new CalcButton(this,"-");
div = new CalcButton(this,"/");
mlt = new CalcButton(this,"*");
modu = new CalcButton(this,"%");
blank = new JButton("");
wys = new Wyswietlacz(); // its JTextPane
wys.setSize(60,20);
przyciski = new JPanel();
przyciski.setSize(60,60);
przyciski.setLayout(new GridLayout(3,3));
przyciski.add(zero);
przyciski.add(one);
przyciski.add(add);
przyciski.add(sub);
przyciski.add(mlt);
przyciski.add(div);
przyciski.add(modu);
przyciski.add(inf);
przyciski.add(blank);
calosc = new JPanel();
calosc.setLayout(new BoxLayout(calosc,BoxLayout.Y_AXIS));
calosc.add(wys);
calosc.add(przyciski);
calosc.setSize(60,80);
add(calosc);
}
and in main i make frame with size (60,80) but when i make it visible all i can see is display and one row of buttons. What am i doing wrong?
Call setPreferredSize(..) instead of setSize() on wys and przyciski. Then use JFrame's pack() instead of specifying a size for it.

JScrollPane Scrolling Horizontally when it Should be Scrolling Vertically

I have a panel with FlowLayout specified that is full of labels, text fields, and text areas. The frame isn't that wide, but the panel becomes tall because of all the components inside. I want to add the panel to a JScrollPane, so I can scroll vertically through the panel. However, when I add the panel to the scroll pane and add the scroll pane to the frame, all the components are right next to each other and it scrolls horizontally through them. Here's the code:
public class Form {
JTextField jtfName = new JTextField(15);
JTextField jtfTitle = new JTextField(15);
JTextField jtfAuthor = new JTextField(15);
JTextArea jtaSetting = new JTextArea(5, 15);
JTextArea jtaMainChars = new JTextArea(5, 15);
JTextArea jtaConflict = new JTextArea(5, 15);
JTextArea jtaQuote = new JTextArea(5, 15);
JTextArea jtaMainCharShows = new JTextArea(5, 15);
JPanel jpnlName = new JPanel();
JPanel jpnlTitle = new JPanel();
JPanel jpnlAuthor = new JPanel();
Form() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Organizer");
jfrm.setLayout(new GridLayout(0,1));
// Give the frame an initial size.
jfrm.setSize(300, 300);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a panel.
JPanel jpnl = new JPanel();
// Create labels.
JLabel jlabName = new JLabel("Student Name:");
JLabel jlabTitle = new JLabel("Title:");
JLabel jlabAuthor = new JLabel("Author:");
JLabel jlabSetting = new JLabel("Setting (Time and Place):");
jlabSetting.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainChars = new JLabel("Main Characters:");
jlabMainChars.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabConflict = new JLabel("<html>Describe the major conflict of the<br>story in one well-written sentence:");
jlabConflict.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabQuote = new JLabel("<html>Find and write down a passage (quote<br>from the book that reveals a significant<br>personality trait of one of the main characters<br>and GIVE THE PAGE #:");
jlabQuote.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainCharShows = new JLabel("<html>Explain in your own words what the<br>passage (quote) shows about the main character.");
jlabMainCharShows.setHorizontalAlignment(SwingUtilities.CENTER);
// Add text fields to panel.
jpnlName.add(jlabName);
jpnlName.add(jtfName);
jpnlTitle.add(jlabTitle);
jpnlTitle.add(jtfTitle);
jpnlAuthor.add(jlabAuthor);
jpnlAuthor.add(jtfAuthor);
// Add components to main panel.
jpnl.add(jpnlName);
jpnl.add(jpnlTitle);
jpnl.add(jpnlAuthor);
jpnl.add(jlabSetting);
jpnl.add(jtaSetting);
jpnl.add(jlabMainChars);
jpnl.add(jtaMainChars);
jpnl.add(jlabConflict);
jpnl.add(jtaConflict);
jpnl.add(jlabQuote);
jpnl.add(jtaQuote);
jpnl.add(jlabMainCharShows);
jpnl.add(jtaMainCharShows);
// Add the panel to a scroll pane.
JScrollPane jspPanel = new JScrollPane(jpnl);
// Add the scroll pane to the frame.
jfrm.getContentPane().add(jspPanel);
// Display the frame.
jfrm.setVisible(true);
}
}
Without know exactly the layout you want, the best solution I can suggest is to try the WrapLayout
It addresses the major problem with the FlowLayout, it doesn't wrap.
Why don't you try BoxLayout:
jpnl.setLayout(new BoxLayout(jpnl, BoxLayout.PAGE_AXIS));

Categories

Resources