This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox attached to a JTable will not appear!!!!!!  RSS feed

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have attempted to attach a JComboBox to a JTable header
but the comboBox will not respond can someone please help I have been stuck for two weeks I have already visited http://www2.gol.com/users/tame/swing/examples/JTableExamples9.htm
as some of you suggested but the code provided does not work.
Thank u for your time
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
import jp.gr.java_conf.tame.swing.table.*;
public class PushableHeaderExample extends JPanel
{
public static JFrame f;

class HeaderListener extends MouseAdapter
{
JTableHeader header;
ComboRenderer renderer[];

HeaderListener(JTableHeader header,ComboRenderer renderer[])
{
this.header = header;
this.renderer = renderer;
}
public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
TableColumnModel columnModel =header.getColumnModel();
header.repaint();

System.out.println("Ouch! " + p);
}

public void mouseReleased(MouseEvent e)
{
int col = header.columnAtPoint(e.getPoint());
header.repaint();
}
}

class ComboRenderer extends JComboBox
implements TableCellRenderer,ActionListener
{
int pushedColumn;
JComboBox menuBar = new JComboBox();
ComboRenderer(String[] items)
{
for(int i = 0; i < items.length; i++)
{
menuBar.addItem(items[i]);
}
menuBar.setSelectedIndex(0);

menuBar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("Im here");
menuBar.showPopup();
}
});
pushedColumn = -1;

}

public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
return this;
}
public void setPickedColumn(int col)
{
pushedColumn = col;
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Im here");
JMenu combo = (JMenu)e.getSource();
combo.setPopupMenuVisible(true);
}
}


public PushableHeaderExample()
{
setLayout(new BorderLayout());
String[] headerStr = {"Push","me","here"};

DefaultTableModel dm = new DefaultTableModel(headerStr, 4);
JTable table = new JTable(dm);

ComboRenderer comboList[] = new ComboRenderer[3];
for(int i = 0; i < comboList.length; i++)
{
comboList[i] = new ComboRenderer(headerStr);
}

TableColumnModel model = table.getColumnModel();

for (int i=0;i<comboList.length;i++) {>
model.getColumn(i).setHeaderRenderer(comboList[i]);
}

JTableHeader header = table.getTableHeader();
header.addMouseListener(new HeaderListener(header,comboList));
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
f.getGlassPane().setVisible(true);
}
public static void main(String[] args) {
f= new JFrame("PushableHeaderTable Example");
f.getContentPane().add(new PushableHeaderExample(), BorderLayout.CENTER);
f.setSize(400, 100);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!