31783

Code is getting displayed rather than components

Please have a look at the following code

import java.awt.GridLayout; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; public class TestSend extends JFrame { private Box names, emails; private JButton ok; private Map mMap; private JLabel nameLabel, emailLabel; private JPanel mainPanel; private JScrollPane scroll; private JTable table; private Object[][] data; private int counter = 0; private List nameHolder; private String[] colNames = {"Names","Emails"}; public TestSend() { nameHolder = new ArrayList(); names = new Box(BoxLayout.Y_AXIS); emails = new Box(BoxLayout.Y_AXIS); nameLabel = new JLabel("Names"); emailLabel = new JLabel("Email"); mainPanel = new JPanel(); mainPanel.setLayout(new GridLayout(2,2)); scroll = new JScrollPane(mainPanel); mainPanel.add(nameLabel); mainPanel.add(emailLabel); mainPanel.add(names); mainPanel.add(emails); mMap = new HashMap(); mMap.put("yohan", "yy@yahoo.com"); mMap.put("Gihan", "gihan@yahoo.com"); mMap.put("Sumi", "sumi@yahoo.com"); mMap.put("mac", "mac@yahoo.com"); mMap.put("Jay", "jay@yahoo.com"); mMap.put("Rom", "rom@yahoo.com"); mMap.put("shane", "shane@yahoo.com"); mMap.put("Mafe", "mafe@yahoo.com"); mMap.put("willi", ""); data = new Object[mMap.size()][mMap.size()]; Iterator iter = mMap.entrySet().iterator(); while(iter.hasNext()) { Map.Entry mEntry = (Map.Entry)iter.next(); JCheckBox cBox = new JCheckBox((String)mEntry.getKey()); JLabel lLabel = new JLabel(); names.add(cBox); String cName = cBox.getText(); nameHolder.add(cName); if((String)mEntry.getValue() != null && ((String)mEntry.getValue()).length() != 0 && !((String)mEntry.getValue()).equals("")) { lLabel = new JLabel((String)mEntry.getValue()); // lLabel.setPreferredSize(new Dimension(cBox.getPreferredSize().width,cBox.getPreferredSize().height)); emails.add(lLabel); emails.add(new JPanel()); } else { lLabel = new JLabel(); //lLabel.setPreferredSize(new Dimension(cBox.getPreferredSize().width,cBox.getPreferredSize().height)); emails.add(lLabel); emails.add(new JPanel()); } data[counter][0] = cBox; data[counter][1] = lLabel; counter++; } table = new JTable(data,colNames); this.add(new JScrollPane(table)); this.pack(); this.setVisible(true); } public static void main(String[]args) { new TestSend(); } }

Here when I run this, what is getting displayed is code, not JCheckBox or JLabel. Situation is displayed in the attachment. Please help

<img src="https://i.stack.imgur.com/0gquU.png" alt="enter image description here">

Answer1:

You are using JTable fundamentally wrong. You don't add Components to the Model that are displayed by the JTable. Your Model consists of the values. The JTable uses Renderer to display these values. As your values are Components, a type for what no specific renderer is registered, the default renderer is used: toString. Which is not the code, but a String representation of the component.

Please have a look at the JTable tutorial. You need to start over from the beginning.

Answer2:

Change:

... data[counter][0] = cBox; data[counter][1] = lLabel; ....

To:

.... data[counter][0] = cBox.getText(); data[counter][1] = lLabel.getText(); .....

<img src="https://i.stack.imgur.com/cp8i8.png" alt="enter image description here">

Recommend

  • How to retrieve string from UserDefaults in Swift?
  • pass a variable into $routeProvider in angular between views? - angularjs
  • Retain UITableView cell state when UISearchBar is used
  • Is a UITableViewCell as parameter not a copy?
  • IOS : com.facebook.sdk error 5
  • How to set background color in CCLabelTTF in cocos2d
  • How to display the values of each array in larger array?
  • Facebook Graph Request is returning name but not email
  • in R sort row data in ascending order
  • R replacing columns by lookup to dictionary
  • Paypal reports email address already taken when trying to import test accounts into Paypal Developer
  • Google Contacts API get phone number (PHP)
  • How to bind Backbone model to jqGrid?
  • How can I manually set the document id in a corpus?
  • mail server requires authentication when attempting to send to a non-local e-mail address when using
  • get all processes in parallel
  • How VBA declared Volatility works
  • Installed module is empty
  • Email verification using google app script and google forms
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • How to handle AllServersUnavailable Exception
  • vba code to select only visible cells in specific column except heading
  • How to set my toolbar fixed while scrolling android
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal