I have made this simple text editor program but can't figure out how to change GUI component's properties while the program is running.
Suppose this is a part of my Text Editor's source code:
boolean wordwrap = false;
void mainFrame() {
frame = new JFrame("Text Editor");
textArea = new JTextArea(50,20);
textArea.setLineWrap(wordwrap);
and let's say I have an event source(JButton) added as Listener to change
textArea's .setLineWrap(boolean). Just like this:
public void actionPerformed(ActionEvent event) {
if(wordwrap) wordwrap = false;
else wordwrap = true;
textArea.setLineWrap(wordwrap);
frame.repaint();
}
But this code is not working!!. So, what is the correct way to update or edit a JAVA GUI component while the program is running ?
revalidate and validate()
will update the frame.
You do not need to use repaint().
Final Method:
public void actionPerformed(ActionEvent event) {
if(wordwrap) wordwrap = false;
else wordwrap = true;
textArea.setLineWrap(wordwrap);
frame.revalidate(); //is preferable but validate() also works.
}
You can either update the whole frame or just update the jComponent (insert TextArea instead of "frame".revalidate();)
Just FYI, after I got a chance to test it, it works fine without either the revalidate() or the repaint(). I suspect the problem was somewhere else in your code.
public class TestTextArea
{
private final static String testLine =
"This is some rather long line that I came up with for testing a textArea.";
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run()
{
gui();
}
} );
}
private static void gui()
{
JFrame frame = new JFrame();
final JTextArea textArea = new JTextArea();
JPanel span = new JPanel();
JButton toggle = new JButton( "Switch line wrap" );
toggle.addActionListener( new ActionListener()
{
#Override
public void actionPerformed( ActionEvent e )
{
textArea.setLineWrap( !textArea.getLineWrap() );
}
} );
for( int i = 0; i < 10; i++ )
textArea.append( testLine + testLine + "\n" );
span.add( toggle );
frame.add( span, BorderLayout.SOUTH );
frame.add( textArea );
frame.pack();
frame.setSize( 500, 500 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
Related
I want to make a kind of digital clock which you can activate by using enter to kind of refresh the clock display, for that I use this method:
private static void GUI(String time, int action){
JLabel textLabel = new JLabel(time);
JPanel panel = new JPanel();
JFrame enterMessage = new JFrame("Tester");
if (action == 1){
enterMessage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
enterMessage.setSize(190, 80);
enterMessage.setVisible(true);
panel.setBackground(Color.WHITE);
panel.add(textLabel);
enterMessage.add(panel);
}else {
System.out.printf("Refresh");
panel.revalidate();
enterMessage.revalidate();
panel.repaint();
enterMessage.repaint();
}
}
}
This method gets called twice in the program code: one time to make the GUI upon opening the program and everytime an enterpress is detected to refresh it. I searched on internet how to refresh a JPanel and I found that you needed to use revalidate(); and then repaint(); but it does not refresh the time displayed by the panel. How would I refresh it?
ps:the time is passed from the main as a string and everytime an enterpress is detected gets overwritten and passed
Follow Java naming conventions. Variable names should NOT start with an upper case character.
Don't keep adding the label to the panel. Just use the setText(...) method of JLabel to change the text being displayed.
Edit:
An example of a SSCCE that shows you how to use the setText(...) method:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class TimerTime extends JPanel implements ActionListener
{
private JLabel timeLabel;
public TimerTime()
{
timeLabel = new JLabel( new Date().toString() );
add( timeLabel );
Timer timer = new Timer(1000, this);
timer.setInitialDelay(1);
timer.start();
}
#Override
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.getSource());
timeLabel.setText( new Date().toString() );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("TimerTime");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TimerTime() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
I am having an issue with my horizontal scroll bar demo. I am trying to move a custom message panel using the scroll bar. I used an anonymous listener and override the adjustmentValueChanged() method like so,
public void adjustmentValueChanged(AdjustmentEvent e){
System.out.println(e.getAdjustmentType());
if( e.getAdjustmentType() == AdjustmentEvent.UNIT_INCREMENT ) {
panel.moveLeft();
}
}
I am trying to get the AdjustmentEvent using e.getAdjustmentType() so I can properly handle the adjustment of the message panel. However, it is not working. I used System.out.println() method to print the adjustment type on my screen to see what the problem is but what I am not understanding is that no matter what part of the scroll bar I press (whether it is the unit increment, unit decrement, block increment, etc..) the value returned is 5? I am not sure what the issue is can someone please help.
public class ScrollBarDemo extends JFrame {
private JScrollBar scrollHort = new JScrollBar(JScrollBar.HORIZONTAL);
private JScrollBar scrollVert = new JScrollBar(JScrollBar.VERTICAL);
private MessagePanel panel = new MessagePanel("Welcom to Java bitch");
public static void main(String[] args) {
ScrollBarDemo frame = new ScrollBarDemo();
frame.setTitle("ScrollBarDemo");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ScrollBarDemo() {
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
add(scrollHort, BorderLayout.SOUTH);
add(scrollVert, BorderLayout.EAST);
scrollHort.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println(e.getAdjustmentType());
if (e.getAdjustmentType() == AdjustmentEvent.UNIT_INCREMENT) {
panel.moveLeft();
}
}
});
}
}
Run the following sample, the method to use is getValue().
public class ScrollBarDemo extends JFrame {
public ScrollBarDemo() {
setLayout( new BorderLayout());
final JScrollBar scrollHort = new JScrollBar( JScrollBar.HORIZONTAL );
add( scrollHort, BorderLayout.SOUTH );
scrollHort.addAdjustmentListener( e -> System.out.println( e.getValue()));
setLocationRelativeTo(null);
setDefaultCloseOperation( EXIT_ON_CLOSE );
pack();
setVisible(true);
}
public static void main( String[] args ) {
new ScrollBarDemo();
}
}
the normal behaviour of native text fields in many environments is as follows:
Textfield with text "abcdefg". I use the mouse to select "efg" from left to right. The caret is now behind "g". When I move the caret to the left by pressing the cursor left key once, the selection is removed and the caret is right before "e". When I do the same in a JTextField or JTextArea (tested on Mac OS) doing the exact same thing results in the caret being right before "g".
I know how I could change that programmatically by using a KeyListener and registering it with each component but I am looking for a way to change that for my entire application. Is that possible? Is there a Flag, I am not finding or do I have to hack my look and feel?
Thanks
I am looking for a way to change that for my entire application
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class CaretAction extends TextAction
{
private boolean caretForward;
public CaretAction(boolean caretForward)
{
super(caretForward ? "Caret Forward" : "Caret Backward");
this.caretForward = caretForward;
}
public void actionPerformed(ActionEvent e)
{
JTextComponent textComponent = getFocusedComponent();
int start = textComponent.getSelectionStart();
int end = textComponent.getSelectionEnd();
int offset = textComponent.getCaretPosition();
if (start != end)
{
if (caretForward)
offset = (offset == end) ? offset + 1 : end;
else
offset = (offset == start) ? offset -1 : start;
}
else
{
offset += (caretForward) ? 1 : -1;
}
offset = Math.max(offset, 0);
offset = Math.min(offset, textComponent.getDocument().getLength());
textComponent.setCaretPosition( offset );
}
private static void createAndShowUI()
{
JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(10);
JPanel panel = new JPanel();
panel.add( textField1 );
panel.add( textField2 );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
ActionMap map = (ActionMap)UIManager.get("TextField.actionMap");
map.put(DefaultEditorKit.backwardAction, new CaretAction(false));
map.put(DefaultEditorKit.forwardAction, new CaretAction(true));
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
You would also need to change the ActionMap for JTextArea, JFormattedTextField ...
What you can do is addCaretListener :
anyField.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent evt) {
anyFieldCaretUpdate(evt);
}
});
And set the Caret at the last again:
private void anyFieldCaretUpdate(CaretEvent evt) {
anyField.setCaretPosition(anyField.getText().length());
}
I have a panel which contains JList.
When i add this panel to west BorderLayout with one element everything is OK and i see one element in it but if i add new element or clear all element i see no effect.
Can any body suggest any solution?
JPanel class which contains JList
public class FtpPanel extends JPanel{
public JList ftpJList;
public DefaultListModel ftpListModel;
public FtpPanel(String[] list) {
this.setLayout(new BorderLayout());
this.setBorder(new EmptyBorder(20, 20, 20, 20));
this.ftpListModel = new DefaultListModel();
for(String s : list){
this.ftpListModel.addElement(s);
}
this.ftpJList = new JList(ftpListModel);
final JScrollPane wsp = new JScrollPane(this.ftpJList);
wsp.setBorder(new TitledBorder(new WebBorder(),"ftpsrv.itra.de"));
this.add(wsp, BorderLayout.CENTER);
}
}
FtpTabPanel to which FtpPanel will be added
public class FtpTabPanel extends JPanel{
public FtpPanel ftpPanel;
public FtpTabPanel() {
createComponents();
layoutComponents();
initializeComponents();
}
private void createComponents() {
ftpPanel = new FtpPanel(new String[]{"You aren't Connected"});
}
private void layoutComponents() {
setLayout(new BorderLayout());
add(ftpPanel, BorderLayout.WEST);
}
}
Add and remove from Jlist
addFileToJlist(listOfFtpFile ){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if(listOfFtpFile !=null)
ftpTabPanel.ftpPanel.ftpListModel.clear();
ftpTabPanel.ftpPanel.updateUI();
for(String s : listOfFtpFile){
ftpTabPanel.ftpPanel.ftpListModel.addElement(s);
ftpTabPanel.ftpPanel.ftpWebList.validate();
ftpTabPanel.ftpPanel.updateUI();
}
ftpTabPanel.ftpPanel.ftpWebList.revalidate();
panel.updateUI();
}
});
}
Here is a simple working example (that is almost same as yours but without unnecessary updates):
public static void main ( String[] args )
{
JFrame frame = new JFrame ();
final DefaultListModel model = new DefaultListModel ();
JList list = new JList ( model );
frame.add ( new JScrollPane ( list ) );
list.addMouseListener ( new MouseAdapter ()
{
public void mousePressed ( MouseEvent e )
{
model.clear ();
Random random = new Random ();
int max = random.nextInt ( 10 );
for ( int i = 0; i <= max; i++ )
{
model.addElement ( "" + random.nextInt ( 100 ) );
}
}
} );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setSize ( 200, 400 );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}
And i don't really see any problems in your example (except unnecessary validation and updateUI calls). Seems that problem is somewhere else...
I had a similar problem recently. Solved it simply like this :
(Add elements to Jlist)
Jlist.setVisible(false);
Jlist.setVisible(true);
Only way it worked by me, donĀ“t know why.
I have 6 JLabel and each is having a different instance of a mouselistener class attached. How to know which JLabel has been clicked ? These JLabel form a two dimentional array.
You use getSource to get a refrence to the object which is clicked on:
label1.addActionListener(new yourListener());
label2.addActionListener(new yourListener());
public class yourListener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
JLabel labelReference=(JLabel)e.getSource();
labelReference.someMethod();
}
}
The easiest way I did something like that was, to use JButtons and make them look like JLabels by the using this syntax formatting.
jButton.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
jButton.setBorderPainted( false );
jButton.setContentAreaFilled( false );
jButton.setFocusPainted( false );
jButton.setHorizontalAlignment( SwingConstants.LEFT );
Then, what you want to is add an ActionLister and a ActionCommand. For example
jButton.addActionListener( this );
jButton.setActionCommand( "label1" );
Then just handle the actionListners to do what you wanted for each label.
public void actionPerformed( ActionEvent arg0 )
{
String command = arg0.getActionCommand();
if( command.equalsIgnoreCase( "label1" ) )
{
//label1 code
}
}
As mentioned below this also has the added benefit of supporting both keyboard and mouse activities.
I put this together based on your description:
public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new FlowLayout());
for (int i = 0; i < 6; i++) {
JLabel l = new JLabel("Label " + (i + 1));
l.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
JLabel l = (JLabel) e.getSource(); // here
System.out.println(l.getText());
}
});
f.add(l);
}
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
I think the line marked // here is mostly what you need.