Icons not visible in applet


Dear

I have a created a JTree where every node has an icon. One class is that extends JPanel is being used for the tree, and everything works fine and the icons show correctly. However, when I create an instance of that class and add it to my Applet(which contains several other components), the icons are not visible.

Anyone has any idea why

Any suggestions will be appreciated.

Posted On: Monday 22nd of October 2012 01:11:27 AM Total Views:  64
View Complete with Replies

RELATED TOPICS OF Swing / AWT / SWT PROGRAMMING LANGUAGE




SWT Canvas.scroll() removing invisible controls

I observed that while scrolling with scrollbars SWT Canvas removes the control which is not in the visible area. //horizontal scroll canvas.scroll(calculatedDistanceX , 0, 0, 0, screen.getSize().x,screen.getSize().y , true );
VIEWS ON THIS POST

92

Posted on:

Friday 21st September 2012
View Replies!

Scrollbar is not visible

I created a frame which have panel,splitpane and scrollpane. The splitpane have one scrollpane and panel. I have jtree structure of xml and i added it to the scrollpane of splitpane. The scrollbar is not visible, not able to see the nodes beyond the visibility os the scrollpane Here is my code Main app = new Main(); JFrame.setDefaultLookAndFeelDecorated(true); app.f = new JFrame("NCX Generator"); app.pane1=new JPanel(); app.pane2=new JSplitPane(); app.right=new JPanel(); app.left=new JPanel(); app.Scrpane=new JScrollPane(); app.pane2.setRightComponent(app.right); app.pane2.setLeftComponent(app.Scrpane); app.pane3=new JScrollPane(); app.browse = new JButton("BROWSE"); app.save = new JButton("SAVE"); app.search = new JButton("SEARCH"); app.field = new JTextField("Enter Text "); //app.pane2.setOneTouchExpandable(true); app.pane2.setDividerLocation(700); app.Scrpane.setLayout(null); app.Scrpane.setBounds(10, 30, 700, 700); app.Scrpane.setBackground(Color.white); app.right.setLayout(null); app.right.setBounds(700, 30, 700, 700); app.right.setBackground(Color.white); app.f.setLayout(new BorderLayout(100,100)); app.f.setBounds(0, 0, 1300, 720); app.pane1.setLayout(new FlowLayout()); app.pane1.add(app.browse); app.pane1.add(app.save); app.pane1.add(app.search); app.pane1.add(app.field); app.f.add(app.pane1,BorderLayout.NORTH); app.f.add(app.pane2,BorderLayout.CENTER); app.f.add(app.pane3,BorderLayout.SOUTH); app.browse.addActionListener(app); app.save.addActionListener(app); app.search.addActionListener(app); app.f.setVisible(true); } private TreeSelectionListener createSelectionListener() { return new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { String temp; TreePath path = e.getPath(); int pathCount = path.getPathCount(); for (int i = 0; i < pathCount; i++) { System.out.print(path.getPathComponent(i).toString()); if (i + 1 != pathCount) { System.out.print("|"); } if (i + 1 == pathCount) { temp = path.getPathComponent(i).toString(); System.out.println("/n"); System.out.println(temp); } } System.out.println(""); } }; } public void actionPerformed(ActionEvent e) { if (e.getSource() == browse) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); File file = chooser.getSelectedFile(); filename = file.getPath(); System.out.println("You have selected: " + filename); if (filename != "") { InputStream in; try { in = new FileInputStream(new File(filename)); new XmlTree(filename, in); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { WindowUtilities.setNativeLookAndFeel(); JTree tree = new JTree(); tree=new XmlTree(filename); tree.setEditable(true); Scrpane.add(tree); tree.addTreeSelectionListener(createSelectionListener()); tree.setBounds(10, 30, 700, 700); //Scrpane.setPreferredSize(tree.getPreferredSize()); Scrpane.setVisible(true); } catch (IOException ioe) { System.out.println("Error creating tree: " + ioe); } } }
VIEWS ON THIS POST

37

Posted on:

Friday 21st September 2012
View Replies!

JCombobox becomes invisible after adding items to it


VIEWS ON THIS POST

128

Posted on:

Monday 24th September 2012
View Replies!

Graphics2D: create a visible line, read on

Hi Im creating an app where I need that a rectangle be visible all the time over the background. So I created this: public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; if (true) { float[] dash1 = { 2f, 0f, 2f }; g.setColor(Color.black); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.white); BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash1, 2f); g2d.setStroke(bs1); g2d.drawRect(0, 0, getWidth(), getHeight()); } }
VIEWS ON THIS POST

53

Posted on:

Wednesday 26th September 2012
View Replies!

JList set element visible

First, let me describe a couple of restrictions that I have in my environment and the problem that I'm having. So first the restrictions, I'm working on an airgapped environment so everything here has to be hand jammed in by me. There is no way for me to just copy paste my working code in so there may be little typos here and there. Second some of the systems that I'm running this one are fairly archaic, so the newest version of java that I'm using is 1.4 and it's not possible for me to put in backports of new java apis. Now for the problem, I'll post the code below this description. I'm making a gui frontend that has a JList in a tabbed pane and another pane that contains a list of buttons. I want these buttons to act like filters so that when they are pressed the jlist only shows items related to that button. I need to maintain the integrity of the elements in the background so that I can set them to visible and invisible at will. I've thought about extending the buttons and backing them with the list, but that's pretty computationally expensive to do every time there is an update. Anyway, here's some code. public class EventWindow extends JFrame { private JPanel systemPanel = new JPanel(); private JList list; private DefaultListModel model; private Logger log = Logger.getLogger(this.Class().getName()); private double diffThresh = 6.5; private JButton clear = new JButton("Clear "); private JButton remove = new JButton("Remove"); private JButton saveError = new JButton("Save Selected"); private ActionListener clear = new Clear(); private DiceCoefficient dice = new DiceCoefficient(); private Map sysButtons = Collections.synchronizedMap(new HashMap()); private GridBagConstraints c = new GridBagConstraints(); private final Insets insets = new Insets(2,2,2,2); private ActionListener systemListener = new SystemSelected(); private String systemSelected = ""; private int x = 0; private int y = 0; private JTabbedPane tabPane = new JTabbePane(); private boolean allSelected = true; public void draw() { this.setSize(400,500); c.insets = insets; this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create the list to display new messages as they come in model = new DefaultListModel(); model.ensureCapacity(300); list = new JList(model); list.setCellRenderer(new TextCellRenderer()); list.setDoubleBuffered(true); list.setAutoScrolls(true); this.getContentPane().setLayout(new GridBagLayout()); JPanel displayPanel = new JPanel(); displayPanel.setLayout(new GridBagLayout()); JScrollPane buttonScroller = new JScrollPane(systemPanel); systemPanel.setLayout(new GridBagLayout()); JScrollPane listScroll = new JScrollPane(list); c.fill = GridBagConstraints.BOTH; c.weightx = 0.5; c.weighty = 1; c.gridwidth = 3; c.gridx = 0; c.gridy = 0; displayPanel.add(listScroll, c); c.weightx = 0.5; c.weighty = 0; c.gridwidth = 1; c.gridx = 0; c.gridy = 1; c.fill = GridBagConstraints.NONE; displayPanel.add(remove, c); c.gridx =1; displayPanel.add(clear, c); c.gridx = 2; displayPanel.add(saveError, c); c.weighty = 0.9; c.gridx = 1; c.gridy - 0; tabPane.addTab("Event Window", displayPanel); c.weighty = 0; c.gridx = 0; c.gridy = 0; tabPane.addTab("Systems List", buttonScroller); c.gridHeight = 1; c.fill = GridBagConstraints.HORIZONTAL; clear.addActionListener(clear); remove.addActionListener(clear); saveError.addActionListener(clear); c.fill = GridBagConstraints.BOTH; c.weightx = 1; c.weighty = 1; EventWindow.this.getContentPane().add(tabPane, c); c.weightx = 0.5; c.weighty = 0; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.FIRST_LINE_START; } //Public accessor that adds an event to the AWT event queue public syncronized void updateGui(final ListItem li) { SwingUtilities.invokeLater(new Runnable() { updateEventArea(li); }); notify(); } //Method ran to start the process of updating the gui with new information private synchronized void updateEventArea(ListItem li) { deDuplicate(li); processButton(li);//Create or update a button this.validateTree(); notify(); } private void deDuplicate(ListItem li) { //Logic goes here to go through all the list items and use the dice algorithm to remove duplicate messages and append the new model.addElement(li); } private void processButton(ListItem li) { String source = li.getSource(); JButton foundButton = (JButton)sysButtons.get(source); if (foundButton != null) { int numErrors = getSystemCount(source);//Current number of uncleared messages String numErrorsString = Integer.toString(numErrors); int paran = foundButton.getText().indexOf("(");//Get the location of the paran in the button text String newButtonText = foundButton.getText().substring(0,paran) + "(" + numErrorString + ")"; foundButton.setText(newButtonText); } else { JButton button = new JButton(source + "(1)"); sysButtons.put(source, button); button.addActionListener(systemListener); if (x ==5) {x = 0; y++} c.gridx = x; c.gridy = y; systemPanel.add(button, c); x++; } } private int getSystemCount(String source) { int num = 0; for (int i = 0; i < model.size(); i++) { String tracked = ((ListItem)model.get(i)).getSource(); if (source.equals(tracked){ num++; } } return num; } private class Clear implements ActionListener { public void actionPerformed(ActionPerformed e) { if (e.getSource().equals(clear) { if (allSelected) { model.clear(); systemPanel.remove(); sysButtons.clear(); } else { String source = null; for (int i = 0; i < model.getSize(); i++) { ListeItem li = ((ListItem)model.getElementAt(i); source = li.getSource(); if (source.equals(selectedSystem)) model.removeElement(li); } } reLayout(); } else if (e.getSource().equals(remove)) { //Go through the model and remove all the selected Items if (list.getSelectedIndices().length > 0) { int tmp[] = list.getSelectedIndices(); int selected[] = list.getSelectedIndices(); for (int i = tmp.length -1; i >=0; i--) { selected = list.getSelectedIndices(); ListItem li = (ListItem)model.get(selected[i]); model.removeElement(li); } } } /*Go through every button and find lines in the tracker *that are associated with each button. Keep a count * of all these systems, then update the text on the button * to reflect the count of currently pending erros. */ Object keys[]=sysButtons.keySet().toArray(); for (int i = 0; i < key.length; i++) { String source = (String)keys[i]; int num = getSystemCount(source); if (num ==0) { JButton button = (JButton)sysButtons.get(source); systemPanel.remove(button); sysButtons.remove(source); } else { JButton button = (JButton)sysButtons.get(source); button.setText(source + "(" + Integer.toString(num) + ")"; } reLayout(); } } } //The next method lays all the sysButtons out in a nice looking grid private void reLayout() { //Reset x and y x = 0; y = 0; c.fill = GridBagConstraints.HORIZONTAL; systemPanel.remove();//Clear the panel out to be re-populated Object buttons[] = sysButtons.keySet().toArray(); for (int i = 0; i < buttons.length; i++) { if (x > 5) { x = 0;//Start at first column y++;//Start new row c.gridx = x; c.gridy = y; systemPanel.add(((JButton)sysButtons.get((String)buttons[i])), c); x++; } else { c.gridx = x; c.gridy = y; systemPanel.add(((JButton)sysButtons.get((String)buttons[i])), c); x++; } } systemPanel.reValidate(); } /* *Here is where I want the magic to happen. I want to keep all the ListItems that *are values of the model elements, but only show the ones that match the variable *selectedSystem. I've tried setting the setVisible() boolean to false for all the *elements that didn't match, but that didn't work and I'm kinda at a loss here. */ private class SystemSelected implements ActionListener { public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); int paran = button.getText().indexOf("("); selectedSystem = button.getText().substring(0,paran); allSelected = false; tabPane.setSelectedIndex(0); } } }
VIEWS ON THIS POST

67

Posted on:

Wednesday 26th September 2012
View Replies!

Refreshing contents of a ComboBox while it is still visible

I have implemented a ComboBoxModel that is populated based on data from a database: public abstract class DatabaseComboBoxModel extends AbstractListModel implements ComboBoxModel { public static final String CHOOSE_ONE = "(Choose one)"; private SQLUtil sqlUtil; private List list = new ArrayList(); private int selectedIndex = -1; /** * Creates a new instance of DatabaseComboBoxModel. */ public DatabaseComboBoxModel(SQLUtil sqlUtil) throws SQLException { this.sqlUtil = sqlUtil; //this.refreshModel(); } public void refreshModel() throws SQLException { this.list.clear(); this.sqlUtil.setSQL(this.getSQLString()); this.setSQLParameters(this.sqlUtil); ResultSet rs = this.sqlUtil.executeQuery(); while (rs.next()) { DatabaseRecord data = this.getDatabaseRecord(rs); this.list.add(data); } } protected abstract String getSQLString(); protected abstract void setSQLParameters(SQLUtil sqlUtil) throws SQLException; protected abstract DatabaseRecord getDatabaseRecord(ResultSet rs) throws SQLException; public Object getElementAt(int param) { if (param == 0) { return CHOOSE_ONE; } else { return list.get(param - 1); } } public Object getSelectedItem() { if (this.selectedIndex == -1) { return CHOOSE_ONE; } else { return this.list.get(this.selectedIndex); } } public int getSize() { return this.list.size() + 1; } public void setSelectedItem(Object obj) { if (CHOOSE_ONE.equals(obj)) { this.selectedIndex = -1; } else { this.selectedIndex = this.list.indexOf(obj); } } public abstract class DatabaseRecord { private int id; public DatabaseRecord(int id) { this.id = id; } public int getID() { return this.id; } public abstract String toString(); } }
VIEWS ON THIS POST

96

Posted on:

Wednesday 3rd October 2012
View Replies!

System Tray - double click icon to set visible

I have this code working just fine, one option I want is to use a double-click on the icon in the system tray to set the application visible again. I am thinking of using a mouse listener but not sure where I would put it or would I use a window listener
VIEWS ON THIS POST

83

Posted on:

Wednesday 17th October 2012
View Replies!

Invisible Buttons still visible

In our application ,(Talking about applet )when we press a button on our main screen( that is a frame), another frame is open. We have to disable the buttons on previous frame and we have achieved this but the problem is ,the disabled buttons of previous frame are visible on the current frame, although we set their visibility false , that is very annoying and not only the buttons but also the other fields are behaving in same way that is they are visible on current frame, they all are not visible at a same time ,the one which was pressed is displayed on the current screen.For instance , if we press "ADD" button and the next frame is appeared on that press,then the "ADD button" would be visible on the current screen but if we press "modify" button then this time "modify" button would be visible on screen. this visibility is not actually the "visibility".it is some sort of image/water mark.As soon as we click anywhere on current frame,this visibility is no longer sustained. How can we fix this bug How can we stop buttons and fields of previous frame to be seen on current frame One more thing this is not one program issue, we are facing this problem through out the application.
VIEWS ON THIS POST

53

Posted on:

Wednesday 17th October 2012
View Replies!

header color and the cursor double headed arrow in JTable is not visible

I am using NetBeans 6.7.1 In this desktop app after the user imports a file, the data is displayed in the JTable. Before the file is imported the JTable is setVisible(false) Once the user selects a file to open the code is below tableScrollPane.setVisible(true); jTable1.setVisible(true); DataFileTableModel model = new DataFileTableModel(dataList); jTable1.setModel(model); jTable1.getTableHeader().setBackground(Color.blue); jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //HERE is the code to assign the width of the column equal to the length of the header text
VIEWS ON THIS POST

93

Posted on:

Wednesday 17th October 2012
View Replies!

How to make label visible

I have created a radio button group with two buttons in NetBeans, and I would like some text to show up when the button is chosen and become greyed out when it is not chosen. I have tried adding the code: private void rdoNoFracActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: lblIWillChoose.setVisible(true); lblIWillChoose.setEnabled(true); lblUserFrac.setEnabled(false); lblUserFrac.setVisible(false); }
VIEWS ON THIS POST

86

Posted on:

Wednesday 17th October 2012
View Replies!

Why Glass Pane becomes visible when resizing JInternalFrame?

, I've created a JFrame with JDesktop and one JInternalFrame on it. I've also added a glasspane (which draws a black circle only) to JFrame - it's invisible by default but when I resize JInternalFrame it shows up.. is this correct behavior Why it is visible only when I resize and diappears when I release mouse button after resize How I can make it invisible when resizing JInternalFrame (Is removing JFrame's glasspane only solution)import java.awt.Graphics; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JPanel; public class RunMe extends JFrame { public RunMe() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(10, 10, 800, 600); JDesktopPane desktop = new JDesktopPane(); JInternalFrame testJIF = new JInternalFrame("test", true, true, true, true); testJIF.setBounds(10, 10, 100, 100); desktop.add(testJIF); testJIF.setVisible(true); this.add(desktop); this.setGlassPane(new MyGlassPane()); this.setVisible(true); } private class MyGlassPane extends JPanel { public MyGlassPane() { this.setOpaque(false); } @Override protected void paintComponent(Graphics g) { g.fillOval(0, 0, 100, 100); super.paintComponent(g); } } public static void main(String[] args) { new RunMe(); } }
VIEWS ON THIS POST

86

Posted on:

Wednesday 17th October 2012
View Replies!

Does Swing have icons coming for free?

Hi all, I often wondered whether Swing offers icons (like right triangle, left triangle, upward triangle, downward triangle, squares, and etc.) with their package. Can pros here offer an answer If the answer is no, where should I be looking for icons to use in my Java applications There should have already been a set of such standard icons, like open folder, save, zoom in/out, copy, cut, this kind of things.
VIEWS ON THIS POST

18

Posted on:

Thursday 18th October 2012
View Replies!

How to make popup invisible

If I run the following application and then right-click on the application, a popup menu will show up. If I move mouse outside the frame (I try to make JComponent visible so that I can move mouse outside JComponent, but I cannot make it happen), popup menu is still visible. I try to add the following to mouseExited method: popupMenu.setVisible(false); or add the following to let popup listen to mouse listener: popupMenu.addMouseListener(new MouseListener(){ public void mouseExited(final MouseEvent e) { System.out.println("Mouse Exited..."); popupMenu.setVisible(false); //line added } public void mouseEntered(final MouseEvent e) { System.out.println("Mouse Entered..."); } public void mousePressed(final MouseEvent e) { System.out.println("Mouse pressed..."); } public void mouseReleased(final MouseEvent e) { } public void mouseClicked(final MouseEvent e) { System.out.println("Mouse Clicked..."); } });
VIEWS ON THIS POST

79

Posted on:

Thursday 18th October 2012
View Replies!

Empty JPanel's aren't invisible

I've been trying to get JTextFields onto my JFrame using the GridBagLayout. I've managed to do this. I was recommended to use empty JPanels with weightx / weighty set as 1, in order to space things out like I wanted. This seemed to work until I put a jpg image onto my frame, and I noticed 2 small grey(same colour as regular background) squares in the centres of the regions where I placed the 2 empty JPanels. Is there anyway of getting rid of these squares, or doing it some other way\t
VIEWS ON THIS POST

73

Posted on:

Thursday 18th October 2012
View Replies!

Customizing window icons...

In the top left corner of each frame/window is an icon. Is there a way to customize/change this Also is there a way to customise the background color/image on a frame -Tad
VIEWS ON THIS POST

30

Posted on:

Thursday 18th October 2012
View Replies!

How do I make two frame visible simaltenously

Hi! I am having two frame frm1 and frm2. fr1 contains one button and textarea. frm2 contains combo box that contains some text. When I clicks on button frm2 gets visible but frm1 gets mininized, I want to make both the frame visible simaltaneously so that I can insert some text in textarea of frm1 by selectin text from combo box of frm2. Just like modeless dialog box. Is it possible, if yes then how
VIEWS ON THIS POST

65

Posted on:

Thursday 18th October 2012
View Replies!

Make table column invisible

Hi , The following code make the column invisible, but also removes it from the model. table.getColumnModel().removeColumn(table.getColumnModel().getColumn(0)); How can I make the column invisible, but *not* remove it from the model, because I need to obtain the data from the invisible column later on. Also, this is not working, since when I increase column width by dragging I can see some dots and the coumn becomes partially visible: TableColumn idClmn= table.getColumn("Property id"); idClmn.setMaxWidth(0); idClmn.setMinWidth(0); idClmn.setPreferredWidth(0);
VIEWS ON THIS POST

73

Posted on:

Thursday 18th October 2012
View Replies!

how to disable the default menubar in applets...?

hi....... i was created one applet .i am getting default menubar (as applet). now i want to disable the default one to create my own menu bar... is it possible ... if possible how to please help me ...
VIEWS ON THIS POST

36

Posted on:

Monday 22nd October 2012
View Replies!

Printing invisible JTable containing JTables - problem

I am trying to print an invisivle JTable that contains inner JTables (nested). The outer JTable prints as it should, but for some reason the inner JTables header is never printed, even though everything else of the inner JTables is printed as it should, and that is where the problem is. I have tried everything I can think of without solving it. I would be happy if someone could help me with this. Thatnks in advance for any help. In my code the table is made visible after it is printed to show what it should be like. Here's my example code: import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.print.PrinterException; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumn; public class TestNestedJTable2 extends JFrame{ private static final long serialVersionUID = -722808327091366767L; private JTable mainTable; private JTable leftTable; private JTable rightTable; private Object[][] tableList; private Container container; private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; private Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)}, }; private String[] columnNames2 = {"", "Last Name", "Group", "Year"}; private Object[][] data2 = { {false, "Li", "Tiger", new Integer(1997), }, {false, "Roy", "Lion", new Integer(1996)}, {false, "Lee", "Dragon", new Integer(1989)}, {false, "Jones", "Lion", new Integer(1981)}, {false, "Lam", "Dragon", new Integer(1980)}, }; private String[] names = {"sdf", "sdf", "dfs"}; public TestNestedJTable2(){ container = this.getContentPane(); leftTable = new JTable(data, columnNames); rightTable = new JTable(data2, columnNames2); leftTable.getTableHeader().setVisible(true); rightTable.getTableHeader().setVisible(true); leftTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); leftTable.setSize(leftTable.getPreferredSize()); JTableHeader header = leftTable.getTableHeader(); header.setSize(header.getPreferredSize()); rightTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); rightTable.setSize(rightTable.getPreferredSize()); header = rightTable.getTableHeader(); header.setSize(header.getPreferredSize()); tableList = new Object[1][]; tableList[0] = new Object[3]; tableList[0][0] = leftTable; tableList[0][1] = rightTable; tableList[0][2] = "dfgfdg"; mainTable = new JTable(new DefaultTableModel(tableList, names)); TableColumn tc = mainTable.getColumnModel().getColumn(0); tc.setCellRenderer(new CustomTableCellRenderer(leftTable)); tc = mainTable.getColumnModel().getColumn(1); tc.setCellRenderer(new CustomTableCellRenderer(rightTable)); mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); DefaultTableColumnModel colModel = (DefaultTableColumnModel) mainTable.getColumnModel(); TableColumn col = colModel.getColumn(0); col.setPreferredWidth(leftTable.getSize().width + 2); col.setWidth(leftTable.getSize().width + 2); col = colModel.getColumn(1); col.setPreferredWidth(rightTable.getSize().width + 2); col.setWidth(rightTable.getSize().width + 2); mainTable.setRowHeight(leftTable.getPreferredSize().height+leftTable.getTableHeader().getPreferredSize().height+1); mainTable.setMinimumSize(mainTable.getPreferredSize()); mainTable.setSize(mainTable.getPreferredSize()); header = mainTable.getTableHeader(); header.setSize(header.getPreferredSize()); try { mainTable.print(JTable.PrintMode.FIT_WIDTH); } catch (PrinterException e) { // TODO Auto-generated catch block e.printStackTrace(); } container.add(new JScrollPane(mainTable)); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize(800,800); setVisible(true); } class CustomTableCellRenderer extends DefaultTableCellRenderer { private static final long serialVersionUID = 4415155875184525824L; JTable table; CustomTableCellRenderer(JTable table){ this.table=table; this.table.setOpaque(true); this.table.setAlignmentY(JTable.LEFT_ALIGNMENT); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { this.table=(JTable)value; this.table.setPreferredScrollableViewportSize(this.table.getSize()); JScrollPane jCrollPane = new JScrollPane(this.table); jCrollPane.setBorder(BorderFactory.createEmptyBorder()); jCrollPane.getViewport().setSize(new Dimension(this.table.getSize().width-1, this.table.getSize().height+16)); return jCrollPane; } } /** * @param args */ public static void main(String[] args) { new TestNestedJTable2(); } }
VIEWS ON THIS POST

105

Posted on:

Monday 22nd October 2012
View Replies!

How do I control the size of a button (or any component) in an applet or Frame?

I wrote a simple applet with a button and a text box inside of a frame. When I run it and resize it the button resizes with the window or applet section of html doc. How can I prevent the button from resizing Here is my code so far. The maxsize method does not seem to work. import java.awt.*; public class FtoC extends java.applet.Applet { /** Initializes the applet FtoC */ public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } private void initComponents() { jButton1 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); setLayout(new java.awt.BorderLayout()); jButton1.setLabel("FtoC"); add(jButton1, java.awt.BorderLayout.CENTER); jTextField1.setText("jTextField1"); add(jTextField1, java.awt.BorderLayout.NORTH); } private javax.swing.JButton jButton1; private javax.swing.JTextField jTextField1; public static void main(String [] args) { Frame f = new Frame("ViewFtoC"); //Example5 ex = new Example5(); FtoC myEx = new FtoC(); //ex.init(); myEx.init(); myEx.jButton1.setSize(50, 100); Dimension myMax = new Dimension(100,50); //Width, Height myEx.jButton1.setMaximumSize(myMax); //f.add("Center", ex); f.add("Center", myEx); f.pack(); //f.show(); f.setVisible(true); }
VIEWS ON THIS POST

38

Posted on:

Monday 22nd October 2012
View Replies!