Does Swing need another Layer?


A lot of people out there in the community keep addressing the fact that they think Swing needs a higher level that developers use. Similar I supose to how SWT has JFace.

Personally, I think this might be a good idea. So I started playing around with some ideas. Don't slam me on this simple primative example, but see if this is something you think is a good idea to build off of.

When I use a JTextArea there is a lot of initial constructing that needs to be done. For example, I always word and line wrap and I always have a JScrollPane. Here is a typical initialization of a JTextArea:

JTextArea text = new JTextArea(10, 10); text.setWrapStyleWord(true); //false by default. Don't know why text.setLineWrap(true); //False by default. Don't know why JScrollPane textScroll = new JScrollPane(text); container.add(textScroll);
Posted On: Tuesday 23rd of October 2012 11:31:25 PM Total Views:  220
View Complete with Replies

RELATED TOPICS OF Swing / AWT / SWT PROGRAMMING LANGUAGE




JPanel repaint(): what does it do inside?

I have no problem using repaint(). But I'm curious to know what it does inside. Does it simply call the paintComponent() method again or do something special I can't imagine how the program will know what and how to repaint if it doesn't call my overridden paintComponent() method
VIEWS ON THIS POST

90

Posted on:

Wednesday 17th October 2012
View Replies!

JComboBox.setSelectedItem() does not update the displayed value

Hi. Consider the following code (trimmed): public class RootEntityChooser implements View, ActionListener { private static final long serialVersionUID = -245102188976205112L; private EntityList entityList; private JComboBox rootEntityComboBox; private ComboBoxModel comboBoxModel; private EventBus eventBus; public RootEntityChooser(EntityList entityList, EventBus eventBus) { this.eventBus = eventBus; this.entityList = entityList; createComboBoxModel(); rootEntityComboBox = new JComboBox(comboBoxModel); rootEntityComboBox.addActionListener(this); } private void createComboBoxModel() { comboBoxModel = new EntityComboBoxModel(entityList); } @Override public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox) e.getSource(); String selectedEntityName = (String) cb.getSelectedItem(); for (Entity en : entityList) { if (en.getDisplayName().equals(selectedEntityName)) { ERTreeItem rootItem = new ERTreeItem(en); eventBus.publish(new RootEntitySelectedEvent(rootItem)); } } } public void setSelectedEntity(ERItem selectedItem) { rootEntityComboBox.setSelectedItem(selectedItem.getDisplayName()); } }
VIEWS ON THIS POST

119

Posted on:

Wednesday 17th October 2012
View Replies!

tablecelleditor doesn't call setvalueat

Hi There, I use comboboxes within my JTable. If I use the DefaultCellEditor, it calls the setValueAt() method of my TableModel perfectly so that I can change the content of my model. However, if I use my custom TableCellEditor, it doesn't call the setValueAt() method. Any idea how I can "convince" my TableCellEditor to call setValueAt() when its value changes
VIEWS ON THIS POST

92

Posted on:

Wednesday 17th October 2012
View Replies!

MutableTreeNode that does not allow duplicate Objects

What I need is a MutableTreeNode that does not allow two references to the same object. What I am thinking is make a subclass of DefaultMutableTreeNode and override the add method. add(MutableTreeNode newchild){ // checks if the object referenced by newchild is referenced by any node in this node // and if not referenced super.add(newchild); } Would this work Is it how such a thing is usually done Also, to check if two objects are the same or not, the JVM calls the equals method, I believe. But the argument to equals() is an object of the same class, isn't it For a String it is boolean equals(String otherString){} and for an ArrayList, boolean equals(ArrayList otherArrayList){} or are the equals methods all for Objects boolean equals(Object newObject){} What if you have a class Dog and you want the program to consider two Dogs to be the same Dog if they have the same name Then you would get an exception by trying to pass a Cat object to the equals method because the equals method asks for the name, which does not exist in the Cat object. But I don't want an exception. I want a return of false because a Cat is not a Dog!
VIEWS ON THIS POST

81

Posted on:

Wednesday 17th October 2012
View Replies!

JOptionPane doesn't show

The program gets to the JOptionPane part, reads it, then returns to the calling method without reading the rest of the recTrip method. If I comment the JOptionPane out the program runs through recTrip with no problems. I put the JOptionPane in a try/catch hoping to catch an error but to no avail. Here is the code: public void recTrip(Driver dri, Object oldVal){ log.debug("In recTrip"); try{ int result = JOptionPane.showConfirmDialog(this, "This trip has been reconciled. Do you want to change it"); }catch(HeadlessException he){ log.debug("Headless " + he); } log.debug("Past JOptionPane"); try { ejbtm.setValue(ejbtm.getCurrentRowValue("main_dri_uid"), row, ejbtm.getRealColumnName(jt.getSelectedColumn())); updateWeekWithNewResource("Driver", "DRI", dri, (EjbTableModel)triptm.getValue(0, "DRI"), (ArrayList)triptm.getValue(0, "DRI_AVL_AL"), "driver_nbr", oldVal); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
VIEWS ON THIS POST

73

Posted on:

Wednesday 17th October 2012
View Replies!

JTable.print() does not work

The objective is to get data from the DB and print it. I got connected to the DB, got the required data. Poplated the data in JTable and used the below mentioned method to print. print(JTable.PrintMode.NORMAL, new MessageFormat("Page Header "), new MessageFormat("Page Footer"), false, null, false);
VIEWS ON THIS POST

73

Posted on:

Wednesday 17th October 2012
View Replies!

What skills does a programmer need to create rich client now?

I'm convinced by your replies on some pastes. I've asked a similar question two years ago. What I've done are: 1 Learn how to use Swing components, layouts. The books before Java Desktop Live seem all in this category. I find Java Tutorial's Creating a GUI with JFC/Swing is enough for this. 2 Learn some guidlines on building GUI. I read several chapters of book Java Look And Feel Design Guidlines 3 Learn multi-thread. Basicaly I read articles in java.net on this issue. And apply SwingWorker in my application. What else With your FRC book, it seems more things need to be learned. You are Swing gurus, would you mind answering this question once more
VIEWS ON THIS POST

95

Posted on:

Wednesday 17th October 2012
View Replies!

Thread in paintComponent doesn't paint

Hi ranchers, I made a test application to just paint a dot. The paintComponent was overridden in an anonymous class of JPanel. When I changed the line commented out (bold) in the following import javax.swing.*; import java.awt.*; public class Test extends JFrame { private JPanel panel; Test() { super("T E S T"); initGUI(); } private void initGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); panel = new JPanel() { @Override protected void paintComponent(final Graphics g) { [I]Runnable dotMaker = new Runnable() { public void run() { g.fillOval(100, 50, 100, 100); System.out.println("dot"); } }; new Thread(dotMaker).start();[/I] [B]// g.fillOval(100, 50, 100, 100);[/B] } }; add(panel); setVisible(true); } public static void main(String[] args) { new Test(); } }
VIEWS ON THIS POST

65

Posted on:

Thursday 18th October 2012
View Replies!

SWT setFocus doesn't work

I have a JFace TitleAreaDialog with several Text widgets. A FocusListener on these Text widgets checks on FocusLost, if the input is valid e.g. a String value. If validation fails, an error message is printed. I would like to have the focus back in field that failed to validate. I tried: Text text = (Text) focusEvent.getSource(); text.setFocus(); This doesn't work, does anybody knows why What else can I do, to remain in a Text widget, if a validation failed
VIEWS ON THIS POST

84

Posted on:

Thursday 18th October 2012
View Replies!

Generic Ok / Cancel Screen --- Alert doesn't do

How can I make a generic Ok/Cancel Screen that I can reuse again and again. example: A user has just pressed a button to delete a file. This file has names of other files in it that must also be deleted. I want to do the following: if (GenericOkCancel("The Following Sub Files must be deleted Too")){ //They pressed 'Ok' so delete everything } --- An Alert will (perhaps by blocking) return to the same place in my code as it was called from (or so i believe)... is there a way that i can extend an alert functionally to have it make use of Ok/Cancel and tell me what ppl pressed If not then how else can I immplement such a screen There are countless examples of when i would like to get a users response to a question and have it return to the same position in execution with a response.
VIEWS ON THIS POST

70

Posted on:

Thursday 18th October 2012
View Replies!

setPage(URL) in JEditorPane doesn't work with j2sdk 1.4.0

everybody, within an application I use JEditorPane.setPage() to call an URL from a Website in the Internet. While working with j2sdk 1.4.2, there're no problems. After testing the programm with j2sdk 1.4.0 it comes out that the setPage()-call causes a NullPointerException, nothing is showed in the JEditorPane... Do anybody know some workaround for this bug, because I need to run this app also within the j2sdk 1.4.0 Here're some facts: // I use some Threads before.. // all this work runs in a Thread: Thread two = new Thread() { public void run() { // running request... // getting information... // setting result in a output-window... } }; two.setPriority(Thread.MIN_PRIORITY); two.start();
VIEWS ON THIS POST

100

Posted on:

Thursday 18th October 2012
View Replies!

JSpinner does not work for Numbers

Can you please have a look at the following code to figure out why my customer JSpinner throws a runtime "java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer" error import java.awt.*; import java.math.BigDecimal; import java.text.NumberFormat; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import javax.swing.*; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.NumberFormatter; public class MyNumberSpinner extends JSpinner { SpinnerModel model = null; JComponent editor = null; public void initialize(BigDecimal initialValue, int dps) { double precision = Math.pow(10, dps); double stepSize = 1.0 / precision; model = new SpinnerNumberModel(initialValue, 0, 1000000.0, stepSize); StringBuffer bf = new StringBuffer("####0."); for (int i=0; i
VIEWS ON THIS POST

68

Posted on:

Monday 22nd October 2012
View Replies!

JFrame declared as setAlwaysOnTop doesn't stay on top during slide show

We came across a very typical difference in the behavior of JFrame and JWindow. We are maintaining a desktop application developed in java swing with one of the functionality to capture the screen. The application is a JFrame with various buttons added to it. The JFrame is set as setAlwaysOnTop(true). This screen capture works fine for all cases expect when OpenOffice presentation is running in slideshow mode on Ubuntu 8.04. When the slideshow for presentation is activated, the presentation is coming on top of JFrame. Even though JFrame is declared as setAlwaysOnTop, it is not coming on top of slideshow. This is preventing the user from initiating the capture. This is happening only on Ubuntu (Linux Flavor). It works fine on Windows. When we made a minor change (i.e. changed the JFrame to JWindow), it is staying always on top even when slideshow is activated. Please go though the below example code. When the below code is run, the JFrame stays on top till a presentation is opened and slideshow activated. When slide show is running, it goes behind the slide show. If you change the JFrame to JWindow (shown in commented line), it is staying always on top even when slideshow is activated and we dont even have the setAlwaysOnTop() property for JWindow. import javax.swing.JFrame; import javax.swing.JWindow; public class TopJFrameTester { public static void main(String[] args) { //JWindow topContainer = new JWindow(); JFrame topContainer = new JFrame(); topContainer.setSize(300, 300); topContainer.setAlwaysOnTop(true); topContainer.setVisible(true); } }
VIEWS ON THIS POST

105

Posted on:

Monday 22nd October 2012
View Replies!

NetBeans manually added component does not appear

, When I programmatically add a component such as a list or button to a frame that I have used code to build then it will appear fine but I have an issue where the NetBeans frame that I have created and which is full of the NetBeans generated code, does not seem to respect the component that I add and thus it is not displayed. I can for example, add a border to the panel through code which does show on screen when the project is run but a JButton or list for example will not. Why might this be public class MainFrame extends javax.swing.JFrame { /** Creates new form MainFrame */ public MainFrame() { initComponents(); this.setDefaultCloseOperation(EXIT_ON_CLOSE); Border spacer = BorderFactory.createEmptyBorder(5, 5, 5, 5); jPanel1.setBackground(Color.WHITE); jPanel1.setBorder(BorderFactory.createLineBorder(Color.BLACK,1)); jPanel2.setBorder(spacer); jPanel3.setBorder(spacer); Set set = Categories.parentCategories.keySet(); Object[] categories = set.toArray(); JList categoryList = new JList(categories); JScrollPane jsp = new JScrollPane(categoryList); // adding the list below does not appear jPanel2.add(jsp); \t\t\t\t\t\t // even if I add a new button it does not show jPanel2.add(new JButton("Test")); \t\t\t\t\t\t } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jPanel3 = new javax.swing.JPanel(); jPanel4 = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Personal Home Budget"); jPanel2.setBackground(new java.awt.Color(241, 255, 204)); jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder()); org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 289, Short.MAX_VALUE) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 154, Short.MAX_VALUE) ); jPanel3.setBackground(new java.awt.Color(241, 255, 204)); jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder()); org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 523, Short.MAX_VALUE) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 94, Short.MAX_VALUE) ); org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 100, Short.MAX_VALUE) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 100, Short.MAX_VALUE) ); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(30, 30, 30) .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(69, 69, 69) .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(38, 38, 38) .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(50, Short.MAX_VALUE)) ); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(24, 24, 24) .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(20, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(25, Short.MAX_VALUE)) ); pack(); }// /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; // End of variables declaration }
VIEWS ON THIS POST

70

Posted on:

Monday 22nd October 2012
View Replies!

setText doesn't display html!

Hi all, I've been searching here as well as googling, but haven't found anything! My problem is: I'm trying to JEditorPane as a simple web browser, but with setText rather than setPage method... So, when I give setText simple html code like this, or even more as long as it is within the parentheses : setText("WelcomeWelcomeWelcome");
VIEWS ON THIS POST

83

Posted on:

Monday 22nd October 2012
View Replies!

JFrame doesnt display image until it is resized

I have written a simple code with a JFrame displaying an image. The problem I am facing is that the image does not appear when I execute the code. At first the JFrame is blank. If I now resize the JFrame, the image is displayed. Could you help me figure out the problem please. Please find my code below public class ImageTest extends JFrame { private JLabel jLabelImage; private JPanel jPanelImage; public ImageTest() { this.getContentPane().setLayout( null ); setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE ); //add the image to panel 1 jLabelImage = new JLabel(); jLabelImage.setBounds( 0, 0, 820, 580 ); ImageIcon mapImageIcon = new ImageIcon( "car.jpg" ); jLabelImage.setIcon( mapImageIcon ); //display panel 1 jPanelImage = new JPanel(); jPanelImage.setLayout( null ); //jPanelImage.setBorder( javax.swing.BorderFactory.createEtchedBorder()); jPanelImage.add( jLabelImage ); jPanelImage.setBounds( 0, 0, 820, 580 ); this.getContentPane().add( jPanelImage ); } public static void main(String[] args) { ImageTest newImageTest = new ImageTest(); newImageTest.setVisible( true ); newImageTest.setBounds( 0, 0, 1200, 700 ); } }
VIEWS ON THIS POST

81

Posted on:

Monday 22nd October 2012
View Replies!

What does Container mean in Swing

I have just starting using swing and am reading the sun homepage about swing .There while i was reading i encountered many places where the word container was used .Please can any one tell me what exactly is a container in Swing .I am familiar with Web technology and there container is a place were all the code like jsp/servlet reside.Is it the same in Swing .If possible please explain it too (with regards to swing).
VIEWS ON THIS POST

52

Posted on:

Monday 22nd October 2012
View Replies!

Border Layout does not paint

Tried two approaches: add(textArea,BorderLayout.CENTER); addLayoutComponent(textArea,BorderLayout.CENTER); JWindow shows up on screen and responds to window close operation, background is painted, but nothing shows up in the window. // no-args constructor public GUI() { super("prototype");// try { // GUI.this.borderlayout = new java.awt.BorderLayout(1,1);// GUI.this.setBackground(java.awt.Color.LIGHT_GRAY); GUI.this.setDefaultCloseOperation(EXIT_ON_CLOSE);// // GUI.this.textArea = new javax.swing.JTextArea("prototype",128,64); GUI.this.add(GUI.this.vernList , BorderLayout.NORTH); GUI.this.add(GUI.this.termsList, BorderLayout.SOUTH); GUI.this.add(GUI.this.wordsList, BorderLayout.EAST); GUI.this.add(GUI.this.fieldsList,BorderLayout.WEST); GUI.this.add(GUI.this.textArea, BorderLayout.CENTER); GUI.this.setPreferredSize(new java.awt.Dimension(800,640));// GUI.this.setLayout(GUI.this.borderlayout);// //super.setVisible(true); is in init super.pack();// } catch(java.awt.HeadlessException he) { this.isValid = false;// } }
VIEWS ON THIS POST

83

Posted on:

Monday 22nd October 2012
View Replies!

component.removeAll() that doesnt work

hi i have this problem, once again i have a splitpane that has 2 scrollpanes on the left and on the right.the left scrollpane contains a tree and the right sp contains a jpanel that displays various components depending on the node selected. the problem is , when selected , everything seems to work fine until the retrieval of the gui in the userObject (userObjec.getGui() returns a jpanel containing components).To display the gui i do something like: //first display guiPanel = new JPanel(new BorderLayout()); guiPanel.add(uo.getGUI(), BorderLayout.PAGE_START); //after an event in the valueAdded() method of treeSelectionListener //******the problem is here guiPanel.remove(); guiPanel.add(uo.getGUI(), BorderLayout.PAGE_START); guiPanel.validate(); no uo.getGui() returns the correct jpanel (i did a print() ) , but the remove all doesnt seem to work well because at times ,the component displayed on the right is the correct one and at times it doesn't change.. which seems bizzaire.. any ideas.. hoping i was clear enuff
VIEWS ON THIS POST

103

Posted on:

Tuesday 23rd October 2012
View Replies!

Submenu in a submenu does not open when the mouse moves over it

I have a web browser application with a favorites menu. The menu has an two options to import favorites from IE and Firefox. When the application imports favorites from IE, it places the favorites as items under the import option. If there are folders of favorites, it will add a submenu containing the favorites in the folder. The problem I have is that after I import the favortites and it creates the submenus, some submenu's don't open the mouse pointer is moved over them. Some menu work fine and when the mouse pointer is moved over such a, then the other submenu open fine as well, but only after selecting a submenu that listens. I have debugged the code and the menu items are added before selecting the menu. I don't need a listener for a submenu Does anyone have an idea what I am overlooking Thanx
VIEWS ON THIS POST

76

Posted on:

Tuesday 23rd October 2012
View Replies!