2013-04-07 22 views
7

Vorrei che la cella del mio JTable fosse allineata orizzontalmente con i pannelli selezionati.
Ecco un SSCCE per illustrare il mio problema. Grazie per qualsiasi aiuto.
Scorrimento a livello di codice

public class TableCellAlignment { 

    private final static int MAX = 50; 
    private static SelectablePanel[] selectablePanels = new SelectablePanel[MAX]; 
    private static JScrollPane slaveScrollPane = new JScrollPane(); 
    private static JScrollPane masterScrollPane = new JScrollPane(); 
    private static JTable slaveTable = new JTable(); 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TableCellAlignment().createGUI(); 
      } 
     }); 
    } 

    private static void createGUI() { 
     JFrame f = new JFrame("TableCellAlignment"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel masterPanel = new JPanel(new GridLayout(MAX, 1)); 
     Integer[][] objs = new Integer[MAX][1]; 
     for (int i = 0; i < MAX; i++) { 
      objs[i][0] = new Integer(i); 
      SelectablePanel masterSelectablePanel = new SelectablePanel(); 
      masterSelectablePanel.setNum(i); 
      selectablePanels[i] = masterSelectablePanel; 
      masterPanel.add(masterSelectablePanel); 
     } 
     DefaultTableModel model = new DefaultTableModel(objs, new Object[]{"Column1"}); 
     model.addTableModelListener(new TableModelListener() { 
      @Override 
      public void tableChanged(TableModelEvent e) { 
       EventQueue.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         slaveTable.setRowHeight(20); 
        } 
       }); 
      } 
     }); 
     model.addRow(objs); 
     slaveTable.setModel(model); 
     final JPanel p = new JPanel(new GridLayout(1, 2)); 
     masterScrollPane.setViewportView(masterPanel); 
     slaveScrollPane.setViewportView(slaveTable); 
     p.add(masterScrollPane); 
     p.add(slaveScrollPane); 
     f.add(p); 

     f.setSize(400, 200); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class SelectablePanel extends JPanel { 

     private PropertyChangeSupport cs; 
     private int num; 
     private boolean selected = false; 

     public SelectablePanel() { 
      cs = new PropertyChangeSupport(this); 
      cs.addPropertyChangeListener(new SelectedPropertyChangeListener()); 
      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        setSelected(true); 
       } 
      }); 
     } 

     public int getNum() { 
      return num; 
     } 

     public void setNum(int num) { 
      this.num = num; 
     } 

     public boolean isSelected() { 
      return selected; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (selected) { 
       Color c = g.getColor(); 
       g.setColor(Color.blue); 
       g.fillRect(0, 0, getWidth(), getHeight()); 
       g.setColor(Color.white); 
       FontMetrics fm = g.getFontMetrics(); 
       g.drawString("" + getNum(), getWidth()/2, (getHeight() + (fm.getAscent() - fm.getDescent()))/2); 
       g.setColor(c); 
      } 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(100, 20); 
     } 

     public void setSelected(boolean selected) { 
      boolean oldVal = isSelected(); 
      this.selected = selected; 
      cs.firePropertyChange("selected", oldVal, selected); 
      repaint(); 
     } 

     private class SelectedPropertyChangeListener implements PropertyChangeListener { 

      @Override 
      public void propertyChange(PropertyChangeEvent evt) { 
       if (evt.getPropertyName().equals("selected")) { 
        boolean selected = (boolean) evt.getNewValue(); 
        if (selected) { 
         for (int i = 0; i < MAX; i++) { 
          SelectablePanel masterSelectablePanel = selectablePanels[i]; 
          if (i != getNum() && masterSelectablePanel.isSelected()) { 
           masterSelectablePanel.setSelected(false); 
          } 

         } 
         slaveTable.setRowSelectionInterval(getNum(), getNum()); 
         final JViewport viewport = slaveScrollPane.getViewport(); 
         Rectangle rect = new Rectangle(getBounds().x, getBounds().y, 1, 1); 
         Rectangle r2 = viewport.getVisibleRect(); 
         slaveTable.scrollRectToVisible(new Rectangle(rect.x, rect.y, (int)r2.getWidth(), (int)r2.getHeight())); 
        } 
       } 
      } 
     } 
    } 
} 
+0

btw, +1 per la fornitura di SSCCE. BTW: nel codice del mondo reale, si farebbe il ridisegno nel setSelected e non nell'ascoltatore. Ma poi, questo è solo un esempio :-) – kleopatra

+0

Ok, grazie per il suggerimento. Penso che lo modificherò comunque per usare JList, penso che sia quello che mKorbel mi suggerisce di chiarire, ma il suo post è stato cancellato. Grazie per la tua risposta. – jerome

+0

come ho commentato (commento eliminato) la migliore domanda sulla scorsa settimana ...., quindi ho solo commentato, [le risposte sono in giro, alcune con grande qualità, ma ho bisogno di sapere come cercare qui] (http: // StackOverflow. it/questions/15868553/fit-all-components-from-jframe-to-actual-size-of-window/15868588 # comment22600617_15868588), non mettere JComponets su JList o JTable, solo la sua descrizione, Valore stringa per JButton, lascia questo lavoro (dipingendo JButton) per XxxRenderer, con la sua risposta piangendo per il tuo upvote – mKorbel

risposta

3

E 'matematica di base e non richiede l'accesso alla finestra:

// in the isSelected block of the propertyChangeListener: 
JComponent current = (JComponent) evt.getSource(); 
slaveTable.setRowSelectionInterval(getNum(), getNum()); 
// get the cellRect of the selected cell 
Rectangle cellRect = slaveTable.getCellRect(getNum(), 0, false); 
// get the bounds of the selected panel 
Rectangle panelRect = current.getBounds(); 
// get the visible rect of the selected panel's parent 
Rectangle parentVisibleRect = ((JComponent) current.getParent()).getVisibleRect(); 
// the diff above the current (to the parent's visible rect) 
int aboveCurrent = panelRect.y - parentVisibleRect.y; 
// translate the cell rect 
cellRect.y = Math.max(cellRect.y - aboveCurrent, 0); 
// adjust size to slaveTable's visible height 
cellRect.height = slaveTable.getVisibleRect().height; 
slaveTable.scrollRectToVisible(cellRect); 

Si noti che questo frammento presuppone che viewport di entrambi i genitori del pannello e la tabella della vista hanno la stessa dimensione, in modo da rimuovere il intestazione dalla tabella, o aggiungi un'intestazione allo scrollPane del pannello, oppure usa un LayoutManager che può allineare le finestre dei due scrollPanes.

3

viewport. setViewPosition (pt); as shown here.

+0

quell'esempio è corretto (sorprendente in sé, proveniente da java2s.com ;-) ma nel contesto di questa domanda, non è necessario accedere il viewport :-) – kleopatra

Problemi correlati