I will run the program, but when I activate the event, the JFrame will not update (it only removes the JLabel ) unless I manually drag the window to resize it, even with the repaint() being called after the event takes place. What's wrong?
public Driver() {
setLayout( new FlowLayout() );
pass = new JPasswordField( 4 );
add( pass );
image = new ImageIcon( "closedD.png" );
label = new JLabel( "Enter the password to enter the journal of dreams" , image , JLabel.LEFT );
add( label );
button = new JButton( "Enter" );
add( button );
event e = new event();
button.addActionListener( e );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible( true );
setSize( 1600/2 , 900/2 );
setTitle( "Diary" );
}
//main method
//
//
public static void main( String[] args ) {
win = new Driver();
}
public class event implements ActionListener {
private boolean clickAgain = false;
public void actionPerformed( ActionEvent e ) {
if ( passEquals( password ) && clickAgain == false ) {
image2 = new ImageIcon( "openD.png" );
remove( label );
label = new JLabel( "Good Job! Here is the journal of dreams." , image2 , JLabel.LEFT );
add( label );
clickAgain = true;
}
repaint();
}
}
Any time you add or remove a component, you must tell its container to re-layout the current components it holds. You do this by calling revalidate() on it. You would then call repaint() after the revalidate call to have the container repaint itself.
public void actionPerformed( ActionEvent e ) {
if ( passEquals( password ) && clickAgain == false ) {
image2 = new ImageIcon( "openD.png" );
remove( label );
label = new JLabel( "Good Job! Here is the journal of dreams.",
image2 , JLabel.LEFT );
add( label );
clickAgain = true;
}
revalidate(); // **** added ****
repaint();
}
Note: your question is worded in such a way as if you assume that we know what you're trying to do. Please give us more information next time. The better and more informative the question, the better and more informative the answer.
Edit 2:
I wonder if you could simplify your code a bit. Instead of removing and adding a JLabel, better to just simply set the current JLabel's text and Icon:
public void actionPerformed( ActionEvent e ) {
if ( passEquals( password ) && clickAgain == false ) {
image2 = new ImageIcon( "openD.png" );
// remove( label ); // removed
label.setText( "Good Job! Here is the journal of dreams.");
label.setIcon(image2);
}
}
Related
We are trying to show the user a milky JGlassPane to indicate that events are currently not accepted, due to a progress-bar or something else currently being shown. While this would usually automatically work, since we are showing a modal dialog, we also want the JGlassPane to be usable without a JDialog, meaning just a JGlassPane and a waiting cursor.
So far, we did this by using a JGlassPane that ignores events and paints a translucent color. Initially we call requestFocus on the JGlassPane. The problem with this is, that we lose the previously focused components when the glasspane goes away. The solution for this was to simply remember the previously focused component. However, in cases where the Action, that triggered the JGlassPane, was invoked via a menu, the focused component was the windows JRootPane. Usually swing correctly restores the focus after closing a menu, however, in this case, we are breaking this functionallity. Is there a way to achieve what we want without breaking the focus behaviour?
In this demo you can see, that the focus is correctly returned when hitting ESC after using one of the two buttons, but not with the context menu on the table.
public class FocusDemo
{
//Temporary static veriable for demo purposes.
private static final AtomicReference<Component> previouslyFocused = new AtomicReference<>();
private static class ConsumingGlassPane extends JPanel
{
public ConsumingGlassPane()
{
setOpaque( false );
setFocusable( true );
addMouseListener( new MouseListener()
{
#Override
public void mouseClicked( #NonNull final MouseEvent e )
{
e.consume();
}
#Override
public void mousePressed( #NonNull final MouseEvent e )
{
e.consume();
}
#Override
public void mouseReleased( #NonNull final MouseEvent e )
{
e.consume();
}
#Override
public void mouseEntered( #NonNull final MouseEvent e )
{
e.consume();
}
#Override
public void mouseExited( #NonNull final MouseEvent e )
{
e.consume();
}
} );
addKeyListener( new KeyListener()
{
#Override
public void keyTyped( final KeyEvent e )
{
e.consume();
}
#Override
public void keyReleased( final KeyEvent e )
{
e.consume();
}
#Override
public void keyPressed( final KeyEvent e )
{
if ( e.getKeyCode() == KeyEvent.VK_ESCAPE )
{
setVisible( false );
final Component component = previouslyFocused.get();
if ( component != null )
{
component.requestFocusInWindow();
}
}
e.consume();
}
} );
// This component keeps the focus until is made hidden
setInputVerifier( new InputVerifier()
{
#Override
public boolean verify( final JComponent input )
{
return !isVisible();
}
} );
}
#Override
protected void paintComponent( final Graphics g )
{
final Graphics2D g2 = (Graphics2D) g.create();
g2.setColor( new Color( 240, 230, 230, 128 ) );
g2.fillRect( 0, 0, getWidth(), getHeight() );
g2.dispose();
}
}
public static void main( final String[] args )
{
final JFrame frame = new JFrame();
frame.setGlassPane( new ConsumingGlassPane() );
final ActionListener showglasspane = __ ->
{
previouslyFocused.set( frame.getFocusOwner() );
frame.getRootPane().getGlassPane().setVisible( true );
frame.getRootPane().getGlassPane().requestFocus();
};
final JButton button1 = new JButton( "Show GlassPane" );
final JButton button2 = new JButton( "Show GlassPane" );
button1.addActionListener( showglasspane );
button2.addActionListener( showglasspane );
final Object[][] objects = { { 1, 2, 3 }, { 1, 2, 3 } };
final String[] col = { "Lel", "Lol", "Lul" };
final JTable table = new JTable( objects, col );
final JMenuItem menuItem = new JMenuItem( "Show GlassPane" );
menuItem.addActionListener( showglasspane );
final JPopupMenu popup = new JPopupMenu();
popup.add( menuItem );
table.setComponentPopupMenu( popup );
frame.setLayout( new GridBagLayout() );
frame.add( button1 );
frame.add( button2 );
frame.add( table );
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
So I have 2 different panels, the first panel works like it should but the Actionlistener might be needed to solve my issue:
class knopHandler implements ActionListener
{
public void actionPerformed ( ActionEvent e )
{
JFrame frame2 = new JFrame ( "Total Hours" );
frame2.setSize ( 600, 500 );
JPanel uitvoerpanel = new uitvoerpanel();
frame2.setContentPane( uitvoerpanel );
frame2.setVisible( true );
frame1.setVisible( false );
String invoerstring1 = maandaginvoer.getText();
int getal1 = Integer.parseInt( invoerstring1 );
String invoerstring2 = dinsdaginvoer.getText();
int getal2 = Integer.parseInt( invoerstring2 );
String invoerstring3 = woensdaginvoer.getText();
int getal3 = Integer.parseInt( invoerstring3 );
String invoerstring4 = donderdaginvoer.getText();
int getal4 = Integer.parseInt( invoerstring4 );
String invoerstring5 = vrijdaginvoer.getText();
int getal5 = Integer.parseInt( invoerstring5 );
String invoerstring6 = zaterdaginvoer.getText();
int getal6 = Integer.parseInt( invoerstring6 );
String invoerstring7 = zondaginvoer.getText();
int getal7 = Integer.parseInt( invoerstring7 );
int resultaat = getal1 + getal2 + getal3 + getal4 + getal5 + getal6 + getal7;
}
}
The relevant part of the second panel looks like this :
public uitvoerpanel()
{
setLayout( null );
naamvak = new JTextField ( 20 );
naamvak.setHorizontalAlignment ( JTextField.LEFT );
naamvak.setEditable ( false );
naamvak.setText( naaminvoer.getText() );
badgevak = new JTextField ( 20 );
badgevak.setHorizontalAlignment ( JTextField.LEFT );
badgevak.setEditable ( false );
badgevak.setText( badgeinvoer.getText() ) ;
totaalurenvak = new JTextField ( 20 );
totaalurenvak.setHorizontalAlignment ( JTextField.LEFT );
totaalurenvak.setEditable ( false );
totaalurenvak.setText( "" + resultaat );
The first panel has a few textfields where people can type something, I want the text from those textfields to appear in the textfields in the second panel but I am at a loss. When I run the program now nothing appears in the textfields in the second panel.
The "int resultaat" from the actionlistener should also appear in one of the textfields but I have also not been able to get this to work.
Am I putting the setter/getter in the right place or should this be in the actionlistener for example? Or am I missing a different piece of code?
Say, your button is jButton1 and your textFields are jTextField1 and jTextField2
Add an actionListner for your button as:
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
Then simply implement this method:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String s=jTextField1.getText();
jTextField2.setText(s);
}
Event handling with Action Listeners in Swing is straightforward.
As mentioned in the docs, you need to perform the following steps:
Declare an event handler class and specify that the class either
implements an ActionListener interface or extends a class that
implements an ActionListener interface. For example:
public class MyClass implements ActionListener {
Register an instance of the event
handler class as a listener on one or more components. For example:
someComponent.addActionListener(instanceOfMyClass);
Include code that
implements the methods in listener interface. For example:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action... }
I see that you are already trying to do some of these.
The problem in your method is that you cannot have an action listener on a panel which is a container of components. You need to define your action listener for individual components e.g. in your case, the text field maandaginvoer. You need to register (step 2) your listener with the text field on the panel 1.
Once you fix this problem, you should be able to see your desired output.
I want to set the most common JTabbedPane mouse event behavior, but I cannot find appropriate options to set:
Left mouse button click - Select tab.
Right mouse button click - Open current tab' dropdown menu.
Wheel mouse button click - Close the tab.
Question: Is there any way to implement them?
PS: Any example from here could be an SSCCE.
Tab selection is performed with left mouse button by default, so you don't need to add that feature. Everything else you can find in this small example:
public static void main ( String[] args )
{
final JFrame frame = new JFrame ();
final JTabbedPane tabbedPane = new JTabbedPane ();
tabbedPane.addTab ( "tab1", new JLabel ( "" ) );
tabbedPane.addTab ( "tab2", new JLabel ( "" ) );
tabbedPane.addTab ( "tab3", new JLabel ( "" ) );
tabbedPane.addTab ( "tab4", new JLabel ( "" ) );
frame.add ( tabbedPane );
tabbedPane.setUI ( new MetalTabbedPaneUI ()
{
protected MouseListener createMouseListener ()
{
return new CustomAdapter ( tabbedPane );
}
} );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
frame.setVisible ( true );
}
private static class CustomAdapter extends MouseAdapter
{
private JTabbedPane tabbedPane;
public CustomAdapter ( JTabbedPane tabbedPane )
{
super ();
this.tabbedPane = tabbedPane;
}
public void mousePressed ( MouseEvent e )
{
final int index = tabbedPane.getUI ().tabForCoordinate ( tabbedPane, e.getX (), e.getY () );
if ( index != -1 )
{
if ( SwingUtilities.isLeftMouseButton ( e ) )
{
if ( tabbedPane.getSelectedIndex () != index )
{
tabbedPane.setSelectedIndex ( index );
}
else if ( tabbedPane.isRequestFocusEnabled () )
{
tabbedPane.requestFocusInWindow ();
}
}
else if ( SwingUtilities.isMiddleMouseButton ( e ) )
{
tabbedPane.removeTabAt ( index );
}
else if ( SwingUtilities.isRightMouseButton ( e ) )
{
final JPopupMenu popupMenu = new JPopupMenu ();
final JMenuItem addNew = new JMenuItem ( "Add new" );
addNew.addActionListener ( new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
tabbedPane.addTab ( "tab", new JLabel ( "" ) );
}
} );
popupMenu.add ( addNew );
final JMenuItem close = new JMenuItem ( "Close" );
close.addActionListener ( new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
tabbedPane.removeTabAt ( index );
}
} );
popupMenu.add ( close );
final JMenuItem closeAll = new JMenuItem ( "Close all" );
closeAll.addActionListener ( new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
tabbedPane.removeAll ();
}
} );
popupMenu.add ( closeAll );
final Rectangle tabBounds = tabbedPane.getBoundsAt ( index );
popupMenu.show ( tabbedPane, tabBounds.x, tabBounds.y + tabBounds.height );
}
}
}
}
Ofcourse you'd better save the displayed menu somewhere so it won't be recreated every time user opens it. You can also move the mouse listener to a separate class to use it every time you need menu and other features.
But my goal was to show you how those things can be done and not making a perfect example, so i guess it is more than enough to start working with tabbed pane :)
I am trying to flash the icon to the user using a GlassPane. I am running a javax.swing.Timer which basically performs this:
for (int i = 0; i < 3; i++) {
frame.getGlassPane().setVisible(true);
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
//To change body of catch statement use File | Settings | File Templates.
e1.printStackTrace();
}
frame.getGlassPane().setVisible(false);
}
Unfortunatly, if I sleep the EDT (current thread within the timer), the icon does not show, as in the paintComponent method did not manage to get invoked fully before the thread went to sleep. Therefore, when the next instruction kicks in, the glass pane is hidden, and, as a result, the icon is never shown. Is there a way to achieve what I want using this (similiar) approach?
You could use a javax.swing.Timer
public FlashTimer() {
javax.swing.Timer flashTimer = new javax.swing.Timer(500, new FlashHandler());
flashTimer.setCoalesce(true);
flashTimer.setRepeats(true);
flashTimer.setInitialDelay(0);
}
public class FlashHandler implements ActionListener {
private int counter;
#Override
public void actionPerformed(ActionEvent ae) {
countrol.setVisible(counter % 2 == 0);
counter++;
if (counter > 3) {
((Timer)ae.getSource()).stop();
}
}
}
It should be obvious - use a separate Thread and do the "blinking logic" there but modify the UI in EDT. Here is a simple example (should be enough to understand the idea):
public static void main ( String[] args )
{
JFrame frame = new JFrame ();
final JLabel label = new JLabel ( "X" );
label.setBorder ( BorderFactory.createEmptyBorder ( 90, 90, 90, 90 ) );
frame.add ( label );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
new Thread ( new Runnable ()
{
public void run ()
{
for ( int i = 0; i < 15; i++ )
{
try
{
setVisible ( false );
Thread.sleep ( 500 );
setVisible ( true );
Thread.sleep ( 500 );
}
catch ( InterruptedException e1 )
{
//
}
}
}
private void setVisible ( final boolean visible )
{
SwingUtilities.invokeLater ( new Runnable ()
{
public void run ()
{
label.setVisible ( visible );
}
} );
}
} ).start ();
}
I'm trying to write an app that goes something like this:
- Display a dialog
- When user clicks OK, close dialog, go to main app
Here are the relevant code snippets:
public class Owari extends JPanel implements ActionListener, MouseListener, Runnable {
// FIELDS
JFrame frame;
JTextField IP;
String IPAddress;
static final int SERVER_MODE = 0;
static final int CLIENT_MODE = 1;
int mode;
OwariBoard board;
public static void main( String[] args ) {
SwingUtilities.invokeLater( new Owari() );
}
Owari() {
setPreferredSize( new Dimension( WIDTH, HEIGHT ) );
board = new OwariBoard();
}
void main() {
this.addMouseListener( this );
frame.dispose();
frame = new JFrame( "Owari" );
frame.setContentPane( this );
frame.pack();
frame.setVisible(true);
if ( mode == SERVER_MODE ) {
server();
}
if ( mode == CLIENT_MODE ) {
client();
}
}
public void run() {
frame = new JFrame( "Owari" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JPanel init = new JPanel( new GridBagLayout() );
frame.setContentPane( init );
add some components to the init panel including a button with
this as its actionListener and OK as its command.
frame.pack();
frame.setVisible( true );
}
public void actionPerformed( ActionEvent e ) {
if ( e.getActionCommand().equals( "Client" ) ) {
mode = CLIENT_MODE;
IP.setVisible( true );
}
else if ( e.getActionCommand().equals( "Server" ) ) {
mode = SERVER_MODE;
IP.setVisible( false );
}
else {
IPAddress = IP.getText();
main();
}
}
public void paintComponent( Graphics g ) {
super.paintComponent( g );
System.out.println( "painting" );
do some paintin
}
void server() {
frame.setTitle( "Owari Server" );
try {
server = new ServerSocket( 666 );
socket = server.accept();
initIO();
} catch ( IOException e ) {}
yourTurn = true;
System.out.println( "Got to end of server()" ); // At this point, the window
DOES get painted
What happens is the following:
The initial dialog displays:
I click the OK button.
The main window gets resized to the preferred size of the main app but it doesn't get painted, it's just transparent (shown here with this page as the background, heh):
http://imgur.com/6Ssij.jpg
I can tell the paintComponent method hasn't been called because "painting" isn't printed to the console.
However, "got to this point in the program" DOES get printed, so the program isn't hanging, it's just not calling paintComponent.
Then when I launch a client and connect, the app finally gets painted, and "painting" and "got a client" get printed to the console.
Also later on in the app, calls to repaint() are delayed (ie paintComponent is actually called later in the program than when the call to repaint() is made).
I also tried replacing the initial dialog using sthing along the lines of
public void main
frame.getRootPane.removeAll()
frame.setContentPane(this)
frame.getRootPane().revalidate()
frame.pack()
Exact same result.
tl;dr paintcomponent isn't being called when i want it to, what do?
Bumping for some more info: the call to repaint() is done before the call to sever.accept() So why does it not repaint() before hanging at the server.accept() call?
openasocketandwaitforaclient
Your code is executing in the Event Dispatch Thread so the blocking socket is preventing the GUI from repainting itself.
YOu need to use a separate Thread for the socket. Read the section from the Swing tutorial on Concurrency for an explanation and a solution.
your code seems to work so, maybe you should try to invoke the repaint() methode of you frame after resizing this frame.
Anhuin