Swing etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Swing etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

30 Mayıs 2022 Pazartesi

Swing MaskFormatter Sınıfı

Giriş
Şu satırı dahil ederiz. JFormattedTextField ile birlikte kullanılır
import javax.swing.text.MaskFormatter;
Örnek
Şöyle yaparız
JFormattedTextField customerId = new JFormattedTextField(new MaskFormatter("UUU_UUU"));
Mask Örnekleri
UUU_UUU
########-#### - Date
#.# - Product Version

4 Şubat 2021 Perşembe

Swing LookAndFeel Sınıfı

Örnek
Yeni bir LookAndFeel için şöyle yaparız. Burada LabelUI için kendi sınıfım kullanılıyor
import javax.swing.UIDefaults;
import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;

public class WindowsClassicLookAndFeelExt extends WindowsClassicLookAndFeel {
  @Override
  protected void initClassDefaults(UIDefaults table){
    super.initClassDefaults(table);
    Object[] uiDefaults = { "LabelUI", WindowsLabelExtUI.class.getCanonicalName()};
    table.putDefaults(uiDefaults);
  }
}
Bunu kodlamak için şöyle yaparız
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import com.sun.java.swing.plaf.windows.WindowsLabelUI;

public class WindowsLabelExtUI extends WindowsLabelUI {
  static WindowsLabelExtUI singleton = new WindowsLabelExtUI();

  public static ComponentUI createUI(JComponent c){
    c.putClientProperty("html.disable", Boolean.TRUE);    
    return singleton;
  }
}
Yeni LookAndFeel sınıfımızı kullanmak için şöyle yaparız
import javax.swing.UIManager;

public class Main {
  public static void main(String[] args){
    try {
UIManager.setLookAndFeel(WindowsClassicLookAndFeelExt.class.getCanonicalName()); }catch (Exception e){ ... } ... }

23 Temmuz 2020 Perşembe

Swing KeyStroke Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.swing.KeyStroke;
Kullanım
1. Bir component'in getInputMa()p ile InputMap nesnesi alınır. Bu nesneye put() metodu ile bir KeyStroke nesnesi ve bir string eklenir. Elimizde bir KeyStroke olsun
KeyStroke controlA = KeyStroke.getKeyStroke("control A");
Şöyle yaparız
JPanel panel = ...;

InputMap inputMap = panel.getInputMap();
inputMap.put(controlA, "foo");
2. Aynı component'in getActionMap() ile ActionMap nesnesi alınır. Bu nesneye put() metodu ile aynı string ve bir Action nesnesi eklenir. Böylece component'in tuş vuruşları ile bir action çalıştırılır.
Şöyle yaparız
JPanel panel = ...;

/* add a new action named "foo" to the panel's action map */
panel.getActionMap().put("foo", new AbstractAction() {
  public void actionPerformed(ActionEvent e) {
    System.out.println("hello, world");
  }
});
getKeyStroke metodu - keycode + modifiers
Ctrl + A için şöyle yaparız. Bazı örneklerde CTRL_DOWN_MASK kullanılıyor.
KeyStroke controlA = KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK);
getKeyStroke metodu - keycode + modifiers + onKeyRelease
Örnek ver

getKeyStroke metodu - String
Ctrl + A için şöyle yaparız.
KeyStroke controlA = KeyStroke.getKeyStroke("control A");

22 Temmuz 2020 Çarşamba

Thread İçinde Swing Bileşenine Çizim

Giriş
Bir seferinde bir ekran bileşenini periyodik bir şekilde çizmek gerekti. Periyodik olması için yeni bir thread başlatıldı. Swing sınıfının Timer metodu kullanılmadı çünkü flicker (titreme) yaratacağı düşünüldü. Ben bu çözümün en doğru çözüm olduğundan emin değilim ancak yine de not almak istedik. Bu yönteme Double Buffering artı Offscreen Drawing deniliyor.

Swing aslınd kendisi de Double Buffering yöntemini kullanıyor. Açıklaması şöyle
Double buffering means that instead of drawing directly on the screen, Swing first performs drawing operations in an offscreen buffer and then copies the completed work to the display in a single painting operation, as shown in Figure 20-5. It takes the same amount of time to do the drawing work, but once it’s done, double buffering instantaneously updates our display so that the user does not perceive any flickering or progressively rendered output.

You’ll see how to implement this technique yourself when we use an offscreen buffer later in this chapter. However, Swing does this kind of double buffering for you whenever you use a Swing component in a Swing container. AWT components do not have automatic double buffering capability.

It is interesting to take our example and turn off double buffering to see the effect. Each Swing JComponent has a method called setDoubleBuffered() that can be set to false in order to disable the technique. Or you can disable it for all components using a call to the Swing RepaintManager, as we’ve indicated in comments in the example. Try uncommenting that line of DragImage and observe the difference in appearance.
Aslında Swing otomatik olarak double buffer kullansa bile biz çizme işlemini bir başka thread içinde yaptığımız için, yine kendi elimizde bir Graphics2D nesnesi yaratmak gerekti.

Thread
Thread şu işleri yapıyordu

1. renderBuffer() metodu
Bu metod double buffering şeklinde çalışıyordu. İki tane Graphics2D nesneni dönüşümlü olarak kullanıyordu. Sırası gelen Graphics2D nesnesine çizim yapıldı. Örneğin indeks 0 ise birinci buffer'a çizim yapıldı. 1 ise ikinci buffer'a çizim yapıldı Metod şöylee
public void renderBuffer(){

  Graphics2D bufferedImageGraphics = (index == 0) : imageGraphics1 : imageGraphis2;
  ...
}
swapBuffer metodu
İndeks 0 ise 1, 1 ise 0 yapıldı

3 repaint metodu
Bu metod aslında paint() metodunu tetikliyor. Bu çağrı paint metodunu da asenkron olarak tetikliyor. Açıklaması şöyle
methods like repaint(), revalidate() are safe to use within any thread. Those methods actually queue requests to EDT(Event Dispatch Thread) to call paint() and validate(). So if you call repaint() many times using different threads, it will queue the request to call paint() method..
paint() metodu içinde yeni buffer çiziliyor. İndeks 0 ise ikinci buffer çiziliyor, 1 ise birinci buffer çiziliyor. Metod şöyle
public void paint(Graphics g) {

  if (firstTime){
    Dimension size = getSize();
    g.fillRect(0,0,size.width,size.height);

    BufferedImage image1 = (BufferedImage)createImage(size.width,size.height);
    Graphics2D imageGraphics1 = image1.createGraphics();

    BufferedImage image2 = (BufferedImage)createImage(size.width,size.height);
    Graphics2D imageGraphics2 = image2.createGraphics();

    firstTime = false;
  }

  BufferedImage bufferedImage = (index == 0) imageGraphics2 : imageGraphics1;

  Graphics2D g2 = (Graphics2D)g;
  g2.drawImage(bufferedImage,0,0,this);
}



31 Mart 2020 Salı

Swing AbstractTableModel Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.swing.table.AbstractTableModel;
constructor
Şöyle yaparız.
class DataModel extends AbstractTableModel{
  ArrayList<Object[]> data = new ArrayList<Object[]>();
  ArrayList<String> columnNames = new ArrayList<String>();

  public DataModel(ArrayList<String> cNames){
    super();
    columnNames = cNames;
    
  }
  ...
}
fireTableStructureChanged metodu
Açıklaması şöyle.
Model notifies the view that the data has changed. This would be done by invoking the: fireTableStructureChanged(…);
getColumnCount metodu
Şöyle yaparız.
public int getColumnCount()
{
  return columnNames.size();
};
getColumnName metodu
Şöyle yaparız.
public String getColumnName(int column)
{
  return columnNames.get(column);
}
getRowCount metodu
Şöyle yaparız.
public int getRowCount()
{
  return data.size();
};
getValueAt metodu
Şöyle yaparız.
public Object getValueAt(int rowIndex, int columnIndex){ 
  Object[] row = data.get(rowIndex);

  return row[columnIndex];
};
isCellEditable metodu
Şöyle yaparız.
public boolean isCellEditable(int rowIndex, int columnIndex)
{
  return true;
}
setValueAt metodu

Şöyle yaparızDüzenleme (Editing) işlemi bitince bu metod tetiklenir.
public void setValueAt(Object newValue, int rowIndex, int columnIndex){
  data.get(rowIndex)[columnIndex] = newValue;
  fireTableDataChanged();
}
Kendi metodumuz
Şöyle yaparız.
public void addRows (ArrayList<Object[]> rows) {
  for (int i = 0; i < rows.size(); i++) {
    Object[] clone = rows.get(i).clone();
    data.add(clone);
  }
  fireTableDataChanged();
}