'Java'에 해당되는 글 13건

  1. 2021.12.20 java swing 동적 툴팁, 멀티 라인 툴팁
  2. 2021.12.13 swing FlowLayout 간격 없애기
  3. 2021.12.13 Swing 단축키(KeyStroke) 등록하기

java swing 동적 툴팁, 멀티 라인 툴팁

프로그래밍/java 2021. 12. 20. 22:28
반응형

getToolTipText 메소드 override 로 구현

https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#getToolTipText-java.awt.event.MouseEvent-

 

JComponent (Java Platform SE 8 )

Returns true if a paint triggered on a child component should cause painting to originate from this Component, or one of its ancestors. Calling repaint(long, int, int, int, int) or paintImmediately(int, int, int, int) on a Swing component will result in ca

docs.oracle.com

package test.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

// dynamic tooltip
public class MainTest {
    public static void main(String[] args) {
        MainUI mainUI = new MainUI();
        mainUI.setVisible(true);
    }
}

class ToolTipButton extends JButton {
    boolean mIsNum = true;

    public ToolTipButton(String text) {
        super(text);
    }

    // 1. use setToolTipText
    @Override
    public String getToolTipText(MouseEvent event) {
        if (mIsNum) {
            setToolTipText("0123456789");
        }
        else {
            setToolTipText("ABCDEFG");
        }
        mIsNum = !mIsNum;
        return super.getToolTipText(event);
    }

    // 2. return text directly
    @Override
    public String getToolTipText(MouseEvent event) {
        String text = "";
        if (mIsNum) {
            text = "0123456789";
        }
        else {
            text = "ABCDEFG";
        }
        mIsNum = !mIsNum;
        return text;
    }
}

class MainUI extends JFrame {

    MainUI() {
        setPreferredSize(new Dimension(400, 300));
        setLayout(new FlowLayout());
        JButton button = new ToolTipButton("TEST");
        button.setToolTipText("");
        button.setPreferredSize(new Dimension(100, 100));
        add(button);
        pack();
    }
}

멀티라인 툴팁

setToolTipText("<html>a<br>b</html>");
반응형
:

swing FlowLayout 간격 없애기

프로그래밍/java 2021. 12. 13. 22:27
반응형

https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/FlowLayout.html

 

FlowLayout (Java SE 17 & JDK 17)

All Implemented Interfaces: LayoutManager, Serializable A flow layout arranges components in a directional flow, much like lines of text in a paragraph. The flow direction is determined by the container's componentOrientation property and may be one of two

docs.oracle.com

// layout 생성시
public FlowLayout(int align, int hgap, int vgap)
new FlowLayout(FlowLayout.LEADING, 0, 0);

// 가로 간격 없애기
public void setHgap(int hgap)
layout.setHgap(0);

// 세로 간격 없애기
public void setVgap(int vgap)
layout.setVgap(0);
반응형
:

Swing 단축키(KeyStroke) 등록하기

프로그래밍/java 2021. 12. 13. 21:00
반응형

ESC 입력시 창 닫기

package test.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

public class MainTest {
    public static void main(String[] args) {
        MainUI mainUI = new MainUI();
        mainUI.setVisible(true);
    }
}

class MainUI extends JFrame {
    MainUI() {
        setPreferredSize(new Dimension(400, 300));
        setLayout(new FlowLayout());
        JButton button = new JButton("TEST");
        button.setPreferredSize(new Dimension(100, 100));
        add(button);
        pack();

        KeyStroke escStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        String actionMapKey = getClass().getName() + ":WINDOW_CLOSING";
        Action closingAction = new AbstractAction() {
            public void actionPerformed(ActionEvent event) {
                MainUI.this.dispatchEvent(new WindowEvent(MainUI.this, WindowEvent.WINDOW_CLOSING));
            }
        };

        installKeyStroke(this, escStroke, actionMapKey, closingAction);
    }

    public void installKeyStroke(final RootPaneContainer container, final KeyStroke stroke, final String actionMapKey, final Action action) {
        JRootPane rootPane = container.getRootPane();
        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, actionMapKey);
        rootPane.getActionMap().put(actionMapKey, action);
    }
}

 

반응형
: