Google Tag Manager

Showing posts with label JRadioButton. Show all posts
Showing posts with label JRadioButton. Show all posts

2016/05/10

aterai

The JRadioButton that is currently selected in the ButtonGroup set as the default focus component

Code

buttons.setFocusTraversalPolicyProvider(true);
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
  @Override public Component getDefaultComponent(Container focusCycleRoot) {
    ButtonModel selection = bg.getSelection();
    for (Component c: focusCycleRoot.getComponents()) {
      JRadioButton r = (JRadioButton) c;
      if (r.getModel().equals(selection)) {
        return r;
      }
    }
    return super.getDefaultComponent(focusCycleRoot);
  }
});

References

2013/12/09

aterai

Breadcrumb Navigation with JRadioButton

Code

private static JComponent makeBreadcrumbList(int overlap, List< String> list) {
  JPanel p = new JPanel(new FlowLayout(FlowLayout.LEADING, -overlap, 0));
  p.setBorder(BorderFactory.createEmptyBorder(4, overlap+4, 4, 4));
  p.setOpaque(false);
  ButtonGroup bg = new ButtonGroup();
  for(String title: list) {
    AbstractButton b = makeButton(title, Color.PINK);
    p.add(b);
    bg.add(b);
  }
  return p;
}

References

2012/11/20

aterai

Make a ToggleButtonBar with JRadioButtons

Code

class ToggleButtonBarCellIcon implements Icon{
  private static final Color TL = new Color(1f,1f,1f,.2f);
  private static final Color BR = new Color(0f,0f,0f,.2f);
  private static final Color ST = new Color(1f,1f,1f,.4f);
  private static final Color SB = new Color(1f,1f,1f,.1f);

  private Color ssc;
  private Color bgc;

  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    int r = 8;
    int w = c.getWidth();
    int h = c.getHeight();

    Container parent = c.getParent();
    if(parent==null) {
      return;
    }

    Graphics2D g2 = (Graphics2D)g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    Path2D.Float p = new Path2D.Float();

    if(c==parent.getComponent(0)) {
      //:first-child
      p.moveTo(x, y + r);
      p.quadTo(x, y, x + r, y);
      p.lineTo(x + w, y);
      p.lineTo(x + w, y + h);
      p.lineTo(x + r, y + h);
      p.quadTo(x, y + h, x, y + h - r);
    }else if(c==parent.getComponent(parent.getComponentCount()-1)) {
      //:last-child
      p.moveTo(x, y);
      p.lineTo(x + w - r, y);
      p.quadTo(x + w, y, x + w, y + r);
      p.lineTo(x + w, y + h - r);
      p.quadTo(x + w, y + h, x + w -r, y + h);
      p.lineTo(x, y + h);
    }else{
      p.moveTo(x, y);
      p.lineTo(x + w, y);
      p.lineTo(x + w, y + h);
      p.lineTo(x, y + h);
    }
    p.closePath();
    Area area = new Area(p);

    g2.setPaint(c.getBackground());
    g2.fill(area);

    ssc = TL;
    bgc = BR;
    if(c instanceof AbstractButton) {
      ButtonModel m = ((AbstractButton)c).getModel();
      if(m.isSelected() || m.isRollover()) {
        ssc = ST;
        bgc = SB;
      }
    }
    g2.setPaint(new GradientPaint(x, y, ssc, x, y+h, bgc, true));
    g2.fill(area);

    g2.setPaint(BR);
    g2.draw(area);

    g2.dispose();
  }
  @Override public int getIconWidth()  {
    return 80;
  }
  @Override public int getIconHeight() {
    return 20;
  }
}

References

2009/06/10

aterai

New Tab Button

Code

class TabLayout implements LayoutManager, java.io.Serializable {
  public void addLayoutComponent(String name, Component comp) {}
  public void removeLayoutComponent(Component comp) {}
  public Dimension preferredLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
      Insets insets = parent.getInsets();
      int last = parent.getComponentCount()-1;
      int w = 0, h = 0;
      if(last >= 0) {
        Component comp = parent.getComponent(last);
        Dimension d = comp.getPreferredSize();
        w = d.width;
        h = d.height;
      }
      return new Dimension(insets.left + insets.right + w,
                           insets.top + insets.bottom + h);
    }
  }

  public Dimension minimumLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
      return new Dimension(100, 24);
    }
  }

  public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
      Insets insets = parent.getInsets();
      int ncomponents = parent.getComponentCount();
      int nrows = 1;
      int ncols = ncomponents-1;
      //boolean ltr = parent.getComponentOrientation().isLeftToRight();

      if (ncomponents == 0) {
        return;
      }
      int lastw = parent.getComponent(ncomponents-1).getPreferredSize().width;
      int width = parent.getWidth() - (insets.left + insets.right) - lastw;
      int h = parent.getHeight() - (insets.top + insets.bottom);
      int w = (width > 100*(ncomponents-1))?100:width/ncols;
      int gap = width - w*ncols;
      int x = insets.left;
      int y = insets.top;
      for (int i=0;i < ncomponents;i++) {
        int a = (gap > 0)?1:0;
        gap--;
        int cw = (i==ncols)?lastw:w+a;
        parent.getComponent(i).setBounds(x, y, cw, h);
        x += w + a;
      }
    }
  }
  public String toString() {
    return getClass().getName();
  }
}

References

2008/11/11

aterai

create JTabbedPane like component using CardLayout and JRadioButton(or JTableHeader)

Code

class CardLayoutTabbedPane extends JPanel {
  protected final CardLayout cardLayout = new CardLayout();
  protected final JPanel tabPanel = new JPanel(new GridLayout(1, 0, 0, 0));
  protected final JPanel wrapPanel = new JPanel(new BorderLayout(0, 0));
  protected final JPanel contentsPanel = new JPanel(cardLayout);
  protected final ButtonGroup bg = new ButtonGroup();
  public CardLayoutTabbedPane() {
    super(new BorderLayout());
    int left  = 1;
    int right = 3;
    tabPanel.setBorder(BorderFactory.createEmptyBorder(1,left,0,right));
    contentsPanel.setBorder(BorderFactory.createEmptyBorder(4,left,2,right));
    wrapPanel.add(tabPanel);
    wrapPanel.add(new JLabel("test:"), BorderLayout.WEST);
    add(wrapPanel, BorderLayout.NORTH);
    add(contentsPanel);
  }
  public void addTab(final String title, final Component comp) {
    JRadioButton b = new TabButton(new AbstractAction(title) {
      public void actionPerformed(ActionEvent e) {
        cardLayout.show(contentsPanel, title);
      }
    });
    tabPanel.add(b);
    bg.add(b);
    b.setSelected(true);
    contentsPanel.add(comp, title);
    cardLayout.show(contentsPanel, title);
  }
}

References

2008/03/26

aterai

JTable pagination using RowFilter

Code

public class MainPanel extends JPanel {
  private static final Color evenColor = new Color(240, 255, 250);
  private static final LinkViewRadioButtonUI ui = new LinkViewRadioButtonUI();
  private static int LR_PAGE_SIZE = 5;

  private final TestModel model = new TestModel();
  private final TableRowSorter< TestModel > sorter = new TableRowSorter< TestModel >(model);
  private final Box box = Box.createHorizontalBox();
  public MainPanel() {
    super(new BorderLayout());
    JTable table = new JTable(model) {
      @Override
      public Component prepareRenderer(TableCellRenderer tcr, int row, int column) {
        Component c = super.prepareRenderer(tcr, row, column);
        if(isRowSelected(row)) {
          c.setForeground(getSelectionForeground());
          c.setBackground(getSelectionBackground());
        }else{
          c.setForeground(getForeground());
          c.setBackground((row%2==0)?evenColor:getBackground());
        }
        return c;
      }
    };
    table.setFillsViewportHeight(true);
    table.setIntercellSpacing(new Dimension());
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    table.setRowSorter(sorter);

    for(int i=0;i <= 2008;i++) {
      model.addTest(new Test(" Test: "+i, (i%2==0)?"":"comment..."));
    }
    initLinkBox(100, 1);

    add(box, BorderLayout.NORTH);
    add(new JScrollPane(table));
    setPreferredSize(new Dimension(320, 240));
  }
  private void initLinkBox(final int itemsPerPage, final int currentPageIndex) {
    //assert currentPageIndex > 0;
    sorter.setRowFilter(makeRowFilter(itemsPerPage, currentPageIndex-1));

    ArrayList< JRadioButton > l = new ArrayList< JRadioButton >();

    int startPageIndex = currentPageIndex-LR_PAGE_SIZE;
    if(startPageIndex <= 0) startPageIndex = 1;

//#if 0
    //int maxPageIndex = (model.getRowCount()/itemsPerPage)+1;
//#else
    /* "maxPageIndex" gives one blank page if the module of the division is not zero.
     *   pointed out by erServi
     * e.g. rowCount=100, maxPageIndex=100
     */
    int rowCount = model.getRowCount();
    int maxPageIndex = (rowCount/itemsPerPage) + (rowCount%itemsPerPage==0?0:1);
//#endif
    int endPageIndex = currentPageIndex+LR_PAGE_SIZE-1;
    if(endPageIndex>maxPageIndex) endPageIndex = maxPageIndex;

    if(currentPageIndex > 1)
        l.add(makePNRadioButton(itemsPerPage, currentPageIndex-1, "Prev"));
    //for(int i=startPageIndex;i <= endPageIndex;i++) 
    //    l.add(makeRadioButton(itemsPerPage, currentPageIndex, i-1));
    //if I only have one page, Y don't want to see pagination buttons
    //suggested by erServi
    if(startPageIndex < endPageIndex) {
      for(int i=startPageIndex; i<= endPageIndex;i++) {
          l.add(makeRadioButton(itemsPerPage, currentPageIndex, i-1));
      }
    }
    if(currentPageIndex < maxPageIndex)
        l.add(makePNRadioButton(itemsPerPage, currentPageIndex+1, "Next"));

    box.removeAll();
    ButtonGroup bg = new ButtonGroup();
    box.add(Box.createHorizontalGlue());
    for(JRadioButton r:l) {
      box.add(r); bg.add(r);
    }
    box.add(Box.createHorizontalGlue());
    box.revalidate();
    box.repaint();
    l.clear();
  }

  private JRadioButton makeRadioButton(final int itemsPerPage,
                                       final int current, final int target) {
    JRadioButton radio = new JRadioButton(""+(target+1));
    radio.setForeground(Color.BLUE);
    radio.setUI(ui);
    if(target+1==current) {
      radio.setSelected(true);
      radio.setForeground(Color.BLACK);
    }
    radio.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        initLinkBox(itemsPerPage, target+1);
      }
    });
    return radio;
  }
  private JRadioButton makePNRadioButton(final int itemsPerPage, final int target, String title) {
    JRadioButton radio = new JRadioButton(title);
    radio.setForeground(Color.BLUE);
    radio.setUI(ui);
    radio.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        initLinkBox(itemsPerPage, target);
      }
    });
    return radio;
  }

  private RowFilter< TestModel, Integer > makeRowFilter(final int itemsPerPage, final int target) {
    return new RowFilter< TestModel, Integer >() {
      @Override
      public boolean include(Entry< ? extends TestModel, ? extends Integer > entry) {
        int ei = entry.getIdentifier();
        return (target*itemsPerPage <= ei && ei < target*itemsPerPage+itemsPerPage);
      }
    };
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }

  public static void createAndShowGUI() {
    try{
      UIManager.getInstalledLookAndFeels();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(Exception e) {
      e.printStackTrace();
    }
    final JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

References