How can I make it like that? 1) If user opens that program, you don't have to click on textfield to receive focus, instead, if you switch to that program, it immediatetly gives you a focus to write instead of clicking for focus.
2) If user writes a ID number or item name to textfield, textarea responds to textfield and shows that ID or name to the user, like, pops up.
3) How to make textarea smaller in case, that even panel shows out in corners? I'd like to make a textarea a little bit smaller like a box and outside a box, its just a gray color.
In order to fully help me, I'll gladly give out the code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
public abstract class Itemlist extends JFrame implements ActionListener, FocusListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
// create JFrame
JFrame frame = new JFrame("small project");
frame.setDefaultCloseOperation(2);
frame.setPreferredSize(new Dimension(400, 600));
frame.setLayout(new BorderLayout());
frame.setResizable(false);
JPanel panel = new JPanel(new FlowLayout(SwingConstants.LEADING, 10, 10));
frame.add(panel);
panel.setPreferredSize(new Dimension(100, 50));
JTextField tf = new JTextField(25);
tf.setPreferredSize(new Dimension(100, 35));
Font f = new Font("Times New Roman", Font.BOLD, 18);
tf.setFont(f);
panel.add(tf);
frame.add(tf, BorderLayout.PAGE_END);
tf.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
tf.getText();
}
@Override
public void focusLost(FocusEvent e) {
tf.setText("");
}
});
JTextArea ta = new JTextArea();
frame.add(ta);
Font font = new Font("Times New Roman", Font.PLAIN, 16);
ta.setEditable(false);
ta.setFont(font);
JScrollPane scrollPane = new JScrollPane(ta);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
BufferedReader br;
String contents;
try {
br = new BufferedReader(new FileReader("C:/ItemList.txt"));
contents = br.readLine();
while ((contents = br.readLine()) != null) {
ta.read(br, null);
}
br.close();
} catch (IOException e) {
}
frame.pack();
// frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
*All help is appreciated. Have been stuck on it.
WindowListener
and monitor for the windowActivated
event, at which time you can call requestFocusInWindow
on the JTextField
to transfer focus to it automatically. Focus management is a little bit of a black art, as it tends to work slightly differently on different platforms ActionListener
support. This will require the user to hit the "action" key (typically Enter ) to trigger it. If you want real time feedback, the a DocumentListener
would be what you're looking for JTextArea
s constructor to specify the preferred number of rows and columns. This will define the preferred scrollable size of the component, which will effect the size of the JScrollPane
as well
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.