Computing.Net > Forums > Programming > Java TextArea question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java TextArea question

Reply to Message Icon

Name: rayray705
Date: July 24, 2006 at 18:52:21 Pacific
OS: Win98
CPU/Ram: 2.8ghz/512mb
Comment:

Hello,
I created a JTextArea object in a scrollable pane which is in a tabbed pane. The text area scrolls fine if the user holds a key down for a while. But when the user moves to a different tabbed pane and comes back the pane containing the JTextArea object, he/she finds that everything is deformed in that pane is there remedy to this problem?

I am pasting the code below in case you want to take look at it. You have to invoke the run() method to make the frame visible. The code is experimental to please ignore the bad style.

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.*;
import javax.swing.event.*;
/**
* Write a description of class AddBookFrame here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class AddBookFrame extends JFrame
{
// instance variables - replace the example below with your own
private JFrame rb;

/**
* Constructor for objects of class AddBookFrame
*/
public AddBookFrame()
{
super("Address Book");
//setSize(475, 400);
// setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//******* start List Entry Pane ********
String labels[] = {"Entry1", "Entry2", "Entry3", "Entry4",
"Entry5", "Entry6"};

JPanel listEntryPane = new JPanel();


DefaultListModel model = new DefaultListModel();
for(String x : labels) {
model.addElement(x);
}
JList entryList = new JList(model);
entryList.setVisibleRowCount(4);

JScrollPane entryListScrollpane = new JScrollPane(entryList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

JButton viewEntryButt = new JButton("View");
JButton eraseEntryFromGroupButt = new JButton("Erase Entry");

listEntryPane.add(entryListScrollpane);
listEntryPane.add(viewEntryButt);
listEntryPane.add(eraseEntryFromGroupButt);
//******* end List Entry Pane ****************//


//******* start Group Pane ***************//
JPanel groupPane = new JPanel();

JList groupList = new JList(model);
groupList.setVisibleRowCount(4);

JScrollPane groupListScrollPane = new JScrollPane(groupList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

JButton viewListButt = new JButton("View List");
JButton editGroupNameButt = new JButton("Edit Group Name");
JButton eraseGroupButt = new JButton("Erase Group");

groupPane.add(groupListScrollPane);
groupPane.add(viewListButt);
groupPane.add(editGroupNameButt);
groupPane.add(eraseGroupButt);
//****** end Group Pane ***********//

//******** start ADD/EDIT entry pane **********//
JPanel viewAddPane = new JPanel();
viewAddPane.setLayout(new GridBagLayout());


final JTextField desigTxtField = new JTextField(20);
final JTextArea detailsTxtArea = new JTextArea(8, 30);
JButton editEntryButt = new JButton("Edit");

JButton saveEntryButt = new JButton("Save");
saveEntryButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
save(desigTxtField, detailsTxtArea);
}
});

JButton eraseEntryButt = new JButton("Erase");

JScrollPane viewAddScrollpane = new JScrollPane(detailsTxtArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

JScrollPane viewAddGListScrollpane = new JScrollPane(groupList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

addComponent(viewAddPane, new JLabel("Designation:"), 0, 0,
1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE);

addComponent(viewAddPane, desigTxtField, 1, 0, 3, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);


addComponent(viewAddPane, new JLabel("Details:"), 0, 2,
1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE);

addComponent(viewAddPane, viewAddGListScrollpane, 4, 1, 3, 3,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);

addComponent(viewAddPane, viewAddScrollpane, 1, 1,
3, 3, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);

addComponent(viewAddPane, editEntryButt, 1, 5, 1, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);

addComponent(viewAddPane, saveEntryButt, 2, 5, 1, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);

addComponent(viewAddPane, eraseEntryButt, 3, 5, 1, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);

CheckListCellRenderer renderer = new CheckListCellRenderer();
groupList.setCellRenderer(renderer);
groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

//**************** end add view pane ************///

JTabbedPane tabs = new JTabbedPane();
tabs.addTab("List Entry", listEntryPane);
tabs.addTab("View/Edit Entry", viewAddPane);
tabs.addTab("Groups", groupPane);


setContentPane(tabs);
}

private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, int anchor,
int fill)
{
Insets insets = new Insets(2,2,2,2);
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
public void run()
{
AddBookFrame rb = new AddBookFrame();
rb.pack();
rb.setVisible(true);

}


class CheckListCellRenderer extends JCheckBox
implements ListCellRenderer
{
protected Border noFocusBorder =
new EmptyBorder(1, 1, 1, 1);

public CheckListCellRenderer()
{
super();
setOpaque(true);
setBorder(noFocusBorder);
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
setText(value.toString());

setBackground(isSelected ? list.getSelectionBackground() :
list.getBackground());

setForeground(isSelected ? list.getSelectionForeground() :
list.getForeground());

setFont(list.getFont());
setBorder((cellHasFocus) ?
UIManager.getBorder("List.focusCellHighlightBorder") :
noFocusBorder);

return this;
}
}

public void save(JTextField desigTxtField, JTextArea detailsTxtArea)
{
System.out.println(desigTxtField.getText());
System.out.println(detailsTxtArea.getText());
}
}



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java TextArea question

Java Method Question www.computing.net/answers/programming/java-method-question/14684.html

Java noob question www.computing.net/answers/programming/java-noob-question/13768.html

java BufferedReader question www.computing.net/answers/programming/java-bufferedreader-question/10020.html